我们使用了Yii2框架的最后一个alpha。已创建用户的角色,但问题是它如何分配给用户。没有文件。
答案 0 :(得分:15)
对于RBAC的数据库版本,请使用DbManager(quote 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
示例访问规则:
<?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
答案 1 :(得分:5)
$user_id = 1;
$auth = new DbManager;
$auth->init();
$role = $auth->createRole('editor');
$auth->add($role);
$auth->assign($role, $user_id);
=============================================== ========================== 如果你想选择角色而不是创建
$auth = new DbManager;
$auth->init();
$role = $auth->getRole('admin');
$auth->assign($role, $user_id);
100%工作!
答案 2 :(得分:3)
解决!
================创建角色============
use yii\rbac\PhpManager;
$r=new PhpManager;
$r->init();
$r->createRole("admin","Администратор");
$r->save();
=============== assign ==================
$r->assign('1','admin'); //1 is user id
答案 3 :(得分:1)
实现管理员角色的一种非常简单的方法是将其添加到您的控制器:
use yii;
/**
* @inheritdoc
*/
public function behaviors()
{
return [
'access' => [
'class' => AccessControl::className(),
'rules' => [
[
'allow' => true,
'actions' => ['index'],
'roles' => ['@'],
],
[
'allow' => !Yii::$app->user->isGuest && Yii::$app->user->identity->isAdmin(),
'actions' => ['view', 'create', 'update', 'delete'],
],
],
],
];
}
然后将User
模型添加到isAdmin()
,其中为您的管理员用户返回true
,为其他人返回false
。就个人而言,我使用:
public function isAdmin() {
return Self::ROLE_ADMIN === $this->role;
}
不可否认,这不是“按书”。但它简单,快速,有效。
答案 4 :(得分:0)
$user_id = \Yii::$app->user->id;
$auth = new DbManager;
$auth->init();
$role = $auth->createRole('editor');
$auth->add($role);
$auth->assign($role, $user_id);