PHP添加角色怎么实现,php – 如何添加角色给用户? Yii2

对于数据库版本的RBAC,使用DbManager(报价frm:Alexufo):

use yii\rbac\DbManager;

$r=new DbManager;

$r->init();

$r->createRole("admin","Administrator");

$r->save();

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

示例访问规则:

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文件).

功能和更多细节:

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值