Ubiquity 2.5.2
php rapid development framework
Loading...
Searching...
No Matches
PermissionsMap.php
Go to the documentation of this file.
1<?php
3
7
17
22 private $arrayMap;
23
24 private const CACHE_KEY = 'permissionsMap';
25
26 public function __construct() {
27 $this->arrayMap = [];
28 $this->createCache($this->getRootKey(self::CACHE_KEY));
29 }
30
31 protected function getKey($controller, $action) {
32 return "$controller.$action";
33 }
34
35 private function _getRessourcePermission(string $controller, string $action) {
36 return $this->arrayMap[$this->getKey($controller, $action)] ?? null;
37 }
38
39 public function addAction(string $controller, string $action, ?string $resource = '*', ?string $permission = 'ALL') {
40 $this->arrayMap[$this->getKey($controller, $action)] = [
41 'resource' => $resource,
42 'permission' => $permission
43 ];
44 }
45
52 public function getRessourcePermission(string $controller, string $action) {
53 if( ($r=$this->_getRessourcePermission($controller, $action))!==null){
54 return $r;
55 }
56 if ($action !== '*') {
57 $r=new \ReflectionMethod($controller,$action);
58 $class=$r->getDeclaringClass()->getName();
59 if($class!==$controller){
60 return $this->_getRessourcePermission($class, $action);
61 }
62 }
63 return $this->_getRessourcePermission($controller, '*');
64 }
65
66 public function save() {
67 CacheManager::$cache->store($this->getRootKey(self::CACHE_KEY), $this->arrayMap);
68 }
69
70 public function init() {
71 CacheManager::$cache->store($this->getRootKey(self::CACHE_KEY), $this->arrayMap = []);
72 }
73
74 public function load() {
75 $this->arrayMap = CacheManager::$cache->fetch($this->getRootKey(self::CACHE_KEY));
76 }
77
78 public function getMap() {
79 $result = [];
80 foreach ($this->arrayMap as $k => $v) {
81 $result[] = [
82 'controller.action' => $k
83 ] + $v;
84 }
85 return $result;
86 }
87
88 public function getObjectMap() {
89 $result = [];
90 foreach ($this->arrayMap as $k => $v) {
91 $resource = $v['resource'] ?? '*';
92 $permission = $v['permission'] ?? 'ALL';
93 $roles = AclManager::getAclList()->getRolesWithPermissionOnResource($resource, $permission);
94 $result[] = new PermissionMapObject($k, $resource, $permission, $roles);
95 }
96 return $result;
97 }
98
99 public function cacheUpdated(): bool {
100 $old=CacheManager::$cache->fetch($this->getRootKey(self::CACHE_KEY));
101 return $this->arrayMap!=$old;
102 }
103}
104
Manager for caches (Router, Rest, models).
Ubiquity\security\acl$AclManager This class is part of Ubiquity.
Ubiquity\security\acl\cache$PermissionMapObject This class is part of Ubiquity.
Ubiquity\security\acl\cache$PermissionsMap This class is part of Ubiquity.
addAction(string $controller, string $action, ?string $resource=' *', ?string $permission='ALL')
_getRessourcePermission(string $controller, string $action)
getRessourcePermission(string $controller, string $action)
Ubiquity\security\acl\cache\traits$AclCacheTrait This class is part of Ubiquity.