You can not select more than 25 topics
			Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
		
		
		
		
		
			
		
			
				
					
					
						
							128 lines
						
					
					
						
							3.8 KiB
						
					
					
				
			
		
		
	
	
							128 lines
						
					
					
						
							3.8 KiB
						
					
					
				| <?php | |
| /** | |
|  * Created by Error202 | |
|  * Date: 15.08.2017 | |
|  */ | |
|  | |
| namespace core\services; | |
|  | |
| use yii\rbac\ManagerInterface; | |
| //use yii\rbac\Permission; | |
| use yii\helpers\Json; | |
| use yii\helpers\ArrayHelper; | |
|  | |
| class PermissionManager | |
| { | |
|     private $manager; | |
|  | |
|     public function __construct(ManagerInterface $manager) | |
|     { | |
|         $this->manager = $manager; | |
|     } | |
|  | |
|     public function create($name, $description = '', $ruleName = null, $data = null) | |
|     { | |
|         $am = $this->manager; | |
|         if ($permission = $am->getPermission($name)) { | |
|             throw new \DomainException('Permission "' . $name . '" is already exist.'); | |
|         } | |
|         $newPermission = $am->createPermission($name); | |
|         $newPermission->description = $description; | |
|         $newPermission->data = $data == null ? null : Json::decode($data); | |
|         $newPermission->ruleName = empty($ruleName) ? null : $ruleName; | |
|         try { | |
|             $am->add($newPermission); | |
|         } | |
|         catch (\ReflectionException $e) | |
|         { | |
|             throw new \DomainException($e->getMessage()); | |
|         } | |
|     } | |
|  | |
|     public function update($name, $newName, $description = '', $ruleName = null, $data = null) | |
|     { | |
|         $am = $this->manager; | |
|         if (!$permission = $am->getPermission($name)) { | |
|             throw new \DomainException('Permission "' . $name . '" does not exist.'); | |
|         } | |
|         $permission->name = $newName; | |
|         $permission->description = $description; | |
|         $permission->ruleName = empty($ruleName) ? null : $ruleName; | |
|         $permission->data = $data == null ? null : Json::decode($data); | |
|         try { | |
|             $am->update($name, $permission); | |
|         } | |
|         catch (\ReflectionException $e) | |
|         { | |
|             throw new \DomainException($e->getMessage()); | |
|         } | |
|     } | |
|  | |
|     public function delete($name) | |
|     { | |
|         $am = $this->manager; | |
|         if (!$permission = $am->getPermission($name)) | |
|         { | |
|             throw new \DomainException('Permission "' . $name . '" does not exist.'); | |
|         } | |
|         $am->remove($permission); | |
|     } | |
|  | |
|     public function assign($roleName, $permissionName) | |
|     { | |
|         $am = $this->manager; | |
|         if (!$role = $am->getRole($roleName)) { | |
|             throw new \DomainException('Role "' . $roleName . '" does not exist.'); | |
|         } | |
|         if (!$permission = $am->getPermission($permissionName)) | |
|         { | |
|             throw new \DomainException('Permission "' . $permissionName . '" does not exist.'); | |
|         } | |
|         $am->addChild($role, $permission); | |
|     } | |
|  | |
|     public function unassign($roleName, $permissionName) | |
|     { | |
|         $am = $this->manager; | |
|         if (!$role = $am->getRole($roleName)) { | |
|             throw new \DomainException('Role "' . $roleName . '" does not exist.'); | |
|         } | |
|         if (!$permission = $am->getPermission($permissionName)) | |
|         { | |
|             throw new \DomainException('Permission "' . $permissionName . '" does not exist.'); | |
|         } | |
|         if (!$am->hasChild($role, $permission)) | |
|         { | |
|             throw new \DomainException('Permission "' . $permissionName . '" does not assigned to "' . $roleName . '".'); | |
|         } | |
|         $am->removeChild($role, $permission); | |
|     } | |
|  | |
|     public function getPermissions() | |
|     { | |
|         $am = $this->manager; | |
|         return $am->getPermissions(); | |
|     } | |
|  | |
|     public function getPermission($name) | |
|     { | |
|         $am = $this->manager; | |
|         if (!$permission = $am->getPermission($name)) { | |
|             throw new \DomainException('Permission "' . $name . '" does not exist.'); | |
|         } | |
|         return $permission; | |
|     } | |
|  | |
|     public function getPermissionsSelectArray() | |
|     { | |
|         $data = ArrayHelper::map($this->getPermissions(), 'name', 'description'); | |
|         return $data; | |
|     } | |
|  | |
|     public function getPermissionsSelectArrayByRole($id) | |
|     { | |
|         $am = $this->manager; | |
|         $data = ArrayHelper::getColumn($am->getPermissionsByRole($id), 'name'); | |
|         return $data; | |
|     } | |
| } |