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
|
|
|
use Ubiquity\security\acl\models\AbstractAclPart; |
11
|
|
|
|
12
|
|
|
/** |
13
|
|
|
* Ubiquity\security\acl$AclManager |
14
|
|
|
* This class is part of Ubiquity |
15
|
|
|
* |
16
|
|
|
* @author jc |
17
|
|
|
* @version 1.0.0 |
18
|
|
|
* |
19
|
|
|
*/ |
20
|
|
|
class AclManager { |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* |
24
|
|
|
* @var AclList |
25
|
|
|
*/ |
26
|
|
|
protected static $aclList; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* Create AclList with default roles and resources. |
30
|
|
|
*/ |
31
|
14 |
|
public static function start(): void { |
32
|
14 |
|
self::$aclList = new AclList(); |
33
|
14 |
|
self::$aclList->init(); |
34
|
14 |
|
} |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* Load acls, roles, resources and permissions from providers. |
38
|
|
|
* |
39
|
|
|
* @param AclProviderInterface[] $providers |
40
|
|
|
*/ |
41
|
7 |
|
public static function initFromProviders(?array $providers = []): void { |
42
|
7 |
|
self::$aclList->setProviders($providers); |
43
|
7 |
|
if (\count($providers) > 0) { |
44
|
6 |
|
self::$aclList->loadAcls(); |
45
|
6 |
|
self::$aclList->loadRoles(); |
46
|
6 |
|
self::$aclList->loadResources(); |
47
|
6 |
|
self::$aclList->loadPermissions(); |
48
|
|
|
} |
49
|
7 |
|
} |
50
|
|
|
|
51
|
5 |
|
public static function addRole(string $name, ?array $parents = []) { |
52
|
5 |
|
self::$aclList->addRole(new Role($name, $parents)); |
53
|
5 |
|
} |
54
|
|
|
|
55
|
4 |
|
public static function addRoles(array $nameParents) { |
56
|
4 |
|
foreach ($nameParents as $name => $parents) { |
57
|
4 |
|
self::$aclList->addRole(new Role($name, $parents)); |
58
|
|
|
} |
59
|
5 |
|
} |
60
|
5 |
|
|
61
|
5 |
|
public static function addResource(string $name, ?string $value = null) { |
62
|
|
|
self::$aclList->addResource(new Resource($name, $value)); |
63
|
3 |
|
} |
64
|
3 |
|
|
65
|
2 |
|
public static function addResources(array $nameValue) { |
66
|
|
|
foreach ($nameValue as $name => $value) { |
67
|
3 |
|
self::$aclList->addResource(new Resource($name, $value)); |
68
|
3 |
|
} |
69
|
|
|
} |
70
|
|
|
|
71
|
3 |
|
public static function addPermission(string $name, int $level = 0) { |
72
|
3 |
|
self::$aclList->addPermission(new Permission($name, $level)); |
73
|
|
|
} |
74
|
|
|
|
75
|
5 |
|
public static function addPermissions(array $nameLevel) { |
76
|
5 |
|
foreach ($nameLevel as $name => $level) { |
77
|
|
|
self::$aclList->addPermission(new Permission($name, $level)); |
78
|
|
|
} |
79
|
3 |
|
} |
80
|
3 |
|
|
81
|
|
|
public static function setPermissionLevel(string $name, int $level) { |
82
|
|
|
self::$aclList->setPermissionLevel($name, $level); |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
public static function getRoles() { |
86
|
|
|
return self::$aclList->getRoles(); |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
public static function getResources() { |
90
|
6 |
|
return self::$aclList->getResources(); |
91
|
6 |
|
} |
92
|
6 |
|
|
93
|
|
|
public static function getPermissions() { |
94
|
|
|
return self::$aclList->getPermissions(); |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
public static function getAcls() { |
98
|
|
|
return self::$aclList->getAcls(); |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
/** |
102
|
14 |
|
* Allow role to access to resource with the permission. |
103
|
14 |
|
* |
104
|
|
|
* @param string $role |
105
|
|
|
* @param string $resource |
106
|
2 |
|
* @param string $permission |
107
|
2 |
|
*/ |
108
|
2 |
|
public static function allow(string $role, ?string $resource = '*', ?string $permission = 'ALL') { |
109
|
|
|
self::$aclList->allow($role, $resource ?? '*', $permission ?? 'ALL'); |
110
|
2 |
|
} |
111
|
2 |
|
|
112
|
2 |
|
/** |
113
|
|
|
* Add role, resource and permission and allow this role to access to resource with the permission. |
114
|
2 |
|
* |
115
|
2 |
|
* @param string $role |
116
|
2 |
|
* @param string $resource |
117
|
|
|
* @param string $permission |
118
|
|
|
*/ |
119
|
|
|
public static function addAndAllow(string $role, ?string $resource = '*', ?string $permission = 'ALL') { |
120
|
|
|
self::$aclList->addRole(new Role($name, [])); |
|
|
|
|
121
|
|
|
if ($resource !== '*') { |
122
|
2 |
|
self::$aclList->addResource($resource); |
|
|
|
|
123
|
2 |
|
} |
124
|
2 |
|
if ($permission !== 'ALL') { |
125
|
|
|
self::$aclList->addPermission($permission); |
|
|
|
|
126
|
|
|
} |
127
|
|
|
self::$aclList->allow($role, $resource ?? '*', $permission ?? 'ALL'); |
128
|
|
|
} |
129
|
|
|
|
130
|
|
|
/** |
131
|
|
|
* Check if access to resource is allowed for role with the permission. |
132
|
|
|
* |
133
|
|
|
* @param string $role |
134
|
|
|
* @param string $resource |
135
|
|
|
* @param string $permission |
136
|
|
|
* @return bool |
137
|
|
|
*/ |
138
|
|
|
public static function isAllowed(string $role, ?string $resource = '*', ?string $permission = 'ALL'): bool { |
139
|
|
|
return self::$aclList->isAllowed($role, $resource ?? '*', $permission ?? 'ALL'); |
140
|
|
|
} |
141
|
|
|
|
142
|
|
|
public static function saveAll() { |
143
|
|
|
self::$aclList->saveAll(); |
144
|
|
|
} |
145
|
|
|
|
146
|
|
|
public static function removeRole(string $role) { |
147
|
|
|
self::$aclList->removeRole($role); |
148
|
|
|
} |
149
|
|
|
|
150
|
|
|
public static function removePermission(string $permission) { |
151
|
|
|
self::$aclList->removePermission($permission); |
152
|
|
|
} |
153
|
|
|
|
154
|
|
|
public static function removeResource(string $resource) { |
155
|
|
|
self::$aclList->removeResource($resource); |
156
|
|
|
} |
157
|
|
|
|
158
|
|
|
public static function removeAcl(string $role, string $resource, string $permission = null) { |
159
|
|
|
self::$aclList->removeAcl($role, $resource, $permission); |
160
|
|
|
} |
161
|
|
|
} |
162
|
|
|
|
163
|
|
|
|