How to implement rbac in yii2
RBAC (Role-Based Access Control) role-based access control.
1. Basic idea: Introduce the concept of roles between users and access permissions, connect users and roles, and control user access to system resources through authorization of roles. Compared with traditional access control, the introduction of roles greatly simplifies the management of permissions. (Recommended learning: yii framework )
1). Role: It can be understood as a collection of permissions and permissions. For example: in a forum system, "super administrator" and "moderator" are roles.
2).Permissions: Moderators can manage posts in the forum, users in the forum, etc. These are permissions.
Implementation of rbac in Yii2
1.Yii2 implements a general layered RBAC, and the model it follows is also the NIST RBAC model.
2. The concept of rule is added in yii2. What is rule?
For example: For the article system, we have administrators and ordinary users, which allow administrators to perform any operations on articles, but only ordinary users are allowed to create articles and modify articles they create, that is to say Ordinary users have the permission to modify articles, but the restriction of Extra is that they can only modify their own articles. This verification of Extra is what the rules are responsible for.
3. The permission management implementation of yii2 supports two carriers: file and db. The core of the db-based implementation is four tables:
1) Storage roles or permissions Table: auth_item (type: 1 represents role; 2 represents permission)
CREATE TABLE `auth_item` ( `name` varchar(64) NOT NULL, `type` int(11) NOT NULL, `description` text, `rule_name` varchar(64) DEFAULT NULL, `data` text, `created_at` int(11) DEFAULT NULL, `updated_at` int(11) DEFAULT NULL, PRIMARY KEY (`name`), KEY `rule_name` (`rule_name`), KEY `type` (`type`), CONSTRAINT `auth_item_ibfk_1` FOREIGN KEY (`rule_name`) REFERENCES `auth_rule` (`name`) ON DELETE SET NULL ON UPDATE CASCADE ) ENGINE=InnoDB DEFAULT CHARSET=utf8
2) The superior and subordinate association table of permissions and roles: auth_item_child
(includes the relationship: Roles can contain roles, roles can contain permissions, and permissions can contain permissions, but permissions cannot contain roles)
CREATE TABLE `auth_item_child` ( `parent` varchar(64) NOT NULL, `child` varchar(64) NOT NULL, PRIMARY KEY (`parent`,`child`), KEY `child` (`child`), CONSTRAINT `auth_item_child_ibfk_1` FOREIGN KEY (`parent`) REFERENCES `auth_item` (`name`) ON DELETE CASCADE ON UPDATE CASCADE, CONSTRAINT `auth_item_child_ibfk_2` FOREIGN KEY (`child`) REFERENCES `auth_item` (`name`) ON DELETE CASCADE ON UPDATE CASCADE ) ENGINE=InnoDB DEFAULT CHARSET=utf8
3) Assignment table of users and permissions (roles): auth_assignment
CREATE TABLE `auth_assignment` ( `item_name` varchar(64) NOT NULL, `user_id` varchar(64) NOT NULL, `created_at` int(11) DEFAULT NULL, PRIMARY KEY (`item_name`,`user_id`), CONSTRAINT `auth_assignment_ibfk_1` FOREIGN KEY (`item_name`) REFERENCES `auth_item` (`name`) ON DELETE CASCADE ON UPDATE CASCADE ) ENGINE=InnoDB DEFAULT CHARSET=utf8
4) Rule table: auth_rule
CREATE TABLE `auth_rule` ( `name` varchar(64) NOT NULL, `data` text, //存的是一个序列化的实现了yii\rbac\Rule接口的类的一个对象实例 `created_at` int(11) DEFAULT NULL, `updated_at` int(11) DEFAULT NULL, PRIMARY KEY (`name`), KEY `name` (`name`), KEY `created_at` (`created_at`), KEY `updated_at` (`updated_at`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8 COMMENT='权限规则表';
The above is the detailed content of How to implement rbac in yii2. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



The article discusses best practices for deploying Yii applications in cloud-native environments, focusing on scalability, reliability, and efficiency through containerization, orchestration, and security measures.

The article discusses key considerations for using Yii in serverless architectures, focusing on statelessness, cold starts, function size, database interactions, security, and monitoring. It also covers optimization strategies and potential integrati

The article discusses strategies for testing Yii applications using Codeception, focusing on using built-in modules, BDD, different test types, mocking, CI integration, and code coverage.

Yii's built-in testing framework enhances application testing with features like PHPUnit integration, fixture management, and support for various test types, improving code quality and development practices.

The article discusses tools for monitoring and profiling Yii application performance, including Yii Debug Toolbar, Blackfire, New Relic, Xdebug, and APM solutions like Datadog and Dynatrace.

The article discusses implementing real-time data synchronization using Yii and WebSockets, covering setup, integration, and best practices for performance and security.

The article discusses key considerations for deploying Yii applications in production, focusing on environment setup, configuration management, performance optimization, security, logging, monitoring, deployment strategies, and backup/recovery plans.

The article discusses Yii's benefits for SaaS development, focusing on performance, security, and rapid development features to enhance scalability and reduce time-to-market.
