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; } }