Как реализовать запрет на редактирование/удаление самого себя
Сделал вот так:
Код: Выделить всё
class notMyselfRule extends Rule
{
public $name = 'isNotMyself';
/**
* @param string|int $manager the user ID.
* @param Item $item the role or permission that this rule is associated width.
* @param array $params parameters passed to ManagerInterface::checkAccess().
* @return bool a value indicating whether the rule permits the role or permission it is associated with.
*/
public function execute($manager, $item, $params)
{
return isset($params['manager']) ? $params['manager']->id != $manager : true;
}
}
Код: Выделить всё
//Правило соответствия текущего менеджера переданному (вернёт false, если совпадают)
$notMyselfRule = new notMyselfRule();
$auth->add($notMyselfRule);
//Редактирование НЕ самого себя (в общей панели менеджеров)
$updateNotMyself = $auth->createPermission('updateNotMyself');
$updateNotMyself->description = 'Редактирование НЕ самого себя';
$updateNotMyself->ruleName = $notMyselfRule->name;
$auth->add($updateNotMyself);
$auth->addChild($updateNotMyself, $updateManager); // updateNotMyself будет использоваться из updateManager
//Удаление НЕ самого себя (в общей панели менеджеров)
$delNotMyself = $auth->createPermission('delNotMyself');
$delNotMyself->description = 'Удаление НЕ самого себя';
$delNotMyself->ruleName = $notMyselfRule->name;
$auth->add($delNotMyself);
$auth->addChild($delNotMyself, $delManager); // delNotMyself будет использоваться из delManager
Добавил разрешения для роли
Код: Выделить всё
$auth->addChild($super, $updateNotMyself);
$auth->addChild($super, $delNotMyself);
Код: Выделить всё
Yii::$app->user->can('updateManager', ['manager' => $model])