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.

149 lines
4.3 KiB

7 years ago
<?php
/**
* Created by Error202
* Date: 15.08.2017
*/
namespace core\services;
3 years ago
use DomainException;
use Exception;
use ReflectionException;
7 years ago
use yii\rbac\ManagerInterface;
use yii\helpers\Json;
use yii\helpers\ArrayHelper;
3 years ago
use yii\rbac\Permission;
7 years ago
class PermissionManager
{
3 years ago
private ManagerInterface $manager;
7 years ago
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)) {
3 years ago
throw new DomainException('Permission "' . $name . '" is already exist.');
7 years ago
}
$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);
}
3 years ago
catch (ReflectionException $e)
7 years ago
{
3 years ago
throw new DomainException($e->getMessage());
7 years ago
}
}
3 years ago
/**
* @param $name
* @param $newName
* @param string $description
* @param null $ruleName
* @param null $data
* @throws Exception
*/
7 years ago
public function update($name, $newName, $description = '', $ruleName = null, $data = null)
{
$am = $this->manager;
if (!$permission = $am->getPermission($name)) {
3 years ago
throw new DomainException('Permission "' . $name . '" does not exist.');
7 years ago
}
$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);
}
3 years ago
catch (ReflectionException $e)
7 years ago
{
3 years ago
throw new DomainException($e->getMessage());
7 years ago
}
}
public function delete($name)
{
$am = $this->manager;
if (!$permission = $am->getPermission($name))
{
3 years ago
throw new DomainException('Permission "' . $name . '" does not exist.');
7 years ago
}
$am->remove($permission);
}
3 years ago
/**
* @param $roleName
* @param $permissionName
* @throws \yii\base\Exception
*/
7 years ago
public function assign($roleName, $permissionName)
{
$am = $this->manager;
if (!$role = $am->getRole($roleName)) {
3 years ago
throw new DomainException('Role "' . $roleName . '" does not exist.');
7 years ago
}
3 years ago
if (!$permission = $am->getPermission($permissionName)) {
throw new DomainException('Permission "' . $permissionName . '" does not exist.');
7 years ago
}
$am->addChild($role, $permission);
}
public function unassign($roleName, $permissionName)
{
$am = $this->manager;
if (!$role = $am->getRole($roleName)) {
3 years ago
throw new DomainException('Role "' . $roleName . '" does not exist.');
7 years ago
}
3 years ago
if (!$permission = $am->getPermission($permissionName)) {
throw new DomainException('Permission "' . $permissionName . '" does not exist.');
7 years ago
}
3 years ago
if (!$am->hasChild($role, $permission)) {
throw new DomainException('Permission "' . $permissionName . '" does not assigned to "' . $roleName . '".');
7 years ago
}
$am->removeChild($role, $permission);
}
3 years ago
public function getPermissions(): array
7 years ago
{
$am = $this->manager;
return $am->getPermissions();
}
3 years ago
public function getPermission($name): ?Permission
7 years ago
{
$am = $this->manager;
if (!$permission = $am->getPermission($name)) {
3 years ago
throw new DomainException('Permission "' . $name . '" does not exist.');
7 years ago
}
return $permission;
}
public function permissionExists($name): bool
{
$am = $this->manager;
if (!$permission = $am->getPermission($name)) {
return false;
}
return true;
}
3 years ago
public function getPermissionsSelectArray(): array
7 years ago
{
3 years ago
return ArrayHelper::map($this->getPermissions(), 'name', 'description');
7 years ago
}
3 years ago
public function getPermissionsSelectArrayByRole($id): array
7 years ago
{
$am = $this->manager;
3 years ago
return ArrayHelper::getColumn($am->getPermissionsByRole($id), 'name');
7 years ago
}
3 years ago
}