php - 如何给用户添加角色?

标签 php

我们在上一个 alpha 中使用了 Yii2 框架。用户的角色已经创建,但问题是它如何分配给用户。没有文档。

最佳答案

对于 RBAC 的数据库版本,使用 DbManager(引用来自 Alexufo):

use yii\rbac\DbManager;
$r=new DbManager;
$r->init();
$r->createRole("admin","Administrator");
$r->save();

$r->assign('1','admin');   //1 is user id 

示例访问规则:

<?php
namespace backend\controllers;

use yii;
use yii\web\AccessControl;
use yii\web\Controller;

class SiteController extends Controller
{
    public function behaviors()
    {
        return [
            'access' => [
                'class' => AccessControl::className(),
                'rules' => [
                    [
                        //'actions' => ['login', 'error'], // Define specific actions
                        'allow' => true, // Has access
                        'roles' => ['@'], // '@' All logged in users / or your access role e.g. 'admin', 'user'
                    ],
                    [
                        'allow' => false, // Do not have access
                        'roles'=>['?'], // Guests '?'
                    ],
                ],
            ],
        ];
    }

    public function actionIndex()
    {
        return $this->render( 'index' );
    }
}
?>

不要忘记将此添加到您的配置文件 (config/main.php):

'components' => [
    'authManager'=>array(
        'class' => 'yii\rbac\DbManager',
        'defaultRoles' => ['end-user'],
    ),
    ...
]

表格:

drop table if exists `tbl_auth_assignment`;
drop table if exists `tbl_auth_item_child`;
drop table if exists `tbl_auth_item`;

create table `tbl_auth_item`
(
   `name`                 varchar(64) not null,
   `type`                 integer not null,
   `description`          text,
   `biz_rule`              text,
   `data`                 text,
   primary key (`name`),
   key `type` (`type`)
) engine InnoDB;

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

create table `tbl_auth_assignment`
(
   `item_name`            varchar(64) not null,
   `user_id`              varchar(64) not null,
   `biz_rule`              text,
   `data`                 text,
   primary key (`item_name`,`user_id`),
   foreign key (`item_name`) references `tbl_auth_item` (`name`) on delete cascade on update cascade
) engine InnoDB;

您还可以在“yii/rbac”目录(包括其他 SQL 文件)中找到此信息。 有关功能和更多详细信息:

https://github.com/yiisoft/yii2/blob/master/docs/guide/security-authorization.md

关于php - 如何给用户添加角色?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21308423/

相关文章:

PHP & do/while 循环内存泄漏

php - 文件中的 Zend 元数据缓存

php - JS 文件在 WordPress 中调用两次

php - MySQL正则表达式按顺序匹配包含某些字段的多个CSV行

php - 使用 PHP 在结帐时添加总计

php - JSencodeURI() 和 PHP urldecode() 之间的不同结果

php - 有经验的 Asp.net 程序员在开始使用 PHP 时应该了解什么?

php - 如何在php中计算crc16

php - 更新多个php文件(具体值,按文件顺序增加)

php - 使用PHP将Access数据导入MySQL