AclListOperationsTrait::saveAll()   A
last analyzed

Complexity

Conditions 3
Paths 3

Size

Total Lines 4
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 3

Importance

Changes 1
Bugs 0 Features 1
Metric Value
cc 3
eloc 3
c 1
b 0
f 1
nc 3
nop 0
dl 0
loc 4
ccs 4
cts 4
cp 1
crap 3
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 17
	public function savePart(AbstractAclPart $aclPart) {
50 17
		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
	public function cacheUpdated(): bool {
121
		foreach ($this->providers as $provider) {
122
			if (! $provider->isAutosave() && $provider->cacheUpdated()) {
123
				return true;
124
			}
125
		}
126
		return false;
127
	}
128
129
130 3
	public function clear() {
131 3
		$this->init();
132
	}
133
134 11
	public function addRole(Role $role) {
135 11
		$this->roles[$role->getName()] = $role;
136 11
		$this->savePart($role);
137
	}
138
139 10
	public function addResource(Resource $resource) {
140 10
		$this->resources[$resource->getName()] = $resource;
141 10
		$this->savePart($resource);
142
	}
143
144 12
	public function addPermission(Permission $permission) {
145 12
		$this->permissions[$permission->getName()] = $permission;
146 12
		$this->savePart($permission);
147
	}
148
149
	public function updateRole(String $roleName, Role $role) {
150
		$oldRole = $this->getRoleByName($roleName);
151
		if ($oldRole) {
152
			$this->updatePart($roleName, $role);
153
		}
154
	}
155
156
	public function updateResource(String $resourceName, Resource $resource) {
157
		$oldResource = $this->getResourceByName($resourceName);
158
		if ($oldResource) {
159
			$this->updatePart($resourceName, $resource);
160
		}
161
	}
162
163
	public function updatePermission(String $permissionName, Permission $permission) {
164
		$oldPermission = $this->getPermissionByName($permissionName);
165
		if ($oldPermission) {
166
			$this->updatePart($permissionName, $permission);
167
		}
168
	}
169
170 3
	public function setPermissionLevel(string $name, int $level) {
171 3
		$perm = $this->getPermissionByName($name);
172 2
		$perm->setLevel($level);
173 2
		$this->updatePart($name, $perm);
174
	}
175
176 13
	public function allow(string $roleName, string $resourceName, string $permissionName, $id=null) {
177 13
		$aclElm = new AclElement();
178 13
		$aclElm->setId($id);
179 13
		$aclElm->allow($this->getRoleByName($roleName), $this->getResourceByName($resourceName), $this->getPermissionByName($permissionName));
180 13
		$this->acls[] = $aclElm;
181 13
		$this->saveAclElement($aclElm);
182
	}
183
184 3
	public function addAndAllow(string $roleName, string $resourceName, string $permissionName, $id=null) {
185 3
		if (! $this->elementExistByName($roleName, $this->roles)) {
186 3
			$this->addRole(new Role($roleName));
187
		}
188 3
		if ($resourceName !== '*' && ! $this->elementExistByName($resourceName, $this->resources)) {
189 3
			$this->addResource(new Resource($resourceName));
190
		}
191 3
		if ($permissionName !== 'ALL' && ! $this->elementExistByName($permissionName, $this->permissions)) {
192 3
			$this->addPermission(new Permission($permissionName));
193
		}
194 3
		$this->allow($roleName, $resourceName ?? '*', $permissionName ?? 'ALL', $id);
195
	}
196
197 1
	public function roleExists(string $roleName): bool {
198 1
		return $this->elementExistByName($roleName, $this->roles);
199
	}
200
201 1
	public function resourceExists(string $resourceName): bool {
202 1
		return $this->elementExistByName($resourceName, $this->resources);
203
	}
204
205 1
	public function permissionExists(string $permissionName): bool {
206 1
		return $this->elementExistByName($permissionName, $this->permissions);
207
	}
208
}
209
210