Ubiquity 2.5.2
php rapid development framework
Loading...
Searching...
No Matches
AclListOperationsTrait.php
Go to the documentation of this file.
1<?php
3
10
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 public function saveAclElement(AclElement $aclElement) {
38 foreach ($this->providers as $provider) {
39 $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 public function savePart(AbstractAclPart $aclPart) {
50 foreach ($this->providers as $provider) {
51 $provider->savePart($aclPart);
52 }
53 }
54
55 public function updatePart(string $id,AbstractAclPart $aclPart) {
56 foreach ($this->providers as $provider) {
57 $provider->updatePart($id,$aclPart);
58 }
59 }
60
61 public function removePart(AbstractAclPart $aclPart) {
62 foreach ($this->providers as $provider) {
63 $provider->removePart($aclPart);
64 }
65 }
66
67 public function removeRole(string $roleName) {
68 $role = $this->getRoleByName($roleName);
69 unset($this->roles[$roleName]);
70 $this->unsetCache($roleName);
71 $this->removeAcl($roleName);
72 $this->removePart($role);
73 }
74
75 protected function unsetCache($name) {
76 if (isset($this->elementsCache[$name])) {
77 unset($this->elementsCache[$name]);
78 }
79 }
80
81 public function removePermission(string $permissionName) {
82 $permission = $this->getPermissionByName($permissionName);
83 unset($this->permissions[$permissionName]);
84 $this->unsetCache($permissionName);
85 $this->removeAcl(null, null, $permissionName);
86 $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 public function removeAcl(string $roleName = null, string $resourceName = null, string $permissionName = null) {
98 $toRemove = [];
99 foreach ($this->acls as $index => $acl) {
100 if (($resourceName == null || $acl->getResource()->getName() === $resourceName) && ($roleName == null || $acl->getRole()->getName() === $roleName) && ($permissionName == null || $acl->getPermission()->getName() === $permissionName)) {
101 foreach ($this->providers as $provider) {
102 $provider->removeAcl($acl);
103 }
104 $toRemove[] = $index;
105 }
106 }
107 foreach ($toRemove as $remove) {
108 unset($this->acls[$remove]);
109 }
110 }
111
112 public function saveAll() {
113 foreach ($this->providers as $provider) {
114 if (! $provider->isAutosave()) {
115 $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 public function clear() {
131 $this->init();
132 }
133
134 public function addRole(Role $role) {
135 $this->roles[$role->getName()] = $role;
136 $this->savePart($role);
137 }
138
139 public function addResource(Resource $resource) {
140 $this->resources[$resource->getName()] = $resource;
141 $this->savePart($resource);
142 }
143
144 public function addPermission(Permission $permission) {
145 $this->permissions[$permission->getName()] = $permission;
146 $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 public function setPermissionLevel(string $name, int $level) {
171 $perm = $this->getPermissionByName($name);
172 $perm->setLevel($level);
173 $this->updatePart($name, $perm);
174 }
175
176 public function allow(string $roleName, string $resourceName, string $permissionName, $id=null) {
177 $aclElm = new AclElement();
178 $aclElm->setId($id);
179 $aclElm->allow($this->getRoleByName($roleName), $this->getResourceByName($resourceName), $this->getPermissionByName($permissionName));
180 $this->acls[] = $aclElm;
181 $this->saveAclElement($aclElm);
182 }
183
184 public function addAndAllow(string $roleName, string $resourceName, string $permissionName, $id=null) {
185 if (! $this->elementExistByName($roleName, $this->roles)) {
186 $this->addRole(new Role($roleName));
187 }
188 if ($resourceName !== '*' && ! $this->elementExistByName($resourceName, $this->resources)) {
189 $this->addResource(new Resource($resourceName));
190 }
191 if ($permissionName !== 'ALL' && ! $this->elementExistByName($permissionName, $this->permissions)) {
192 $this->addPermission(new Permission($permissionName));
193 }
194 $this->allow($roleName, $resourceName ?? '*', $permissionName ?? 'ALL', $id);
195 }
196
197 public function roleExists(string $roleName): bool {
198 return $this->elementExistByName($roleName, $this->roles);
199 }
200
201 public function resourceExists(string $resourceName): bool {
202 return $this->elementExistByName($resourceName, $this->resources);
203 }
204
205 public function permissionExists(string $permissionName): bool {
206 return $this->elementExistByName($permissionName, $this->permissions);
207 }
208}
209
Ubiquity\security\acl\models$AbastractAclElement This class is part of Ubiquity.
Ubiquity\security\acl\models$AclElement This class is part of Ubiquity.
Ubiquity\security\acl\models$Permission This class is part of Ubiquity.
Ubiquity\security\acl\models$Resource This class is part of Ubiquity.
Definition Resource.php:12
Ubiquity\security\acl\models$Role This class is part of Ubiquity.
Definition Role.php:12
allow(string $roleName, string $resourceName, string $permissionName, $id=null)
removeAcl(string $roleName=null, string $resourceName=null, string $permissionName=null)
updatePermission(String $permissionName, Permission $permission)
addAndAllow(string $roleName, string $resourceName, string $permissionName, $id=null)
Ubiquity\security\acl\persistence$AclProviderInterface This class is part of Ubiquity.