带有 rbac 和数据库存储的 Yii2 角色管理 [英] Yii2 role management with rbac and database storage

查看:25
本文介绍了带有 rbac 和数据库存储的 Yii2 角色管理的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想学习 Yii2 会员资格并使用 Yii 使用数据库存储和检索角色.

I want to learn Yii2 membership and use Yii to store and retrieve roles using a database.

我已阅读安全授权如何为用户添加角色?有人有 Rbac 的工作示例吗? 并尝试使用 yii2-admin 扩展并尝试了解 Yii管理用户角色,但我找不到任何工作示例或简单的分步示例.

I have read Security Authorization and How to add role to user? and Does Anyone Have A Working Example Of Rbac? and also try using the yii2-admin extension and tried to understand how Yii manages user roles but I can't find any working samples or simple step by step examples.

请指导我并告诉我最简单的解决方案.

Please guide me and tell me the simplest solution.

推荐答案

实现基于角色的访问控制是一个非常简单的过程,您甚至可以根据需要从数据库加载您的角色.

Implementing a role based access control is a very easy process and you can even load your roles from the database if you want.

步骤 1:在数据库中创建必要的表 [您也可以使用控制台命令 yii migrate 而不是步骤 1 来应用迁移]

Step1: Creating necessary tables in the database [ You can also apply migrations with console command yii migrate instead of step 1 ]

第一步是在数据库中创建必要的表.下面是你需要在数据库中运行的sql.

The first step is to create necessary tables in the database.Below is the sql you need to run in the database.

drop table if exists `auth_assignment`;
drop table if exists `auth_item_child`;
drop table if exists `auth_item`;
drop table if exists `auth_rule`;

create table `auth_rule`
(
`name` varchar(64) not null,
`data` text,
`created_at` integer,
`updated_at` integer,
    primary key (`name`)
) engine InnoDB;

create table `auth_item`
(
`name` varchar(64) not null,
`type` integer not null,
`description` text,
`rule_name` varchar(64),
`data` text,
`created_at` integer,
`updated_at` integer,
primary key (`name`),
foreign key (`rule_name`) references `auth_rule` (`name`) on delete set null on update cascade,
key `type` (`type`)
) engine InnoDB;

create table `auth_item_child`
(
`parent` varchar(64) not null,
`child` varchar(64) not null,
primary key (`parent`, `child`),
foreign key (`parent`) references `auth_item` (`name`) on delete cascade on update cascade,
foreign key (`child`) references `auth_item` (`name`) on delete cascade on update cascade
) engine InnoDB;

create table `auth_assignment`
(
`item_name` varchar(64) not null,
`user_id` varchar(64) not null,
`created_at` integer,
primary key (`item_name`, `user_id`),
foreign key (`item_name`) references `auth_item` (`name`) on delete cascade on update cascade
) engine InnoDB;

Step2:设置配置文件

Step2: Setting up the config file

现在您可以设置配置文件以将 authmanager 用作 DbManager.这是通过将以下行添加到配置文件的 components 部分来完成的

Now you can set up the config file to use the authmanager as DbManager. This is done by adding the following lines to the components section of your config file

     'authManager' => [
                           'class' => 'yii\rbac\DbManager',
                           'defaultRoles' => ['guest'],
          ],

步骤 3:添加和分配角色.

Step3: Adding and assigning roles.

现在您只需将以下代码写入相应的控制器即可添加角色.

Now you can add roles by simply writing the following code to your corresponding controller.

    use yii\rbac\DbManager;
    $r=new DbManager;
    $r->init();
    $test = $r->createRole('test');
    $r->add($test);

您可以通过

    $r->assign($test, 2);

http://www.yiiframework.com/doc-2.0/指南安全授权.html

这篇关于带有 rbac 和数据库存储的 Yii2 角色管理的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