Yii2rbac permission control rule tutorial details, yii2rbac_PHP tutorial

Source: Internet
Author: User
The rule tutorial of Yii2rbac permission control is described in detail. Yii2rbac permission control rule tutorial details, yii2rbac in our previous Yii2 set up the background and implement rbac permission control complete instance tutorial, do not know you have ever wondered no problem, yii2 rbac permission control rule tutorial details, yii2rbac

In our previous tutorial on setting up a backend and implementing rbac permission control for Yii2, I don't know if you have ever wondered what the rule table is, why is this table not involved in the whole process?

I believe not to mention that some people will try it, or baidu or google. In the end, there will be little content to explain!

For general permission systems, the previously implemented rbac is generally sufficient, and there is no rule in real time. I believe you can also implement the functions implemented using rule.

Let's take an example on the official website to show you how this mysterious rule works!

View requirements:

We have administrators and common users. for the article system, we allow administrators to perform any operations on the article, but only allow common users to create articles and modify their own articles, allow users to modify their own articles, not all articles that are not allowed to be modified!

Let's look at how yii2 rbac rule is implemented. the focus is to teach you how to use this rule and to solve many popular sections!

Before we add rule, we need to implement the execute method of the yii \ rbac \ Rule class.

<? Phpnamespace backend \ components; use Yii; use yii \ rbac \ Rule; class ArticleRule extends Rule {public $ name = 'article'; public function execute ($ user, $ item, $ params) {// Set it to false first, and then improve return false After logic ;}}

Then, we can go to the background rule list (/admin/rule/index) to add rule. For more information, see the following section.

Note: Many of the above steps will die in adding class names. remember to add the namespace where our ArticleRule file is located!

Let's take a look at step 3, which is also prone to errors! Please pay attention to this tutorial!

In the access permission list (/admin/permission/index), we add the permission. this permission is only modified for the article, and then we assign it to the user's role.

Note that a severe warning is given here. the route controlled by the newly added permission is the update operation of the article (/article/update) assigned to the current user only once, if you repeatedly allocate the current operation to the role or user, the rule may be invalid because the failure is overwritten!

Refresh the update page (/article/update/1) of the article again. it is clear that the 403 forbidden has no access permission, that is, the newly added rule takes effect! If it does not take effect at the moment, check the two notes mentioned above!

Then we implement the business logic in the ArticleRule: execute method, which can be referred to as follows:

Class ArticleRule extends Rule {public $ name = 'article';/*** @ param string | integer $ uid of the user Currently logged on * @ param Item $ rule of the item, that is, the new rule we will follow * @ param array $ params parameters carried by the current request. * @ return true or false. true users can access false users. */public function execute ($ 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 ;}}

Finally, it is verified. Does the rule authentication work?

The test procedure is as follows:

1. the current User creates an article. remember to record the creator of the current article. The role is administrator. our default User is: ROLE_ADMIN.

2. create a common user, create an article, and record the creator of the current article.

3. the two articles are modified by logging on to the system with the administrator account and the common user respectively. The conclusion is that the two articles can be modified by the administrator. the common user can only modify their own articles.

The above is a detailed explanation of the Yii2 rbac permission control rule tutorial. I hope it will help you. if you have any questions, please leave a message and I will reply to you in a timely manner. Thank you very much for your support for the help House website!

Http://www.bkjia.com/PHPjc/1136612.htmlwww.bkjia.comtruehttp://www.bkjia.com/PHPjc/1136612.htmlTechArticleYii2 rbac permission control rule tutorial details, yii2rbac in our previous Yii2 set up the background and implement rbac permission control complete instance tutorial, do not know you have been confused no problem ,...

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.