Passed
Push — main ( 6d76a8...9a92e8 )
by Jean-Christophe
02:58 queued 15s
created

AclListOperationsTrait::addRole()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 1
Bugs 1 Features 0
Metric Value
cc 1
eloc 2
c 1
b 1
f 0
nc 1
nop 1
dl 0
loc 3
ccs 3
cts 3
cp 1
crap 1
rs 10
1
<?php
2
namespace Ubiquity\security\acl\models\traits;
3
4
use Ubiquity\security\acl\models\AclElement;
5
use Ubiquity\security\acl\models\AbstractAclPart;
6
use Ubiquity\security\acl\models\Role;
7
use Ubiquity\security\acl\models\Resource;
8
use Ubiquity\security\acl\models\Permission;
9
use Ubiquity\security\acl\persistence\AclProviderInterface;
10
11
/**
12
 * Ubiquity\security\acl\models\traits$AclListOperations
13
 * This class is part of Ubiquity
14
 *
15
 * @author jc
16
 * @version 1.0.2
17
 * @property Role[] $roles
18
 * @property Resource[] $resources
19
 * @property Permission[] $permissions
20
 * @property AclProviderInterface[] $providers
21
 * @property array $elementsCache
22
 * @property AclElement[] $acls
23
 *
24
 */
25
trait AclListOperationsTrait {
26
27
	abstract public function getRoleByName(string $name);
28
29
	abstract public function getResourceByName(string $name);
30
31
	abstract public function getPermissionByName(string $name);
32
33
	abstract public function init();
34
35
	abstract protected function elementExistByName(string $name, array $inArray): bool;
36
37 13
	public function saveAclElement(AclElement $aclElement) {
38 13
		foreach ($this->providers as $provider) {
39 5
				$provider->saveAcl($aclElement);
40
		}
41
	}
42
43
	public function removeAclElement(AclElement $aclElement) {
44
		foreach ($this->providers as $provider) {
45
			$provider->removeAcl($aclElement);
46
		}
47
	}
48
49 14
	public function savePart(AbstractAclPart $aclPart) {
50 14
		foreach ($this->providers as $provider) {
51 6
			$provider->savePart($aclPart);
52
		}
53
	}
54
55 2
	public function updatePart(string $id,AbstractAclPart $aclPart) {
56 2
		foreach ($this->providers as $provider) {
57 2
			$provider->updatePart($id,$aclPart);
58
		}
59
	}
60
61 4
	public function removePart(AbstractAclPart $aclPart) {
62 4
		foreach ($this->providers as $provider) {
63 4
			$provider->removePart($aclPart);
64
		}
65
	}
66
67 2
	public function removeRole(string $roleName) {
68 2
		$role = $this->getRoleByName($roleName);
69 2
		unset($this->roles[$roleName]);
70 2
		$this->unsetCache($roleName);
71 2
		$this->removeAcl($roleName);
72 2
		$this->removePart($role);
73
	}
74
75 4
	protected function unsetCache($name) {
76 4
		if (isset($this->elementsCache[$name])) {
77 4
			unset($this->elementsCache[$name]);
78
		}
79
	}
80
81 2
	public function removePermission(string $permissionName) {
82 2
		$permission = $this->getPermissionByName($permissionName);
83 2
		unset($this->permissions[$permissionName]);
84 2
		$this->unsetCache($permissionName);
85 2
		$this->removeAcl(null, null, $permissionName);
86 2
		$this->removePart($permission);
87
	}
88
89
	public function removeResource(string $resourceName) {
90
		$resource = $this->getResourceByName($resourceName);
91
		unset($this->resources[$resourceName]);
92
		$this->unsetCache($resourceName);
93
		$this->removeAcl(null, $resourceName);
94
		$this->removePart($resource);
95
	}
96
97 6
	public function removeAcl(string $roleName = null, string $resourceName = null, string $permissionName = null) {
98 6
		$toRemove = [];
99 6
		foreach ($this->acls as $index => $acl) {
100 6
			if (($resourceName == null || $acl->getResource()->getName() === $resourceName) && ($roleName == null || $acl->getRole()->getName() === $roleName) && ($permissionName == null || $acl->getPermission()->getName() === $permissionName)) {
101 2
				foreach ($this->providers as $provider) {
102 2
					$provider->removeAcl($acl);
103
				}
104 2
				$toRemove[] = $index;
105
			}
106
		}
107 6
		foreach ($toRemove as $remove) {
108 2
			unset($this->acls[$remove]);
109
		}
110
	}
111
112 4
	public function saveAll() {
113 4
		foreach ($this->providers as $provider) {
114 4
			if (! $provider->isAutosave()) {
115 3
				$provider->saveAll();
116
			}
117
		}
118
	}
119
120 3
	public function clear() {
121 3
		$this->init();
122
	}
123
124 10
	public function addRole(Role $role) {
125 10
		$this->roles[$role->getName()] = $role;
126 10
		$this->savePart($role);
127
	}
128
129 9
	public function addResource(Resource $resource) {
130 9
		$this->resources[$resource->getName()] = $resource;
131 9
		$this->savePart($resource);
132
	}
133
134 11
	public function addPermission(Permission $permission) {
135 11
		$this->permissions[$permission->getName()] = $permission;
136 11
		$this->savePart($permission);
137
	}
138
139
	public function updateRole(String $roleName,Role $role){
140
		$oldRole = $this->getRoleByName($roleName);
141
		if($oldRole) {
142
			$this->updatePart($roleName,$role);
143
		}
144
	}
145
146
	public function updateResource(String $resourceName,Resource $resource){
147
		$oldResource = $this->getResourceByName($resourceName);
148
		if($oldResource) {
149
			$this->updatePart($resourceName,$resource);
150
		}
151
	}
152
153
	public function updatePermission(String $permissionName,Permission $permission){
154
		$oldPermission = $this->getPermissionByName($permissionName);
155
		if($oldPermission) {
156
			$this->updatePart($permissionName,$permission);
157
		}
158
	}
159
160 3
	public function setPermissionLevel(string $name, int $level) {
161 3
		$perm = $this->getPermissionByName($name);
162 2
		$perm->setLevel($level);
163 2
		$this->updatePart($name,$perm);
164
	}
165
166 13
	public function allow(string $roleName, string $resourceName, string $permissionName, $id=null) {
167 13
		$aclElm = new AclElement();
168 13
		$aclElm->setId($id);
169 13
		$aclElm->allow($this->getRoleByName($roleName), $this->getResourceByName($resourceName), $this->getPermissionByName($permissionName));
170 13
		$this->acls[] = $aclElm;
171 13
		$this->saveAclElement($aclElm);
172
	}
173
174 3
	public function addAndAllow(string $roleName, string $resourceName, string $permissionName, $id=null) {
175 3
		if (! $this->elementExistByName($roleName, $this->roles)) {
176 3
			$this->addRole(new Role($roleName));
177
		}
178 3
		if ($resourceName !== '*' && ! $this->elementExistByName($resourceName, $this->resources)) {
179 3
			$this->addResource(new Resource($resourceName));
180
		}
181 3
		if ($permissionName !== 'ALL' && ! $this->elementExistByName($permissionName, $this->permissions)) {
182 3
			$this->addPermission(new Permission($permissionName));
183
		}
184 3
		$this->allow($roleName, $resourceName ?? '*', $permissionName ?? 'ALL', $id);
185
	}
186
}
187
188