Home >Backend Development >PHP Tutorial >PHP and Yii2 integrate to implement RBAC permission management function

PHP and Yii2 integrate to implement RBAC permission management function

王林
王林Original
2023-06-25 21:18:431874browse

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. In the RBAC model, permissions are divided into a series of roles and operations. Users can be assigned one or more roles, and each role has different permissions. The advantage of the RBAC model is its flexibility and easy scalability.

2. RBAC in Yii2

The Yii2 framework provides a complete set of RBAC implementation solutions, including concepts such as permissions, roles, rules, and their operations. In Yii2, a permission is defined as an operation or a collection of multiple operations, and each permission can be assigned to one or more roles. A role represents a set of permissions that can be granted to a user. Rules are used to restrict actions for roles.

Yii2's RBAC implementation adopts a database-based role management method, which stores information such as permissions, roles, rules, etc. in the database. The Yii2 framework provides two components, ActiveRecord and DbManager, to handle these role management operations.

3. Implementation steps

The following will introduce the steps to implement RBAC permission management in the Yii2 framework.

  1. Create permission table

Create a table named auth_item in the database to store permission information. The table structure is as follows:

CREATE TABLE `auth_item` (
  `name` varchar(64) NOT NULL,
  `type` smallint(6) NOT NULL,
  `description` varchar(255) DEFAULT NULL,
  `rule_name` varchar(64) DEFAULT NULL,
  `data` blob DEFAULT NULL,
  `created_at` int(11) DEFAULT NULL,
  `updated_at` int(11) DEFAULT NULL,
  PRIMARY KEY (`name`),
  KEY `idx-auth_item-type` (`type`),
  KEY `idx-auth_item-rule_name` (`rule_name`),
  CONSTRAINT `fk-auth_item-rule_name` FOREIGN KEY (`rule_name`) REFERENCES `auth_rule` (`name`) ON DELETE SET NULL ON UPDATE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

In the table, the name field represents the permission name, the type field represents the permission type (1 represents role, 2 represents permission), the description field represents the permission description, the rule_name field represents the permission rule name, data Fields represent permission data. The created_at and updated_at fields represent the creation time and modification time.

  1. Create role table

Create a table named auth_item_child in the database to store role information. The table structure is as follows:

CREATE TABLE `auth_item_child` (
  `parent` varchar(64) NOT NULL,
  `child` varchar(64) NOT NULL,
  PRIMARY KEY (`parent`,`child`),
  KEY `idx-auth_item_child-child` (`child`),
  CONSTRAINT `fk-auth_item_child-child` FOREIGN KEY (`child`) REFERENCES `auth_item` (`name`) ON DELETE CASCADE ON UPDATE CASCADE,
  CONSTRAINT `fk-auth_item_child-parent` FOREIGN KEY (`parent`) REFERENCES `auth_item` (`name`) ON DELETE CASCADE ON UPDATE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

In the table, the parent field represents the name of the parent role, and the child field represents the name of the child role.

  1. Create a rule table

Create a table named auth_rule in the database to store rule information. The table structure is as follows:

CREATE TABLE `auth_rule` (
  `name` varchar(64) NOT NULL,
  `data` blob DEFAULT NULL,
  `created_at` int(11) DEFAULT NULL,
  `updated_at` int(11) DEFAULT NULL,
  PRIMARY KEY (`name`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

In the table, the name field represents the rule name, and the data field represents the rule data. The created_at and updated_at fields represent the creation time and modification time.

  1. Create user role table

Create a table named auth_assignment in the database to store user role information. The table structure is as follows:

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 `fk-auth_assignment-item_name` FOREIGN KEY (`item_name`) REFERENCES `auth_item` (`name`) ON DELETE CASCADE ON UPDATE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

In the table, the item_name field represents the role name, and the user_id field represents the user ID.

  1. Configuring the database

Configure the components DbManager and AuthManager in main.php:

'components' => [
    'authManager' => [
        'class' => 'yiibacDbManager',
    ],
    'db' => [
        'class' => 'yiidbConnection',
        'dsn' => 'mysql:host=localhost;dbname=test',
        'username' => 'root',
        'password' => '',
        'charset' => 'utf8',
    ],
],
  1. Create permissions and roles

Using the AuthManager component, you can create permissions and roles through code:

$auth = Yii::$app->authManager;

// 创建权限
$createPost = $auth->createPermission('createPost');
$createPost->description = '创建文章';
$auth->add($createPost);

// 创建角色
$admin = $auth->createRole('admin');
$admin->description = '管理员';
$auth->add($admin);

// 将权限分配给角色
$auth->addChild($admin, $createPost);
  1. Assign roles to users

Assign roles to users through code:

$auth = Yii::$app->authManager;

// 将角色分配给用户
$auth->assign($admin, $user->id);
  1. Using RBAC

Use RBAC for permission control in the program:

if (Yii::$app->user->can('createPost')) {
    // 允许创建文章
} else {
    // 不允许创建文章
}

4. Summary

This article introduces how to use RBAC in the Yii2 framework Implement RBAC permission management functions, including creating permission tables, role tables, rule tables and user role tables, configuring the database, using the AuthManager component to create permissions and roles, and using RBAC for permission control. RBAC is a flexible and easily extensible permission management model that can meet the needs of various applications. In the Yii2 framework, through the use of RBAC and AuthManager components, the RBAC permission management function can be easily implemented.

The above is the detailed content of PHP and Yii2 integrate to implement RBAC permission management function. For more information, please follow other related articles on the PHP Chinese website!

Statement:
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