Passed
Push — main ( b9df1c...66c5b2 )
by Jean-Christophe
02:06
created

AclListOperationsTrait::removeAclElement()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 3
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 1
Bugs 0 Features 1
Metric Value
cc 2
eloc 2
c 1
b 0
f 1
nc 2
nop 1
dl 0
loc 3
ccs 0
cts 3
cp 0
crap 6
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.0
17
 * @property Role[] $roles
18
 * @property Resource[] $resources
19
 * @property Permission[] $permissons
20
 * @property AclProviderInterface[] $providers
21
 * @property array elementsCache
22
 *
23
 */
24
trait AclListOperationsTrait {
25
26
	abstract public function getRoleByName(string $name);
27
28
	abstract public function getResourceByName(string $name);
29
30
	abstract public function getPermissionByName(string $name);
31
32 13
	public function saveAclElement(AclElement $aclElement) {
33 13
		foreach ($this->providers as $provider) {
34 5
			$provider->saveAcl($aclElement);
35
		}
36 13
	}
37
38
	public function removeAclElement(AclElement $aclElement) {
39
		foreach ($this->providers as $provider) {
40
			$provider->removeAcl($aclElement);
41
		}
42
	}
43
44 15
	public function savePart(AbstractAclPart $aclPart) {
45 15
		foreach ($this->providers as $provider) {
46 7
			$provider->savePart($aclPart);
47
		}
48 15
	}
49
50 2
	public function updatePart(AbstractAclPart $aclPart) {
51 2
		foreach ($this->providers as $provider) {
52 2
			$provider->updatePart($aclPart);
53
		}
54 2
	}
55
56 6
	public function removePart(AbstractAclPart $aclPart) {
57 6
		foreach ($this->providers as $provider) {
58 6
			$provider->removePart($aclPart);
59
		}
60 6
	}
61
62 4
	public function removeRole(string $roleName) {
63 4
		$role = $this->getRoleByName($roleName);
64 4
		unset($this->roles[$roleName]);
65 4
		$this->unsetCache("role_$roleName");
66 4
		$this->removeAcl($roleName);
67 4
		$this->removePart($role);
68 4
	}
69
70 6
	protected function unsetCache($name) {
71 6
		if (isset($this->elementsCache[$name])) {
72 6
			unset($this->elementsCache[$name]);
73
		}
74 6
	}
75
76 4
	public function removePermission(string $permissionName) {
77 4
		$permission = $this->getPermissionByName($permissionName);
78 4
		unset($this->permissions[$permissionName]);
79 4
		$this->unsetCache("perm_$permissionName");
80 4
		$this->removeAcl(null, null, $permissionName);
81 4
		$this->removePart($permission);
82 4
	}
83
84 2
	public function removeResource(string $resourceName) {
85 2
		$resource = $this->getResourceByName($resourceName);
86 2
		unset($this->resources[$resourceName]);
87 2
		$this->unsetCache("res_$resourceName");
88 2
		$this->removeAcl(null, $resourceName);
89 2
		$this->removePart($resource);
90 2
	}
91
92 8
	public function removeAcl(string $roleName = null, string $resourceName = null, string $permissionName = null) {
93 8
		$toRemove = [];
94 8
		foreach ($this->acls as $index => $acl) {
95 8
			if (($resourceName == null || $acl->getResource()->getName() === $resourceName) && ($roleName == null || $acl->getRole()->getName() === $roleName) && ($permissionName == null || $acl->getPermission()->getName() === $permissionName)) {
96 4
				foreach ($this->providers as $provider) {
97 4
					$provider->removeAcl($acl);
98
				}
99 4
				$toRemove[] = $index;
100
			}
101
		}
102 8
		foreach ($toRemove as $remove) {
103 4
			unset($this->acls[$remove]);
104
		}
105 8
	}
106
107 5
	public function saveAll() {
108 5
		foreach ($this->providers as $provider) {
109 5
			if (! $provider->isAutosave()) {
110 4
				$provider->saveAll();
111
			}
112
		}
113 5
	}
114
115 7
	public function clear() {
116 7
		$this->init();
0 ignored issues
show
Bug introduced by
It seems like init() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( Ignorable by Annotation )

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

116
		$this->/** @scrutinizer ignore-call */ 
117
         init();
Loading history...
117 7
	}
118
119 10
	public function addRole(Role $role) {
120 10
		$this->roles[$role->getName()] = $role;
121 10
		$this->savePart($role);
122 10
	}
123
124 10
	public function addResource(Resource $resource) {
125 10
		$this->resources[$resource->getName()] = $resource;
126 10
		$this->savePart($resource);
127 10
	}
128
129 12
	public function addPermission(Permission $permission) {
130 12
		$this->permissions[$permission->getName()] = $permission;
0 ignored issues
show
Bug Best Practice introduced by
The property permissions does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
131 12
		$this->savePart($permission);
132 12
	}
133
134 3
	public function setPermissionLevel(string $name, int $level) {
135 3
		$perm = $this->getPermissionByName($name);
136 2
		$perm->setLevel($level);
137 2
		$this->updatePart($perm);
138 2
	}
139
140 13
	public function allow(string $roleName, string $resourceName, string $permissionName) {
141 13
		$aclElm = new AclElement();
142 13
		$aclElm->allow($this->getRoleByName($roleName), $this->getResourceByName($resourceName), $this->getPermissionByName($permissionName));
143 13
		$this->acls[] = $aclElm;
0 ignored issues
show
Bug Best Practice introduced by
The property acls does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
144 13
		$this->saveAclElement($aclElm);
145 13
	}
146
147 4
	public function addAndAllow(string $roleName, string $resourceName, string $permissionName) {
148 4
		if (! $this->elementExistByName($roleName, $this->roles)) {
0 ignored issues
show
Bug introduced by
It seems like elementExistByName() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( Ignorable by Annotation )

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

148
		if (! $this->/** @scrutinizer ignore-call */ elementExistByName($roleName, $this->roles)) {
Loading history...
149 3
			$this->addRole(new Role($roleName));
150
		}
151 4
		if ($resourceName !== '*' && ! $this->elementExistByName($resourceName, $this->resources)) {
152 3
			$this->addResource(new Resource($resourceName));
153
		}
154 4
		if ($permissionName !== 'ALL' && ! $this->elementExistByName($permissionName, $this->permissions)) {
155 3
			$this->addPermission(new Permission($permissionName));
156
		}
157 4
		$this->allow($roleName, $resourceName ?? '*', $permissionName ?? 'ALL');
158 4
	}
159
}
160
161