Ubiquity 2.5.2
php rapid development framework
Loading...
Searching...
No Matches
AclDAOProvider.php
Go to the documentation of this file.
1<?php
3
17
28
29 protected string $aclClass;
30
31 protected string $roleClass;
32
33 protected string $permissionClass;
34
35 protected string $resourceClass;
36
42 public function __construct(array &$config, array $classes = []) {
43 Startup::$config=$config;
44 $this->setClasses($classes);
45 }
46
47 private function setClasses(array $classes): void {
48 $this->aclClass = $classes['acl'] ?? AclElement::class;
49 $this->roleClass = $classes['role'] ?? Role::class;
50 $this->resourceClass = $classes['resource'] ?? Resource::class;
51 $this->permissionClass = $classes['permission'] ?? Permission::class;
52 }
53
58 public function initModelsCache(&$config) {
59 CacheManager::start($config);
60 CacheManager::createOrmModelCache($this->aclClass);
61 CacheManager::createOrmModelCache($this->roleClass);
62 CacheManager::createOrmModelCache($this->resourceClass);
63 CacheManager::createOrmModelCache($this->permissionClass);
64 }
65
71 public function setDbOffset(string $dbOffset = 'default', bool $persist=true): void {
72 DAO::setModelDatabase($this->aclClass, $dbOffset);
73 DAO::setModelDatabase($this->resourceClass, $dbOffset);
74 DAO::setModelDatabase($this->roleClass, $dbOffset);
75 DAO::setModelDatabase($this->permissionClass, $dbOffset);
76 if ($persist) {
78 }
79 }
80
86 public function createModels(string $dbOffset='default', ?array $classes=null): void {
87 $classes??=[
88 'acl'=>'models\\AclElement','role'=>'models\\Role','resource'=>'models\\Resource','permission'=>'models\\Permission'
89 ];
90 $this->setClasses($classes);
91 $this->createModel($classes['acl'] ?? $this->aclClass,AclElement::class, $dbOffset);
92 $this->createModel($classes['role'] ?? $this->roleClass,Role::class, $dbOffset);
93 $this->createModel($classes['resource'] ?? $this->resourceClass,Resource::class, $dbOffset);
94 $this->createModel($classes['permission'] ?? $this->permissionClass,Permission::class, $dbOffset);
95 }
96
97 public function createModel(string $modelName, string $refName, string $dbOffset='default'): void {
98 if ($modelName!==$refName) {
99 $className=ClassUtils::getClassSimpleName($modelName);
100 $ns=ClassUtils::getNamespaceFromCompleteClassname($modelName);
101 $cCreator=new ClassCreator($className, $ns, ' extends \\'.$refName);
102 if ($dbOffset!=='default') {
103 $annot=CacheManager::getAnnotationsEngineInstance()->getAnnotation($cCreator, 'database', ['name'=>$dbOffset]);
104 $cCreator->addClassAttribute($annot);
105 }
106 $cCreator->generate();
107 }
108 }
109
116 public function generateDbTables(string $dbOffset='default',bool $createDb=false):void{
117 $this->setDbOffset($dbOffset);
118 $generator = new DatabaseReversor(new DbGenerator(), $dbOffset);
119 $activeOffsetValue=DAO::getDbOffset(Startup::$config, $dbOffset);
120 if (($dbName=$activeOffsetValue['dbName']??'')!='') {
121 $generator->setModels([$this->aclClass,$this->roleClass,$this->resourceClass,$this->permissionClass]);
122 $generator->createDatabase($dbName, $createDb);
123 $db=DAO::getDatabase($dbOffset);
124 $db->beginTransaction();
125 $db->execute($generator->__toString());
126 $db->commit();
127 } else {
128 throw new AclException('dbName key is not present or his value is empty!');
129 }
130 }
131
137 public function loadAllAcls(): array {
138 $result= DAO::getAll($this->aclClass);
139 foreach ($result as $elm) {
140 $elm->setType(AclDAOProvider::class);
141 }
142 return $result;
143 }
144
150 public function saveAcl(AclElement $aclElement) {
151 $aclElement->_rest=[];
152 if (!$this->existPart($aclElement->getResource())) {
153 $this->savePart($aclElement->getResource());
154 }
155 if (!$this->existPart($aclElement->getPermission())) {
156 $this->savePart($aclElement->getPermission());
157 }
158 if (!$this->existPart($aclElement->getRole())) {
159 $this->savePart($aclElement->getRole());
160 }
161 $object = $this->castElement($aclElement);
162 if ($this->existAcl($aclElement)) {
163 $res=DAO::update($object);
164 } else {
165 $res = DAO::insert($object);
166 }
167 if ($res) {
168 $aclElement->setId($object->getId());
169 }
170 return $res;
171 }
172
178 public function removeAcl(AclElement $aclElement) {
179 return DAO::remove($aclElement);
180 }
181
182 protected function loadElements(string $className): array {
183 $elements = DAO::getAll($className);
184 $result = [];
185 foreach ($elements as $elm) {
186 $elm->setType(AclDAOProvider::class);
187 $result[$elm->getName()] = $elm;
188 }
189 return $result;
190 }
191
192 protected function castElement($part) {
193 $class = $this->getModelClasses()[get_class($part)] ?? get_class($part);
194 return $part->castAs($class);
195 }
196
202 public function loadAllPermissions(): array {
203 return $this->loadElements($this->permissionClass);
204 }
205
211 public function loadAllResources(): array {
212 return $this->loadElements($this->resourceClass);
213 }
214
220 public function loadAllRoles(): array {
221 return $this->loadElements($this->roleClass);
222 }
223
229 public function savePart(\Ubiquity\security\acl\models\AbstractAclPart $part) {
230 $object = $this->castElement($part);
231 if ($this->existPart($part)) {
232 $object->_rest=[];
233 $res = DAO::update($object);
234 } else {
235 $res=DAO::insert($object);
236 }
237 if ($res) {
238 $part->setName($object->getName());
239 }
240 return $res;
241 }
242
248 public function updatePart(string $id, \Ubiquity\security\acl\models\AbstractAclPart $part) {
249 $object=$this->castElement($part);
250 $object->_rest=[];
251 $object->_pkv['___name']=$id;
252 return DAO::update($object);
253 }
254
260 public function removePart(\Ubiquity\security\acl\models\AbstractAclPart $part) {
261 return DAO::remove($this->castElement($part));
262 }
263
264 public function isAutosave(): bool {
265 return true;
266 }
267
268 public function saveAll(): void {}
269
270 public function existPart(AbstractAclPart $part): bool {
271 $elm = $this->castElement($part);
272 return DAO::exists(\get_class($elm), 'name= ?', [
273 $elm->getName()
274 ]);
275 }
276
277 public function existAcl(AclElement $aclElement): bool {
278 $elm = $this->castElement($aclElement);
279 return DAO::exists(\get_class($elm), 'id= ?', [
280 $elm->getId()
281 ]);
282 }
283
284 public function getDetails(): array {
285 return [
286 'user' => $this->roleClass,
287 'archive' => $this->resourceClass,
288 'unlock alternate' => $this->permissionClass,
289 'lock' => $this->aclClass
290 ];
291 }
292
293 public function getModelClassesSwap(): array {
294 $swap = $this->getModelClasses();
295 $classes = \array_values($swap);
296 $result = [];
297 foreach ($classes as $class) {
298 $result[$class] = $swap;
299 }
300 return $result;
301 }
302
303 public function getModelClasses(): array {
304 return [
305 AclElement::class => $this->aclClass,
306 Role::class => $this->roleClass,
307 Resource::class => $this->resourceClass,
308 Permission::class => $this->permissionClass
309 ];
310 }
311
312 public function clearAll(): void {}
313
325 public static function initializeProvider(array $config,string $dbOffset='default',array $classes = []): AclDAOProvider {
326 $dbProvider=new AclDAOProvider($config,$classes);
327 $dbProvider->initModelsCache($config);
328 $dbProvider->setDbOffset($dbOffset);
329 $dbProvider->generateDbTables($dbOffset);
330 return $dbProvider;
331 }
332
333 public function cacheUpdated(): bool {
334 return false;
335 }
336}
Manager for caches (Router, Rest, models).
Manipulates class and namespace names Ubiquity\cache$ClassUtils This class is part of Ubiquity.
static start(array &$config)
Starts the cache in dev mode, for generating the other caches Do not use in production.
static storeModelsDatabases(array $modelsDatabases)
static createOrmModelCache(string $classname)
Starts the framework.
Definition Startup.php:19
Ubiquity\exceptions$AclException This class is part of Ubiquity.
Gateway class between database and object model.
Definition DAO.php:33
static setModelDatabase($model, $database='default')
Defines the database connection to use for $model class.
Definition DAO.php:291
static getModelsDatabase()
Definition DAO.php:386
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
Load and save Acls with a database using DAO.
createModels(string $dbOffset='default', ?array $classes=null)
Generates the models.
__construct(array &$config, array $classes=[])
initModelsCache(&$config)
Initialize the cache for the ACL models.
updatePart(string $id, \Ubiquity\security\acl\models\AbstractAclPart $part)
{}
generateDbTables(string $dbOffset='default', bool $createDb=false)
Generates the tables for ACL model classes.
createModel(string $modelName, string $refName, string $dbOffset='default')
removePart(\Ubiquity\security\acl\models\AbstractAclPart $part)
{}
setDbOffset(string $dbOffset='default', bool $persist=true)
Defines the database offset used for ACL.
savePart(\Ubiquity\security\acl\models\AbstractAclPart $part)
{}
Ubiquity\security\acl\persistence$AclProviderInterface This class is part of Ubiquity.
Class Configuration \config.