Home Backend Development PHP Tutorial Yii2 rbac permission control rule tutorial detailed explanation_php example

Yii2 rbac permission control rule tutorial detailed explanation_php example

Jul 06, 2016 pm 01:32 PM
rbac rbac permission control yii2

In our previous complete tutorial on setting up the backend of Yii2 and implementing rbac permission control, I wonder if you have ever wondered what the rule table does. Why did we not involve this table in the whole process?

Believe me, if I don’t tell you, some people will try Baidu or Google, but in the end they will just draw water from a bamboo basket. There is very little content to explain in this part!

For general permission systems, the rbac we made before is generally sufficient. Even without rules, I believe you can also achieve the functions we use rules to achieve.

We will give a specific operation tutorial using the example from the official website to see what this mysterious rule does!

Depending on demand:

We have administrators and ordinary users. For the article system, we allow administrators to do any operation on articles, but only ordinary users are allowed to create articles and modify articles created by themselves. Note that they are allowed to modify articles created by themselves. Articles are not allowed to be modified, nor are all articles allowed to be modified!

Let’s see how to implement the yii2 rbac rule. The focus is to teach everyone how to use this rule, and also to solve the knots in many people’s minds!

Before we add a rule, we need to implement the execute method of the yiirbacRule class.

<&#63;php
namespace backend\components;
use Yii;
use yii\rbac\Rule;
class ArticleRule extends Rule
{
public $name = 'article';
public function execute($user, $item, $params)
{
// 这里先设置为false,逻辑上后面再完善
return false;
}
}
Copy after login

Then, we can go to the background rule list (/admin/rule/index) to add rules. For specific adding methods, please refer to the screenshot below

Note that many people will fail in adding the class name in the above step. Remember to add the namespace where our ArticleRule file is located!

Let’s look at the third step, which is also where it’s easy to make mistakes! Please pay attention to this tutorial, high energy lies ahead!

We added a new permission to the access permission list (/admin/permission/index). This permission is only for modifying articles, and then we assign it to the user's role

Please note that there is a serious warning here. The route controlled by the newly added permissions here is the update operation of the article (/article/update). It is assigned to the current user only once. The current operation is assigned repeatedly to the role or user to which it belongs. , which may cause the rule to fail, and the reason for the failure is overwriting!

At this moment, refresh the article update page (/article/update/1) again. Obviously, we are directly given a 403 forbidden no access prompt, which means that the rule we just added has taken effect! If it does not take effect at this moment, please check the two points mentioned above!

Then we implement the business logic in the ArticleRule::execute method, please refer to the following:

class ArticleRule extends Rule
{
public $name = 'article';
/**
* @param string|integer $user 当前登录用户的uid
* @param Item $item 所属规则rule,也就是我们后面要进行的新增规则
* @param array $params 当前请求携带的参数. 
* @return true或false.true用户可访问 false用户不可访问
*/
public function execute($user, $item, $params)
{
$id = isset($params['id']) &#63; $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;
}
}
Copy after login

The last step is verification. Has the rule authentication we implemented worked?

The test steps are as follows for reference:

1. The current user creates an article. Remember to record the creator of the current article. His role is the administrator. Our default is User::ROLE_ADMIN

2. Create an ordinary user and create an article. You also need to record the creator of the current article

3. Use the administrator account and the ordinary user to log in to the system to modify these two articles. The conclusion is naturally to meet the needs we mentioned at the beginning. The administrator can modify both articles, and ordinary users can only modify their own articles.

The above is the detailed explanation of the rules tutorial for Yii2 rbac permission control introduced by the editor. I hope it will be helpful to you. If you have any questions, please leave me a message and the editor will reply to you in time. I would also like to thank you all for your support of the Script House website!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
WWE 2K25: How To Unlock Everything In MyRise
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

How to remove jquery in yii2 How to remove jquery in yii2 Feb 17, 2023 am 09:55 AM

