Brief Introduction to the full guide to using RBAC in Yii (User Role permission Control) (2024)

Developer on Alibaba Coud: Build your first app with APIs, SDKs, and tutorials on the Alibaba Cloud. Read more >

Preface
* My feed address has been changed to: http://feeds.imdong.net, please update your reader.
* The following content is suitable for Yii 1.0.x. Other versions may be slightly different.
* Based on your comments and feedback, this article will be continuously modified and supplemented to facilitate new learners.

Start preparation
Yii provides powerful configuration mechanisms and many ready-made class libraries. It is very easy to use RBAC in Yii, and no RBAC code needs to be written at all. So the preparation is to open the editor and come with me.
Set parameters and create a database
Add the following content to the configuration array:

Copy code

The Code is as follows: 'components' => array (
//......
'Authmanager' => array (
'Class' => 'cdbauthmanager', // authentication class Name
'Defaultrole' => array ('guest '), // default role
'Itemtable' => 'pre _ auth_item ', // The Name Of The authentication item table.
'Itemchildtable' => 'pre _ auth_item_child ', // authentication item parent-child relationship
'Assignmenttable' => 'pre _ auth_assignment ', // authorization relationship of authentication items
),
//......

How can we create these three data tables? It's easy to look at framework/web/auth/schema. SQL. Be sure to match your custom table name. For example, in the SQL file, you must change AuthItem to pre_auth_item. Then, run the statements in the SQL file in the database.

Understanding concepts
What about the remaining code? I tell you, no. The RBAC system is established in this way. But to use it, you need to understand its operating mechanism. I will try to speak a little too long ...... (The Official RBAC document is here, but I have read it 4-5 times before I understand it .)

Three Concepts
You need to know that authorized projects can be divided into operations, tasks, and roles ).
A user has one or more roles. For example, we have three roles: bank presidents, bank employees, and customers. Let's assume that:
* President Zhang has a role: the Bank Governor, Bank staff, and customers (who can save money themselves ).
* Mr. Wang has the roles of bank employees and customers.
* Mr. Li has a role: customer.

Then, as long as the customer can do something, Xiao Li can do it, as well as Wang and Zhang xingchang. What bank staff can do is Wang and Zhang xingchang can do, but Xiao Li cannot.

For example, if a "customer" can save money, Zhang, Wang, and Xiao Li, who have the "customer" role, can save money. "Bank staff" can print the customer's transaction records, so Zhang and Wang employees with "bank staff" roles can, But Xiao Li cannot, you must find a person with a "bank clerk" role to print detailed transaction records. A "bank governor" can enter the bank's cash bank to raise money, so only President Zhang can, because it has the role of "bank governor.
This is a role-based authentication system, RBAC for short.

Role inheritance
Roles can be inherited. For example, the rules are as follows:
* All "bank presidents" are "bank employees". That is to say, as long as the bank staff can do anything, the bank governors can do.
* Any "bank employee" is a customer. The same as above, the bank employee can also do what the customer can do.
Then the role relationship becomes:
* President Zhang has the role of the Bank Governor.
* Wang has a role as a bank employee.
* Mr. Li has a role: customer.
This is simpler. This is the inheritance of roles.

Task inheritance
A task can contain another task. For example, "enter a bank ".
We set the "customer" role to have the "Bank access" permission. That is to say, the "customer" can execute the "entering the bank" task. Next, let's assume that "entering the counter" is the parent permission of the bank, that is, "entering the counter" includes "entering the bank ". Anyone who can "Enter the counter" can "enter the bank ". We authorize the "go to the counter" task to the "bank clerk ".

In terms of roles, Mr. Wang can enter the bank because Mr. Wang is a "bank clerk" and "bank clerk" includes the "customer" role. Therefore, "tasks" that "customers" can perform can also be performed by "bank staff. While "customers" can "Enter the Bank", Wang's staff can also "enter the bank ". This is brought about by the inheritance of roles.

Let's assume that there is a superior Zhao, who can go to the counter for inspection. Then, our task relationship is:
* Zhao has a task: Enter the counter.
Then, Zhao Can "enter the bank ". Because "entering the bank" is a task included in "entering the counter. Anyone who can execute "Enter the counter" can execute "enter the bank ". This is the inheritance of tasks.

