Passed
Push — main ( 25e437...1b4ef4 )
by Jean-Christophe
02:19
created

AclManager::addResources()   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
17
/**
18
 * Ubiquity\security\acl$AclManager
19
 * This class is part of Ubiquity
20
 *
21
 * @author jc
22
 * @version 1.0.0
23
 *
24
 */
25
class AclManager {
26
27
	/**
28
	 *
29
	 * @var AclList
30
	 */
31
	protected static $aclList;
32
33
	/**
34
	 * Create AclList with default roles and resources.
35
	 */
36 19
	public static function start(): void {
37 19
		self::$aclList = new AclList();
38 19
		self::$aclList->init();
39 19
	}
40
41
	/**
42
	 * Load acls, roles, resources and permissions from providers.
43
	 *
44
	 * @param AclProviderInterface[] $providers
45
	 */
46 8
	public static function initFromProviders(?array $providers = []): void {
47 8
		self::$aclList->setProviders($providers);
48 8
		if (\count($providers) > 0) {
49 7
			self::$aclList->loadAcls();
50 7
			self::$aclList->loadRoles();
51 7
			self::$aclList->loadResources();
52 7
			self::$aclList->loadPermissions();
53
		}
54 8
	}
55
56 6
	public static function addRole(string $name, ?array $parents = []) {
57 6
		self::$aclList->addRole(new Role($name, $parents));
58 6
	}
59
60 1
	public static function addRoles(array $nameParents) {
61 1
		foreach ($nameParents as $name => $parents) {
62 1
			self::$aclList->addRole(new Role($name, $parents));
63
		}
64 1
	}
65
66 6
	public static function addResource(string $name, ?string $value = null) {
67 6
		self::$aclList->addResource(new Resource($name, $value));
68 6
	}
69
70 1
	public static function addResources(array $nameValue) {
71 1
		foreach ($nameValue as $name => $value) {
72 1
			self::$aclList->addResource(new Resource($name, $value));
73
		}
74 1
	}
75
76 7
	public static function addPermission(string $name, int $level = 0) {
77 7
		self::$aclList->addPermission(new Permission($name, $level));
78 7
	}
79
80 1
	public static function addPermissions(array $nameLevel) {
81 1
		foreach ($nameLevel as $name => $level) {
82 1
			self::$aclList->addPermission(new Permission($name, $level));
83
		}
84 1
	}
85
86 3
	public static function setPermissionLevel(string $name, int $level) {
87 3
		self::$aclList->setPermissionLevel($name, $level);
88 2
	}
89
90 6
	public static function getRoles() {
91 6
		return self::$aclList->getRoles();
92
	}
93
94 6
	public static function getResources() {
95 6
		return self::$aclList->getResources();
96
	}
97
98
	/**
99
	 *
100
	 * @return \Ubiquity\security\acl\models\AclList
101
	 */
102
	public static function getAclList() {
103
		return AclManager::$aclList;
104
	}
105
106 9
	public static function getPermissions() {
107 9
		return self::$aclList->getPermissions();
108
	}
109
110 3
	public static function getAcls() {
111 3
		return self::$aclList->getAcls();
112
	}
113
114
	/**
115
	 * Allow role to access to resource with the permission.
116
	 *
117
	 * @param string $role
118
	 * @param string $resource
119
	 * @param string $permission
120
	 */
121 9
	public static function allow(string $role, ?string $resource = '*', ?string $permission = 'ALL') {
122 9
		self::$aclList->allow($role, $resource ?? '*', $permission ?? 'ALL');
123 9
	}
124
125
	/**
126
	 * Add role, resource and permission and allow this role to access to resource with the permission.
127
	 *
128
	 * @param string $role
129
	 * @param string $resource
130
	 * @param string $permission
131
	 */
132 1
	public static function addAndAllow(string $role, ?string $resource = '*', ?string $permission = 'ALL') {
133 1
		self::$aclList->addAndAllow($role, $resource ?? '*', $permission ?? 'ALL');
134 1
	}
135
136
	/**
137
	 * Check if access to resource is allowed for role with the permission.
138
	 *
139
	 * @param string $role
140
	 * @param string $resource
141
	 * @param string $permission
142
	 * @return bool
143
	 */
144 18
	public static function isAllowed(string $role, ?string $resource = '*', ?string $permission = 'ALL'): bool {
145 18
		return self::$aclList->isAllowed($role, $resource ?? '*', $permission ?? 'ALL');
146
	}
147
148 3
	public static function saveAll() {
149 3
		self::$aclList->saveAll();
150 3
	}
151
152 2
	public static function removeRole(string $role) {
153 2
		self::$aclList->removeRole($role);
154 2
	}
155
156 2
	public static function removePermission(string $permission) {
157 2
		self::$aclList->removePermission($permission);
158 2
	}
159
160 1
	public static function removeResource(string $resource) {
161 1
		self::$aclList->removeResource($resource);
162 1
	}
163
164 2
	public static function removeAcl(string $role, string $resource, string $permission = null) {
165 2
		self::$aclList->removeAcl($role, $resource, $permission);
166 2
	}
167
168 1
	public static function initCache(&$config) {
169 1
		CacheManager::start($config);
170 1
		CacheManager::registerAnnotations([
171 1
			'allow' => AllowAnnotation::class,
172
			'resource' => ResourceAnnotation::class,
173
			'permission' => PermissionAnnotation::class
174
		]);
175 1
		$files = \Ubiquity\cache\CacheManager::getControllersFiles($config, true);
176 1
		$parser = new AclControllerParser();
177 1
		$parser->init();
178 1
		foreach ($files as $file) {
179 1
			if (\is_file($file)) {
180 1
				$controller = ClassUtils::getClassFullNameFromFile($file);
181
				try {
182 1
					$parser->parse($controller);
183
				} catch (\Exception $e) {
184
					if ($e instanceof AclException) {
185
						throw $e;
186
					}
187
				}
188
			}
189
		}
190 1
		$parser->save();
191 1
	}
192
}
193
194