Passed
Push — main ( 441a12...f99148 )
by Jean-Christophe
02:19
created

AclManager::removeAcl()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 2
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 1
c 0
b 0
f 0
nc 1
nop 3
dl 0
loc 2
ccs 2
cts 2
cp 1
crap 1
rs 10
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 19
	public static function start(): void {
44 19
		self::$aclList = new AclList();
45 19
		self::$aclList->init();
46 19
	}
47
48
	/**
49
	 * Load acls, roles, resources and permissions from providers.
50
	 *
51
	 * @param AclProviderInterface[] $providers
52
	 */
53 8
	public static function initFromProviders(?array $providers = []): void {
54 8
		self::$aclList->setProviders($providers);
55 8
		if (\count($providers) > 0) {
56 7
			self::$aclList->loadAcls();
57 7
			self::$aclList->loadRoles();
58 7
			self::$aclList->loadResources();
59 7
			self::$aclList->loadPermissions();
60
		}
61 8
	}
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 6
	public static function addResource(string $name, ?string $value = null) {
74 6
		self::$aclList->addResource(new Resource($name, $value));
75 6
	}
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 8
	public static function addPermission(string $name, int $level = 0) {
84 8
		self::$aclList->addPermission(new Permission($name, $level));
85 8
	}
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 7
	public static function getRoles() {
98 7
		return self::$aclList->getRoles();
99
	}
100
101 6
	public static function getResources() {
102 6
		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 10
	public static function getPermissions() {
114 10
		return self::$aclList->getPermissions();
115
	}
116
117 4
	public static function getAcls() {
118 4
		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 2
	public static function addAndAllow(string $role, ?string $resource = '*', ?string $permission = 'ALL') {
140 2
		self::$aclList->addAndAllow($role, $resource ?? '*', $permission ?? 'ALL');
141 2
	}
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 19
	public static function isAllowed(string $role, ?string $resource = '*', ?string $permission = 'ALL'): bool {
152 19
		return self::$aclList->isAllowed($role, $resource ?? '*', $permission ?? 'ALL');
153
	}
154
155 3
	public static function saveAll() {
156 3
		self::$aclList->saveAll();
157 3
	}
158
159 3
	public static function removeRole(string $role) {
160 3
		self::$aclList->removeRole($role);
161 3
	}
162
163 3
	public static function removePermission(string $permission) {
164 3
		self::$aclList->removePermission($permission);
165 3
	}
166
167 1
	public static function removeResource(string $resource) {
168 1
		self::$aclList->removeResource($resource);
169 1
	}
170
171 3
	public static function removeAcl(string $role, string $resource, string $permission = null) {
172 3
		self::$aclList->removeAcl($role, $resource, $permission);
173 3
	}
174
175 1
	public static function initCache(&$config) {
176 1
		CacheManager::start($config);
177 1
		CacheManager::registerAnnotations([
178 1
			'allow' => AllowAnnotation::class,
179
			'resource' => ResourceAnnotation::class,
180
			'permission' => PermissionAnnotation::class
181
		]);
182 1
		$files = \Ubiquity\cache\CacheManager::getControllersFiles($config, true);
183 1
		$parser = new AclControllerParser();
184 1
		$parser->init();
185 1
		foreach ($files as $file) {
186 1
			if (\is_file($file)) {
187 1
				$controller = ClassUtils::getClassFullNameFromFile($file);
188
				try {
189 1
					$parser->parse($controller);
190
				} catch (\Exception $e) {
191
					if ($e instanceof AclException) {
192
						throw $e;
193
					}
194
				}
195
			}
196
		}
197 1
		$parser->save();
198 1
	}
199
200
	public static function getPermissionMap() {
201
		if (! isset(self::$permissionMap)) {
202
			self::$permissionMap = new PermissionsMap();
203
			self::$permissionMap->load();
204
		}
205
		return self::$permissionMap;
206
	}
207
}
208
209