Action
Action is an unclassified level. That is to say. An action cannot contain other actions. Assume that we have an action called "raising money from a bank warehouse ". We will include "entering the counter ". As long as you can perform the "withdraw money from the bank warehouse" role, you can execute the "Enter the counter" task.

Relationship
* A role can contain one or more roles.
* A role can contain one or more tasks.
* A role can contain one or more actions.
*
* A task can contain one or more tasks.
* A task can contain one or more actions.
*
* An action can only be contained by a role or task. An action cannot contain or be divided.
In this way, a permission management system is formed. You don't have to think about the literal meaning of "task" and "action. Both form two levels of permissions.

Empower
We have established RBAC permission management, and we need to manage permissions on the WEB. You need to write the code yourself.
Call one of the following methods to define an authorization project based on different types of projects:
* CAuthManager: createRole
* CAuthManager: createTask
* CAuthManager: createOperation
Once we have an authorization project, we can call the following method to establish an authorization project relationship:
* CAuthManager: addItemChild
* CAuthManager: removeItemChild
* CAuthItem: addChild
* CAuthItem: removeChild
Finally, we call the following method to assign a role project to each user:
* CAuthManager: assign
* CAuthManager: revoke
The following example shows how to use the provided API to create an authorization level:

Copy code

The Code is as follows: $ auth = Yii: app ()-> authManager;
$ Auth-> createOperation ('createpost', 'create a Post ');
$ Auth-> createOperation ('readpost', 'read a Post ');
$ Auth-> createOperation ('updatepost', 'Update a Post ');
$ Auth-> createOperation ('deletepost', 'delete a Post ');
$ BizRule = 'Return Yii: app ()-> user-> id = $ params ["post"]-> authID ;';
$ Task = $ auth-> createTask ('updateownpost', 'Update a post by author himself ', $ bizRule );
$ Task-> addChild ('updatepost ');
$ Role = $ auth-> createRole ('reader ');
$ Role-> addChild ('readpost ');
$ Role = $ auth-> createRole ('author ');
$ Role-> addChild ('reader ');
$ Role-> addChild ('createpost ');
$ Role-> addChild ('updateownpost ');
$ Role = $ auth-> createRole ('editor ');
$ Role-> addChild ('reader ');
$ Role-> addChild ('updatepost ');
$ Role = $ auth-> createRole ('admin ');
$ Role-> addChild ('editor ');
$ Role-> addChild ('author ');
$ Role-> addChild ('deletepost ');
$ Auth-> assign ('reader', 'readera ');
$ Auth-> assign ('author', 'authorb ');
$ Auth-> assign ('edit', 'editorc ');
$ Auth-> assign ('admin', 'admind ');

That is to say, you need to write a management interface to list your roles, tasks, and actions, and then you can manage them on this interface. Such as add, delete, and modify.

Permission check
If you have granted permissions on your management interface, you can check the permissions in the program:

Copy code

The Code is as follows: if (Yii: app ()-> user-> checkAccess ('createpost '))
{
// Operations such as form display
} Else {
// If the check fails, you can jump to or display a warning.
}

The code above checks whether the user can execute "createPost", which may be a task or action.

Other
Many people who say that RBAC is not easy to use in the Yii permission system do not actually understand the document. Based on my experience, I feel that RBAC in the Yii framework is the best framework I have ever used. In addition, you need to write the least code yourself.
RBAC of Yii has more advanced usage, such as "Business Rules" and "Default roles ". You can refer to the official documentation.
I know that some people still do not understand RBAC, or do not use RBAC of Yii. It doesn't matter. You can ask a question in the comment box below.
Happy Yii!

This article is an English version of an article which is originally in the Chinese language on aliyun.com and is provided for information purposes only. This website makes no representation or warranty of any kind, either expressed or implied, as to the accuracy, completeness ownership orreliability of the article or any translations thereof. If you have any concerns or complaints relating to the article, please send an email, providing a detailed description of the concern orcomplaint, to info-contact@alibabacloud.com. A staff member will contact you within 5 working days. Once verified, infringing content will be removed immediately.

Brief Introduction to the full guide to using RBAC in Yii (User Role permission Control) (2024)

FAQs

What is the full form of RBAC in yii2? ›

Yii provides two authorization methods: Access Control Filter (ACF) and Role-Based Access Control (RBAC).

What is the RBAC guide? ›

Through RBAC, system and network administrators are able to determine the roles and permissions for users throughout the organization, as well as on a granular level. They can also allocate roles to users, i.e. administrators, special users, and end-users, and also align roles according to the employees' positions.