How to remove jquery from yii2: 1. Edit the AppAsset.php file and comment out the "yii\web\YiiAsset" value in the variable $depends; 2. Edit the main.php file and add the configuration "'yii" under the field "components" \web\JqueryAsset' => ['js' => [],'sourcePath' => null,]," to remove the jquery script.

Implementing Role-Based Access Control (RBAC): Using PHP and RBAC Implementing Role-Based Access Control (RBAC): Using PHP and RBAC Jun 20, 2023 pm 10:39 PM

With the popularity of Internet applications, we hope to protect data within the application to ensure that sensitive data is not misused or stolen. One of the solutions is to use role-based access control (RBAC). Role-based access control (RBAC) is an access control model based on the relationship between users and roles. The core idea of ​​this model is to link the user's role to the access control operation, rather than linking the access control operation directly to the user. This approach improves the flexibility of access control,

A few selected CTF exercises will help you learn the yii2 framework! A few selected CTF exercises will help you learn the yii2 framework! Feb 23, 2022 am 10:33 AM

This article will introduce you to the yii2 framework, share a few CTF exercises, and use them to learn the yii2 framework. I hope it will be helpful to everyone.

How to manage RBAC permissions in ThinkPHP6? How to manage RBAC permissions in ThinkPHP6? Jun 12, 2023 am 08:10 AM

With the continuous development of the Internet and the widespread use of applications, more and more websites and applications require access control to ensure the security of sensitive information and resources. With the continuous development of the project and the continuous increase of functions, the RBAC permission management system has become a very popular and mature solution. In this article, we will introduce how to use RBAC for permission management in the ThinkPHP6 framework. What is RBAC permission management? RBAC (Role-BasedAccess

How to install Redis extension using YII2 framework How to install Redis extension using YII2 framework May 26, 2023 pm 06:41 PM

1. You need to download the windows version of the master branch of yii2-redis with composer 2. Unzip and copy it to vendor/yiisoft 3. Add 'yiisoft/yii2-redis'=>array('name'=>'yiisoft to extensions.php under yiisoft /yii2-redis','version'=>'2.0.

Master the role in PHP-Based Access Control (RBAC) authentication Master the role in PHP-Based Access Control (RBAC) authentication Aug 07, 2023 pm 03:39 PM

Mastering the Role in PHP - BasedAccessControl (RBAC) Authentication Introduction: Authentication is an essential feature when developing web applications. Role-BasedAccessControl (RBAC) is a commonly used authentication mode that manages access control around roles, making the distribution of permissions more flexible and easier to maintain. This article will introduce how to implement RBAC authentication in PHP and provide relevant code examples. 1. Overview of the role of RBAC

PHP and Yii2 integrate to implement RBAC permission management function PHP and Yii2 integrate to implement RBAC permission management function Jun 25, 2023 pm 09:18 PM

With the rapid development of Internet technology, more and more applications need to deal with permission management issues. RBAC (Role-Based Access Control), as a mature permission management model, is widely used in various applications. In the PHP field, the Yii2 framework provides a complete set of RBAC implementation solutions. This article will introduce the method of integrating PHP and Yii2 to implement RBAC permission management. 1. What is RBAC? RBAC is a commonly used permission management model, that is, role-based access control. at RBAC

Latest best practices for Java JAAS Latest best practices for Java JAAS Feb 23, 2024 pm 10:52 PM

1. JAAS Overview JavaJAAS (JavaAuthenticationandAuthorizationService) is a framework for multi-system single sign-on (SSO) integration, role-based access control (RBAC) and authorization management. JAAS allows applications to protect access to data or resources and define access control mechanisms. 2. The latest best practices of JAAS 1. Use JAAS for authentication JAAS provides two main authentication methods: Token-based authentication: This method uses tokens (for example, username and password) to verify user identity. Certification-based authentication: This method uses certification (for example, a digital certificate) to verify the user's identity. 2

See all articles