Answer the question
In order to leave comments, you need to log in
How to organize RBAC in Yii2 to control, in addition to accesses, access by language to the content?
The point is this. There is a multilingual site.
It is necessary to implement differentiation of access rights to content for different countries.
For example, when a manager enters Spanish, only Spanish content can be edited, but there must also be the ability to control the languages available to the manager. Those. it is necessary to provide for the possibility for the same manager to add another language or several (if necessary) that he can edit. To the specific user, but not to group.
My vision for the solution is to extend rbac
I create another table rbac_language which contains the list of available languages
and rbac_language_assignment where the particular language is stored.
But here is the question. In this variant, it is very inconvenient to use permission to differentiate rights, since permission can only be tied to the user's group, but not to the user himself.
And here the problem of such an architecture arises - the user has many groups.
On the other hand, would it be better to create a new user group each time for a new set of available languages, or is there a better solution?
I ask in order to get advice or criticism of the design of access control and suggestions for improvement.
Answer the question
In order to leave comments, you need to log in
In this variant, it is very inconvenient to use permission to differentiate rights, since permission can only be tied to the user's group, but not to the user himself.
$authManager = Yii::$app->authManager;
$userID = 1;
$permission = $authManager->getPermission('language_ru');
$authManager->assign($permission, $userID);
Yes, this is true, but there is one "but", I'm trying to automate the rules check, I took the GlobalAccessBehavior
from the wonderful project https://github.com/trntv/yii2-starter-kit , which sets global access rights to actions.
This is what it looks like.
There is a console command that runs through all the admin modules, collects controllers and all actions, also runs through the controllers inside the admin and does the same.
the rules are compiled as follows
backend___
Further, all this is tied to the role.
Everything works fine, but when a situation arises when a specific user (and there may be a dozen of them) needs to disable a specific controller or even an action, then in the case of using permission, AccessControl does not understand it, since access can be set for the role.
In the case of using permission, such a check should be carried out in the controller's beforeAction method
public function beforeAction() {
$permission = 'backend_' . Yii::$app->controller->module->id . '_' . Yii::$app->controller->id . '_' . Yii::$app->controller->action->id;
if (!Yii::$app->user->can($permission)) {
throw new yii\web\ForbiddenHttpException;
}
}
<?php
namespace common\behaviors;
use yii\base\Action;
use yii\filters\AccessRule;
use yii\helpers\Inflector;
/**
* @class AccessRuleCustom
*
* @package common\behaviors
* @author Sergey Doniy <[email protected]>
*/
class AccessRuleCustom extends AccessRule
{
public $checkPermissions = true;
public $prefix = 'backend_';
public function allows($action, $user, $request)
{
return parent::allows($action, $user, $request) || $this->hasPermission($action);
}
protected function hasPermission(Action $action) {
if (!$this->checkPermissions)
return false;
$permission = $this->prefix . $action->controller->module->id . '_' . $action->controller->id . '_' . Inflector::id2camel($action->id);
return \Yii::$app->user->can($permission);
}
}
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question