Passed
Push — main ( da0020...573841 )
by Jean-Christophe
02:26
created

AclManager::addAndAllow()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 9
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 3

Importance

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