Passed
Push — main ( 2af279...b35506 )
by Jean-Christophe
01:56
created

AclManager::addPermissions()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 3
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 2

Importance

Changes 0
Metric Value
cc 2
eloc 2
nc 2
nop 1
dl 0
loc 3
ccs 3
cts 3
cp 1
crap 2
rs 10
c 0
b 0
f 0
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\persistence\AclProviderInterface;
9
use Ubiquity\cache\ClassUtils;
10
use Ubiquity\security\acl\cache\AclControllerParser;
11
use Ubiquity\exceptions\AclException;
12
use Ubiquity\cache\CacheManager;
13
use Ubiquity\annotations\acl\AllowAnnotation;
14
use Ubiquity\annotations\acl\ResourceAnnotation;
15
use Ubiquity\annotations\acl\PermissionAnnotation;
16
use Ubiquity\security\acl\cache\PermissionsMap;
17
18
/**
19
 * Ubiquity\security\acl$AclManager
20
 * This class is part of Ubiquity
21
 *
22
 * @author jc
23
 * @version 1.0.0
24
 *
25
 */
26
class AclManager {
27
28
	/**
29
	 *
30
	 * @var AclList
31
	 */
32
	protected static $aclList;
33
34
	/**
35
	 *
36
	 * @var PermissionsMap
37
	 */
38
	protected static $permissionMap;
39
40
	/**
41
	 * Create AclList with default roles and resources.
42
	 */
43 20
	public static function start(): void {
44 20
		self::$aclList = new AclList();
45 20
		self::$aclList->init();
46 20
	}
47
48
	/**
49
	 * Load acls, roles, resources and permissions from providers.
50
	 *
51
	 * @param AclProviderInterface[] $providers
52
	 */
53 9
	public static function initFromProviders(?array $providers = []): void {
54 9
		self::$aclList->setProviders($providers);
55 9
		if (\count($providers) > 0) {
56 8
			self::$aclList->loadAcls();
57 8
			self::$aclList->loadRoles();
58 8
			self::$aclList->loadResources();
59 8
			self::$aclList->loadPermissions();
60
		}
61 9
	}
62
63 6
	public static function addRole(string $name, ?array $parents = []) {
64 6
		self::$aclList->addRole(new Role($name, $parents));
65 6
	}
66
67 1
	public static function addRoles(array $nameParents) {
68 1
		foreach ($nameParents as $name => $parents) {
69 1
			self::$aclList->addRole(new Role($name, $parents));
70
		}
71 1
	}
72
73 7
	public static function addResource(string $name, ?string $value = null) {
74 7
		self::$aclList->addResource(new Resource($name, $value));
75 7
	}
76
77 1
	public static function addResources(array $nameValue) {
78 1
		foreach ($nameValue as $name => $value) {
79 1
			self::$aclList->addResource(new Resource($name, $value));
80
		}
81 1
	}
82
83 9
	public static function addPermission(string $name, int $level = 0) {
84 9
		self::$aclList->addPermission(new Permission($name, $level));
85 9
	}
86
87 1
	public static function addPermissions(array $nameLevel) {
88 1
		foreach ($nameLevel as $name => $level) {
89 1
			self::$aclList->addPermission(new Permission($name, $level));
90
		}
91 1
	}
92
93 3
	public static function setPermissionLevel(string $name, int $level) {
94 3
		self::$aclList->setPermissionLevel($name, $level);
95 2
	}
96
97 8
	public static function getRoles() {
98 8
		return self::$aclList->getRoles();
99
	}
100
101 7
	public static function getResources() {
102 7
		return self::$aclList->getResources();
103
	}
104
105
	/**
106
	 *
107
	 * @return \Ubiquity\security\acl\models\AclList
108
	 */
109
	public static function getAclList() {
110
		return AclManager::$aclList;
111
	}
112
113 11
	public static function getPermissions() {
114 11
		return self::$aclList->getPermissions();
115
	}
116
117 5
	public static function getAcls() {
118 5
		return self::$aclList->getAcls();
119
	}
120
121
	/**
122
	 * Allow role to access to resource with the permission.
123
	 *
124
	 * @param string $role
125
	 * @param string $resource
126
	 * @param string $permission
127
	 */
128 9
	public static function allow(string $role, ?string $resource = '*', ?string $permission = 'ALL') {
129 9
		self::$aclList->allow($role, $resource ?? '*', $permission ?? 'ALL');
130 9
	}
131
132
	/**
133
	 * Add role, resource and permission and allow this role to access to resource with the permission.
134
	 *
135
	 * @param string $role
136
	 * @param string $resource
137
	 * @param string $permission
138
	 */
139 3
	public static function addAndAllow(string $role, ?string $resource = '*', ?string $permission = 'ALL') {
140 3
		self::$aclList->addAndAllow($role, $resource ?? '*', $permission ?? 'ALL');
141 3
	}
142
143
	/**
144
	 * Check if access to resource is allowed for role with the permission.
145
	 *
146
	 * @param string $role
147
	 * @param string $resource
148
	 * @param string $permission
149
	 * @return bool
150
	 */
151 20
	public static function isAllowed(string $role, ?string $resource = '*', ?string $permission = 'ALL'): bool {
152 20
		return self::$aclList->isAllowed($role, $resource ?? '*', $permission ?? 'ALL');
153
	}
154
155
	/**
156
	 * Save all acls,roles, resources and permissions for AclProviders with no autoSave.
157
	 */
158 4
	public static function saveAll() {
159 4
		self::$aclList->saveAll();
160 4
	}
161
162
	/**
163
	 *
164
	 * @param string $role
165
	 */
166 4
	public static function removeRole(string $role) {
167 4
		self::$aclList->removeRole($role);
168 4
	}
169
170
	/**
171
	 *
172
	 * @param string $permission
173
	 */
174 4
	public static function removePermission(string $permission) {
175 4
		self::$aclList->removePermission($permission);
176 4
	}
177
178
	/**
179
	 *
180
	 * @param string $resource
181
	 */
182 2
	public static function removeResource(string $resource) {
183 2
		self::$aclList->removeResource($resource);
184 2
	}
185
186
	/**
187
	 *
188
	 * @param string $role
189
	 * @param string $resource
190
	 * @param string $permission
191
	 */
192 4
	public static function removeAcl(string $role, string $resource, string $permission = null) {
193 4
		self::$aclList->removeAcl($role, $resource, $permission);
194 4
	}
195
196
	/**
197
	 * Initialize acls cache with controllers annotations.
198
	 * Do not execute at runtime
199
	 *
200
	 * @param array $config
201
	 * @throws \Ubiquity\exceptions\AclException
202
	 */
203 2
	public static function initCache(&$config) {
204 2
		CacheManager::start($config);
205 2
		CacheManager::registerAnnotations([
206 2
			'allow' => AllowAnnotation::class,
207
			'resource' => ResourceAnnotation::class,
208
			'permission' => PermissionAnnotation::class
209
		]);
210 2
		$files = \Ubiquity\cache\CacheManager::getControllersFiles($config, true);
211 2
		$parser = new AclControllerParser();
212 2
		$parser->init();
213 2
		foreach ($files as $file) {
214 2
			if (\is_file($file)) {
215 2
				$controller = ClassUtils::getClassFullNameFromFile($file);
216
				try {
217 2
					$parser->parse($controller);
218
				} catch (\Exception $e) {
219
					if ($e instanceof AclException) {
220
						throw $e;
221
					}
222
				}
223
			}
224
		}
225 2
		$parser->save();
226 2
	}
227
228
	/**
229
	 *
230
	 * @return \Ubiquity\security\acl\cache\PermissionsMap
231
	 */
232 1
	public static function getPermissionMap() {
233 1
		if (! isset(self::$permissionMap)) {
234 1
			self::$permissionMap = new PermissionsMap();
235 1
			self::$permissionMap->load();
236
		}
237 1
		return self::$permissionMap;
238
	}
239
240
	/**
241
	 *
242
	 * @param string $controller
243
	 * @param string $action
244
	 * @param string $resource
245
	 * @param string $permission
246
	 */
247 1
	public static function associate(string $controller, string $action, string $resource, string $permission = 'ALL') {
248 1
		self::$aclList->getResourceByName($resource);
249 1
		self::$aclList->getPermissionByName($permission);
250 1
		self::$permissionMap->addAction($controller, $action, $resource, $permission);
251 1
	}
252
}
253
254