YII2 Authority control RBAC rule detailed explanation, YII2 permission control RBAC
Before we yii2 build the background and RBAC in the detailed tutorial, do not know you once wondered if there is not a problem, the rule table is what to do, why in the whole process we did not involve this table?
Believe me not to say, some people will also try, or Baidu or Google, in the end will also bamboo basket, this part of the content of the explanation is less ah!
For the general permission system, the RBAC we have done in the past is generally sufficient, without the rule at once, and I believe you can also implement the functions we have implemented with rule.
We will take the official website example to give a specific operation of the tutorial, to see what this mysterious rule is exactly what to do!
See Requirements:
We have administrators and ordinary users, for the article system, we allow the administrator to any action on the article, but only allow ordinary users to create articles and modify their own created articles, note oh, is allowed to modify their own creation of the article, not not allowed to modify the article, not to modify all articles!
See yii2 RBAC rule How to achieve, the focus is to teach you how to use this rule, but also to unlock the hearts of many people in the festival!
Before we add rule, we need to implement the Execute method of the Yii\rbac\rule class first.
Phpnamespace backend\components; Use Yii; Use Yii\rbac\rule; class extends rule{ public$name = ' article '; Public function Execute ($user$item$params) { // This is set to false, which is logically followed by the returnfalse; }}
Then we can go to the background rule list (/admin/rule/index) to add rule. For specific additions, refer to below
Note that many people in this step will die in the class name additions, remember to add our Articlerule file where the namespace!
We look at the third step, this step is also a very error-prone place! This tutorial please pay attention to focus, high energy ahead!
Our Access rights list (/admin/permission/index) has new permissions, which are only for the modification of the article, and then we assign it to the role that the user belongs to
It is important to note here that the newly added permissions control the route that is the update operation of the article (/article/update) is assigned to the current user only once, repeatedly assigning the current operation to the owning role or user, may cause rule invalidation, failure reason is overwrite!
Now again refresh the article update page (/ARTICLE/UPDATE/1), it is obvious directly to US 403 Forbidden No access to the prompt, that is, we just added the rule to take effect! If this is not the case, please check the two points above!
Then we implement the Articlerule::execute method within the business logic, can refer to the following:
classArticleruleextendsrule{ Public $name= ' article '; /** * @param string|integer $user The UID of the currently logged on user * @param Item $item rule, which is the new rule we want to make * @param array $pa Rams the parameters carried by the current request. * @return True or False.true user can access false user is not accessible*/ Public functionExecute$user,$item,$params) { $id=isset($params[' ID ']) ?$params[' ID ']:NULL; if(!$id) { return false; } $model= Article::findone ($id); if(!$model) { return false; } $username= Yii::$app->user->identity->username; $role= Yii::$app->user->identity->role; if($role= = User::role_admin | |$username==$model-operate) { return true; } return false; }}
The last is to verify that the rule certification we've implemented has worked?
The test steps can be consulted as follows:
[Considering the current domestic web site most of the collection of articles very frequently, not to mention the original source, the original author would like to see the original text, in case there are any problems can not update all articles, to avoid misleading! ]
View Original
http://www.bkjia.com/PHPjc/1138631.html www.bkjia.com true http://www.bkjia.com/PHPjc/1138631.html techarticle yii2 Rights Control RBAC rule detailed explanation, YII2 permission control RBAC before we yii2 build background and RBAC detailed tutorial, do not know that you have ever wondered if there is not a problem, the rule table is to do ...