|
1
|
|
|
<?php |
|
2
|
|
|
namespace Ubiquity\security\acl; |
|
3
|
|
|
|
|
4
|
|
|
use Ubiquity\security\acl\models\AclList; |
|
5
|
|
|
use Ubiquity\security\acl\models\Role; |
|
6
|
|
|
use Ubiquity\security\acl\models\Resource; |
|
7
|
|
|
use Ubiquity\security\acl\models\Permission; |
|
8
|
|
|
use Ubiquity\security\acl\models\AclElement; |
|
9
|
|
|
use Ubiquity\security\acl\persistence\AclProviderInterface; |
|
10
|
|
|
|
|
11
|
|
|
/** |
|
12
|
|
|
* Ubiquity\security\acl$AclManager |
|
13
|
|
|
* This class is part of Ubiquity |
|
14
|
|
|
* |
|
15
|
|
|
* @author jc |
|
16
|
|
|
* @version 1.0.0 |
|
17
|
|
|
* |
|
18
|
|
|
*/ |
|
19
|
|
|
class AclManager { |
|
20
|
|
|
|
|
21
|
|
|
/** |
|
22
|
|
|
* |
|
23
|
|
|
* @var AclList |
|
24
|
|
|
*/ |
|
25
|
|
|
protected static $aclList; |
|
26
|
|
|
|
|
27
|
|
|
/** |
|
28
|
|
|
* Create AclList with default roles and resources. |
|
29
|
|
|
*/ |
|
30
|
8 |
|
public static function start(): void { |
|
31
|
8 |
|
self::$aclList = new AclList(); |
|
32
|
8 |
|
self::$aclList->init(); |
|
33
|
8 |
|
} |
|
34
|
|
|
|
|
35
|
|
|
/** |
|
36
|
|
|
* Load acls, roles, resources and permissions from providers. |
|
37
|
|
|
* |
|
38
|
|
|
* @param AclProviderInterface[] $providers |
|
39
|
|
|
*/ |
|
40
|
1 |
|
public static function initFromProviders(?array $providers = []): void { |
|
41
|
1 |
|
self::$aclList->setProviders($providers); |
|
42
|
1 |
|
self::$aclList->loadAcls(); |
|
43
|
1 |
|
self::$aclList->loadRoles(); |
|
44
|
1 |
|
self::$aclList->loadResources(); |
|
45
|
1 |
|
self::$aclList->loadPermissions(); |
|
46
|
1 |
|
} |
|
47
|
|
|
|
|
48
|
3 |
|
public static function addRole(string $name, ?array $parents = []) { |
|
49
|
3 |
|
self::$aclList->addRole(new Role($name, $parents)); |
|
50
|
3 |
|
} |
|
51
|
|
|
|
|
52
|
4 |
|
public static function addResource(string $name, ?string $value = null) { |
|
53
|
4 |
|
self::$aclList->addResource(new Resource($name, $value)); |
|
54
|
4 |
|
} |
|
55
|
|
|
|
|
56
|
3 |
|
public static function addPermission(string $name, int $level = 0) { |
|
57
|
3 |
|
self::$aclList->addPermission(new Permission($name, $level)); |
|
58
|
3 |
|
} |
|
59
|
|
|
|
|
60
|
1 |
|
public static function setPermissionLevel(string $name, int $level) { |
|
61
|
1 |
|
self::$aclList->setPermissionLevel($name, $level); |
|
62
|
|
|
} |
|
63
|
|
|
|
|
64
|
|
|
/** |
|
65
|
|
|
* Allow role to access to resource with the permission. |
|
66
|
|
|
* |
|
67
|
|
|
* @param string $role |
|
68
|
|
|
* @param string $resource |
|
69
|
|
|
* @param string $permission |
|
70
|
|
|
*/ |
|
71
|
4 |
|
public static function allow(string $role, ?string $resource = '*', ?string $permission = 'ALL') { |
|
72
|
4 |
|
self::$aclList->allow($role, $resource ?? '*', $permission ?? 'ALL'); |
|
73
|
4 |
|
} |
|
74
|
|
|
|
|
75
|
|
|
/** |
|
76
|
|
|
* Check if access to resource is allowed for role with the permission. |
|
77
|
|
|
* |
|
78
|
|
|
* @param string $role |
|
79
|
|
|
* @param string $resource |
|
80
|
|
|
* @param string $permission |
|
81
|
|
|
* @return bool |
|
82
|
|
|
*/ |
|
83
|
8 |
|
public static function isAllowed(string $role, ?string $resource = '*', ?string $permission = 'ALL'): bool { |
|
84
|
8 |
|
return self::$aclList->isAllowed($role, $resource ?? '*', $permission ?? 'ALL'); |
|
85
|
|
|
} |
|
86
|
|
|
} |
|
87
|
|
|
|
|
88
|
|
|
|