php 带有 rbac 和数据库存储的 Yii2 角色管理

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/24554712/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 17:21:37  来源:igfitidea点击:

Yii2 role management with rbac and database storage

phpyiiyii2yii-extensionsrbac

提问by b24

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

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

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

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

Please guide me and tell me the simplest solution.

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

回答by Dency G B

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

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

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

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

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

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

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: Setting up the config file

第二步:设置配置文件

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 用作DbManager. 这是通过将以下行添加到配置文件的 components 部分来完成的

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

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);

And you can assign it to the users by

您可以通过以下方式将其分配给用户

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

http://www.yiiframework.com/doc-2.0/guide-security-authorization.html

http://www.yiiframework.com/doc-2.0/guide-security-authorization.html

回答by JJPunch

Updated link from official docs: http://www.yiiframework.com/doc-2.0/guide-security-authorization.html

来自官方文档的更新链接:http: //www.yiiframework.com/doc-2.0/guide-security-authorization.html

If you are working with database you have to add authmanager to your application components:

如果您正在使用数据库,则必须将 authmanager 添加到应用程序组件中:

return [
// ...
'components' => [
    'authManager' => [
        'class' => 'yii\rbac\DbManager',
    ],
    // ...
],

];

];

And then execute a migration:

然后执行迁移:

yii migrate --migrationPath=@yii/rbac/migrations

It will create automatically the required tables in your database. Now you can access the AuthManager via

它将自动在您的数据库中创建所需的表。现在您可以通过访问 AuthManager

yii migrate --migrationPath=@yii/rbac/migrations

yii migrate --migrationPath=@yii/rbac/migrations