Test Failed
Push — main ( 573841...5a0b4f )
by Jean-Christophe
06:17
created

AclManager::addResource()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 2
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 1
CRAP Score 1

Importance

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

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

172
		CacheManager::/** @scrutinizer ignore-call */ 
173
                registerAnnotations([

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...
173
			'allow' => AllowAnnotation::class,
174
			'resource' => ResourceAnnotation::class,
175
			'permission' => PermissionAnnotation::class
176
		]);
177
		$files = \Ubiquity\cache\CacheManager::getControllersFiles($config, true);
178
		$parser = new AclControllerParser();
179
		$parser->init();
180
		foreach ($files as $file) {
181
			if (\is_file($file)) {
182
				$controller = ClassUtils::getClassFullNameFromFile($file);
183
				try {
184
					$parser->parse($controller);
185
				} catch (\Exception $e) {
186
					if ($e instanceof AclException) {
187
						throw $e;
188
					}
189
				}
190
			}
191
		}
192
		$parser->save();
193
	}
194
}
195
196