| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  | namespace Ubiquity\security\acl\cache; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  | use Ubiquity\cache\CacheManager; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  | use Ubiquity\security\acl\cache\traits\AclCacheTrait; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  | use Ubiquity\security\acl\AclManager; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  | /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  |  * Ubiquity\security\acl\cache$PermissionsMap | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  |  * This class is part of Ubiquity | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  |  * @author jc | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  |  * @version 1.0.3 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  |  */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  | class PermissionsMap { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  | 	use AclCacheTrait; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  | 	/** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  | 	 * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  | 	 * @var array | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  | 	 */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  | 	private $arrayMap; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  | 	private const CACHE_KEY = 'permissionsMap'; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 | 2 |  | 	public function __construct() { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 | 2 |  | 		$this->arrayMap = []; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 | 2 |  | 		$this->createCache($this->getRootKey(self::CACHE_KEY)); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 |  |  | 	} | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 31 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 32 | 2 |  | 	protected function getKey($controller, $action) { | 
            
                                                                        
                            
            
                                    
            
            
                | 33 | 2 |  | 		return "$controller.$action"; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 |  |  | 	} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 |  |  | 	 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 | 1 |  | 	private function _getRessourcePermission(string $controller, string $action) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 | 1 |  | 		return $this->arrayMap[$this->getKey($controller, $action)] ?? null; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 |  |  | 	} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 | 2 |  | 	public function addAction(string $controller, string $action, ?string $resource = '*', ?string $permission = 'ALL') { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 | 2 |  | 		$this->arrayMap[$this->getKey($controller, $action)] = [ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 | 2 |  | 			'resource' => $resource, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 | 2 |  | 			'permission' => $permission | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 | 2 |  | 		]; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 |  |  | 	} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 |  |  | 	/** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 |  |  | 	 * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 |  |  | 	 * @param string $controller | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 |  |  | 	 * @param string $action | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 |  |  | 	 * @return array|NULL | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 |  |  | 	 */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 | 1 |  | 	public function getRessourcePermission(string $controller, string $action) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 | 1 |  | 		if( ($r=$this->_getRessourcePermission($controller, $action))!==null){ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 | 1 |  | 			return $r; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 |  |  | 		} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 | 1 |  | 		if ($action !== '*') { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 | 1 |  | 			$r=new \ReflectionMethod($controller,$action); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 | 1 |  | 			$class=$r->getDeclaringClass()->getName(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 | 1 |  | 			if($class!==$controller){ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 |  |  | 				return $this->_getRessourcePermission($class, $action); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 |  |  | 			} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 |  |  | 		} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 | 1 |  | 		return $this->_getRessourcePermission($controller, '*'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 |  |  | 	} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 | 2 |  | 	public function save() { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 | 2 |  | 		CacheManager::$cache->store($this->getRootKey(self::CACHE_KEY), $this->arrayMap); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 |  |  | 	} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 |  |  | 	public function init() { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 72 |  |  | 		CacheManager::$cache->store($this->getRootKey(self::CACHE_KEY), $this->arrayMap = []); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 73 |  |  | 	} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 74 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 75 | 1 |  | 	public function load() { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 76 | 1 |  | 		$this->arrayMap = CacheManager::$cache->fetch($this->getRootKey(self::CACHE_KEY)); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 77 |  |  | 	} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 78 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 79 |  |  | 	public function getMap() { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 80 |  |  | 		$result = []; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 81 |  |  | 		foreach ($this->arrayMap as $k => $v) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 82 |  |  | 			$result[] = [ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 83 |  |  | 				'controller.action' => $k | 
            
                                                                                                            
                            
            
                                    
            
            
                | 84 |  |  | 			] + $v; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 85 |  |  | 		} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 86 |  |  | 		return $result; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 87 |  |  | 	} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 88 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 89 |  |  | 	public function getObjectMap() { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 90 |  |  | 		$result = []; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 91 |  |  | 		foreach ($this->arrayMap as $k => $v) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 92 |  |  | 			$resource = $v['resource'] ?? '*'; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 93 |  |  | 			$permission = $v['permission'] ?? 'ALL'; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 94 |  |  | 			$roles = AclManager::getAclList()->getRolesWithPermissionOnResource($resource, $permission); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 95 |  |  | 			$result[] = new PermissionMapObject($k, $resource, $permission, $roles); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 96 |  |  | 		} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 97 |  |  | 		return $result; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 98 |  |  | 	} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 99 |  |  | 	 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 100 |  |  | 	public function cacheUpdated(): bool { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 101 |  |  | 		$old=CacheManager::$cache->fetch($this->getRootKey(self::CACHE_KEY)); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 102 |  |  | 		return $this->arrayMap!=$old; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 103 |  |  | 	} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 104 |  |  | } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 105 |  |  |  | 
            
                                                        
            
                                    
            
            
                | 106 |  |  |  |