What is RBAC with permissions? ›

Role-based access control (RBAC) refers to the idea of assigning permissions to users based on their role within an organization. It offers a simple, manageable approach to access management that is less prone to error than assigning permissions to users individually.

What is full RBAC? ›

Role-based access control (RBAC), also known as role-based security, is a mechanism that restricts system access. It involves setting permissions and privileges to enable access to authorized users.

Is RBAC authorization or authentication? ›

The term Role-Based Access Control (RBAC) refers to an authorization strategy that organizes privileges based on a role (hence the 'role-based' prefix). The RBAC authorization strategy is commonly used by medium-sized to large organizations that need to categorize their personnel using role assignment.

What are the 4 models of RBAC? ›

Four models for RBAC
  • Level 1: Flat RBAC. Flat RBAC is based on the three primary rules of role-based access control. ...
  • Level 2: Hierarchical RBAC. Hierarchical RBAC incorporates all of the rules and capabilities of Flat RBAC along with support for hierarchies. ...
  • Level 3: Constrained RBAC. ...
  • Level 4: Symmetric RBAC.

What is an example of a RBAC role? ›

Users can also be assigned temporary access to certain data or programs to complete a task and be removed after. Common examples of RBAC include: Software engineering role: Has access to GCP, AWS, and GitHub. Marketing role: Has access to HubSpot, Google Analytics, Facebook Ads, and Google Ads.

What are the fundamentals of RBAC? ›

RBAC's core principle is straightforward: it restricts system access to authorized users based on their roles within an organization or a system. Let's consider a common application scenario: an e-commerce platform.

How do I give access to RBAC? ›

Sign in to the Azure portal. In the Search box at the top, search for the scope you want to grant access to. For example, search for Management groups, Subscriptions, Resource groups, or a specific resource. Click the specific resource for that scope.

What is the difference between access control IAM and RBAC? ›

Role-Based Access Control (RBAC) is a cornerstone of Identity and Access Management (IAM), helping organizations manage user permissions in a structured and efficient manner. RBAC is rooted in the concept of least privilege, ensuring that users only receive the minimum level of access necessary to perform their duties.

What is the principle of RBAC? ›

Core RBAC includes three basic operating principles: role assignment, role authorization and permission authorization. Role assignment requires that users be assigned a role before exercising permissions contained within that role.

Why is RBAC hard? ›

One of the main problems is that it is not an automatic process, meaning that it needs to be painstakingly managed and often involves significant manual intervention.

What is the RBAC technique? ›

The Role-Based Access Control, or RBAC, model provides access control based on the position an individual fills in an organization. So, instead of assigning Alice permissions as a security manager, the position of security manager already has permissions assigned to it.

What are the disadvantages of RBAC? ›

The main disadvantage of RBAC is what is most often called the 'role explosion': due to the increasing number of different (real world) roles (sometimes differences are only very minor) you need an increasing number of (RBAC) roles to properly encapsulate the permissions (a permission in RBAC is an action/operation on ...

What does the RBAC stand for? ›

role-based access control (RBAC)

What is RBAC in IAM? ›

Role-based access control (RBAC) is a security methodology based on managing user access to protect resources, including data, applications, and systems, from improper access, modification, addition, or deletion. It grants access based on a user's needs according to their position.

What is the full form of RBAC in Kubernetes? ›

Role-based access control (RBAC) is a method of regulating access to computer or network resources based on the roles of individual users within your organization.

What is RBAC in Sailpoint? ›

Role-based access control (RBAC) is an approach to access security that relies on a person's role within an organization to determine what access they have. A role is a collection of permissions, and users receive permissions through the roles they have been assigned.

Top Articles
Latest Posts
Article information

Author: Mr. See Jast

Last Updated:

Views: 5735

Rating: 4.4 / 5 (55 voted)

Reviews: 94% of readers found this page helpful

Author information

Name: Mr. See Jast

Birthday: 1999-07-30

Address: 8409 Megan Mountain, New Mathew, MT 44997-8193

Phone: +5023589614038

Job: Chief Executive

Hobby: Leather crafting, Flag Football, Candle making, Flying, Poi, Gunsmithing, Swimming

Introduction: My name is Mr. See Jast, I am a open, jolly, gorgeous, courageous, inexpensive, friendly, homely person who loves writing and wants to share my knowledge and understanding with you.