Passed
Push — main ( 9a92e8...e44aa5 )
by Jean-Christophe
02:29
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\cache\CacheManager;
5
use Ubiquity\cache\ClassUtils;
6
use Ubiquity\exceptions\AclException;
7
use Ubiquity\security\acl\cache\AclControllerParser;
8
use Ubiquity\security\acl\cache\PermissionsMap;
9
use Ubiquity\security\acl\models\AbstractAclPart;
10
use Ubiquity\security\acl\models\AclElement;
11
use Ubiquity\security\acl\models\AclList;
12
use Ubiquity\security\acl\models\Permission;
13
use Ubiquity\security\acl\models\Resource;
14
use Ubiquity\security\acl\models\Role;
15
use Ubiquity\security\acl\persistence\AclCacheProvider;
16
use Ubiquity\controllers\Router;
17
use Ubiquity\security\acl\persistence\AclDAOProvider;
18
use Ubiquity\security\acl\persistence\AclProviderInterface;
19
use Ubiquity\security\acl\traits\AclManagerInit;
20
21
/**
22
 * Ubiquity\security\acl$AclManager
23
 * This class is part of Ubiquity
24
 *
25
 * @author jc
26
 * @version 1.0.1
27
 *
28
 */
29
class AclManager {
30
31
	use AclManagerInit;
32
33
	protected static ?AclList $aclList=null;
34
35
	protected static PermissionsMap $permissionMap;
36
37
	protected static array $providersPersistence;
38
39
40 8
	public static function addRole(string $name, ?array $parents = []): void {
41 8
		self::$aclList->addRole(new Role($name, $parents));
1 ignored issue
show
Bug introduced by
The method addRole() does not exist on null. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

41
		self::$aclList->/** @scrutinizer ignore-call */ 
42
                  addRole(new Role($name, $parents));

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
42
	}
43
44 1
	public static function addRoles(array $nameParents): void {
45 1
		foreach ($nameParents as $name => $parents) {
46 1
			self::$aclList->addRole(new Role($name, $parents));
47
		}
48
	}
49
50 7
	public static function addResource(string $name, ?string $value = null): void {
51 7
		self::$aclList->addResource(new Resource($name, $value));
52
	}
53
54 1
	public static function addResources(array $nameValue): void {
55 1
		foreach ($nameValue as $name => $value) {
56 1
			self::$aclList->addResource(new Resource($name, $value));
57
		}
58
	}
59
60 9
	public static function addPermission(string $name, int $level = 0): void {
61 9
		self::$aclList->addPermission(new Permission($name, $level));
62
	}
63
64 1
	public static function addPermissions(array $nameLevel): void {
65 1
		foreach ($nameLevel as $name => $level) {
66 1
			self::$aclList->addPermission(new Permission($name, $level));
67
		}
68
	}
69
70 3
	public static function setPermissionLevel(string $name, int $level): void {
71 3
		self::$aclList->setPermissionLevel($name, $level);
72
	}
73
74 8
	public static function getRoles(): array {
75 8
		return self::$aclList->getRoles();
76
	}
77
78 7
	public static function getResources(): array {
79 7
		return self::$aclList->getResources();
80
	}
81
82
	/**
83
	 *
84
	 * @return \Ubiquity\security\acl\models\AclList
85
	 */
86 3
	public static function getAclList(): ?AclList {
87 3
		return AclManager::$aclList;
88
	}
89
90 11
	public static function getPermissions():array {
91 11
		return self::$aclList->getPermissions();
92
	}
93
94 6
	public static function getAcls() {
95 6
		return self::$aclList->getAcls();
96
	}
97
98
	/**
99
	 * Allow role to access to resource with the permission.
100
	 *
101
	 * @param string $role
102
	 * @param ?string $resource
103
	 * @param ?string $permission
104
	 */
105 10
	public static function allow(string $role, ?string $resource = '*', ?string $permission = 'ALL'): void {
106 10
		self::$aclList->allow($role, $resource ?? '*', $permission ?? 'ALL');
107
	}
108
109
	/**
110
	 * Add role, resource and permission and allow this role to access to resource with the permission.
111
	 *
112
	 * @param string $role
113
	 * @param ?string $resource
114
	 * @param ?string $permission
115
	 */
116 3
	public static function addAndAllow(string $role, ?string $resource = '*', ?string $permission = 'ALL'): void {
117 3
		self::$aclList->addAndAllow($role, $resource ?? '*', $permission ?? 'ALL');
118
	}
119
120
	/**
121
	 * Check if access to resource is allowed for role with the permission.
122
	 *
123
	 * @param string $role
124
	 * @param ?string $resource
125
	 * @param ?string $permission
126
	 * @return bool
127
	 */
128 20
	public static function isAllowed(string $role, ?string $resource = '*', ?string $permission = 'ALL'): bool {
129 20
		return self::$aclList->isAllowed($role, $resource ?? '*', $permission ?? 'ALL');
130
	}
131
132
	public static function isAllowedRoute(string $role,string $routeName): bool {
133
		$routeInfo=Router::getRouteInfoByName($routeName);
134
		if (!isset ( $routeInfo ['controller'] )) {
135
			$routeInfo=\current($routeInfo);
136
		}
137
		$controller=$routeInfo['controller']??null;
138
		$action=$routeInfo['action']??null;
139
		if(isset($controller) && isset($action)){
140
			$resourceController = self::getPermissionMap ()->getRessourcePermission ( $controller, $action );
141
			if (isset ( $resourceController )) {
142
				try{
143
					if (self::isAllowed ( $role, $resourceController ['resource'], $resourceController ['permission'] )) {
144
						return true;
145
					}
146
				}
147
				catch(AclException $e){
148
					//Nothing to do
149
				}
150
			}
151
			return false;
152
		}
153
		return false;
154
	}
155
156
	/**
157
	 * Save all acls,roles, resources and permissions for AclProviders with no autoSave.
158
	 */
159 4
	public static function saveAll(): void {
160 4
		self::$aclList->saveAll();
161
	}
162
163
	/**
164
	 *
165
	 * @param string $role
166
	 */
167 2
	public static function removeRole(string $role): void {
168 2
		self::$aclList->removeRole($role);
169
	}
170
171
	/**
172
	 *
173
	 * @param string $permission
174
	 */
175 2
	public static function removePermission(string $permission): void {
176 2
		self::$aclList->removePermission($permission);
177
	}
178
179
	/**
180
	 *
181
	 * @param string $resource
182
	 */
183
	public static function removeResource(string $resource): void {
184
		self::$aclList->removeResource($resource);
185
	}
186
187
	/**
188
	 *
189
	 * @param string $role
190
	 * @param string $resource
191
	 * @param ?string $permission
192
	 */
193 2
	public static function removeAcl(string $role, string $resource, ?string $permission = null): void {
194 2
		self::$aclList->removeAcl($role, $resource, $permission);
195
	}
196
197
	/**
198
	 *
199
	 * @return \Ubiquity\security\acl\cache\PermissionsMap
200
	 */
201 1
	public static function getPermissionMap():PermissionsMap {
202 1
		if (! isset(self::$permissionMap)) {
203 1
			self::$permissionMap = new PermissionsMap();
204 1
			self::$permissionMap->load();
205
		}
206 1
		return self::$permissionMap;
207
	}
208
209
	/**
210
	 *
211
	 * @param string $controller
212
	 * @param string $action
213
	 * @param string $resource
214
	 * @param string $permission
215
	 */
216 1
	public static function associate(string $controller, string $action, string $resource, string $permission = 'ALL'):void {
217 1
		self::$aclList->getResourceByName($resource);
218 1
		self::$aclList->getPermissionByName($permission);
219 1
		self::$permissionMap->addAction($controller, $action, $resource, $permission);
220
	}
221
222
	/**
223
	 *
224
	 * @param AbstractAclPart $part
225
	 * @param string $providerClass
226
	 * @return boolean
227
	 */
228 2
	public static function existPartIn(AbstractAclPart $part, string $providerClass):bool {
229 2
		return self::$aclList->existPartIn($part, $providerClass);
230
	}
231
232
	/**
233
	 *
234
	 * @param AclElement $elm
235
	 * @param string $providerClass
236
	 * @return boolean
237
	 */
238 2
	public static function existAclIn(AclElement $elm, string $providerClass):bool {
239 2
		return self::$aclList->existAclIn($elm, $providerClass);
240
	}
241
}
242