【问题标题】:Yii2 role management with rbac and database storageYii2 角色管理与 rbac 和数据库存储
【发布时间】:2017-11-02 20:33:31
【问题描述】:

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

我已阅读 Security AuthorizationHow to add role to user?Does Anyone Have A Working Example Of Rbac? 并尝试使用 yii2-admin 扩展名并尝试了解 Yii 如何管理用户角色,但我找不到任何工作示例或简单的分步示例.

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

【问题讨论】:

    标签: php yii yii2 yii-extensions rbac


    【解决方案1】:

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

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

    第一步是在数据库中创建必要的表。下面是你需要在数据库中运行的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;
    

    第二步:设置配置文件

    现在您可以设置配置文件以将身份验证管理器用作DbManager。这是通过将以下行添加到配置文件的组件部分来完成的

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

    Step3:添加和分配角色。

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

        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/guide-security-authorization.html

    【讨论】:

    • 谢谢 Dency,但我有两个问题:**1.**是基于表结构约定还是您的建议? **2.**是否可以通过迁移配置角色管理(创建表、创建角色和分配角色)??
    • 谢谢它工作得很好,我从你的回答中理解了 RBAC。
    • 你好,你能解释一下:我需要在'auth_assignment'表中添加外键user_id吗?
    • 第1步可以换成./yii migrate --migrationPath=@yii/rbac/migrations/
    【解决方案2】:

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

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

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

    ];

    然后执行迁移:

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

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

    访问 AuthManager

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

    【讨论】:

    • Stack Overflow 不欢迎仅链接的答案。如果您包含本文的重要部分,那就更好了。
    • 这个答案好多了。
    • 对于基本应用程序,它需要添加到 2 个位置,然后才可以工作 console.php & web.php
    猜你喜欢
    • 1970-01-01
    • 2017-04-24
    • 2019-01-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-02-13
    • 2021-12-14
    相关资源
    最近更新 更多