第一种方法PhpManager
在config下的main-local.php 加上
'authManager' => [
'class' => 'yii\rbac\PhpManager',
]
记住要在前台或者后台建一个rbac文件夹
运行完会自动生成rbac下的文件
第二种DbManager
打开Windows 的cmd
在config下的main-local.php 加上
'authManager' => [
'class' => 'yii\rbac\DbManager',
'itemTable' => 'auth_item',
'assignmentTable' => 'auth_assignment',
'itemChildTable' => 'auth_item_child',
],
然后在windows 里cmd进入到框架输入
yii migrate(运行这个命令,生成user表)
yii migrate --migrationPath=@yii/rbac/migrations/ 运行此命令生成权限数据表
两种方法控制器都可以这么写
<?php
namespace backend\controllers;
use Yii;
use yii\web\Controller;
use yii\filters\AccessControl;
class RbacController extends Controller{
//判断是否有权限
public function beforeAction($action){
$name=$action->id;
$user=\Yii::$app->user;
if(!$user->can($name)){
echo $this->renderContent('没有权限执行该操作');die;
}
return true;
}
public function actionAdd()
{
return $this->renderContent('添加');
}
public function actionDelete()
{
return $this->renderContent('删除');
}
public function actionUpdate()
{
return $this->renderContent('更新');
}
public function actionSelect()
{
return $this->renderContent('查询');
}
public function actionIndex(){
// 获取authMananger组件对象
$auth = \Yii::$app->authManager;
// 增加角色
$author = $auth->createRole('院长');
$auth->add($author);
//$auth->assign($author, 1);//用户添加角色
// 增加权限
$postAdd = $auth->createPermission('add');
$postAdd->description = '文章添加';
$auth->add($postAdd);
$postDelete = $auth->createPermission('delete');
$postDelete->description = '文章删除';
$auth->add($postDelete);
$postUpdate = $auth->createPermission('update');
$postUpdate->description = '文章编辑';
$auth->add($postUpdate);
$postSelect = $auth->createPermission('select');
$postSelect->description = '文章查看';
$auth->add($postSelect);
//$auth->addChild($author, $postAdd);// 给角色添加权限
}
}
namespace backend\controllers;
use Yii;
use yii\web\Controller;
use yii\filters\AccessControl;
class RbacController extends Controller{
//判断是否有权限
public function beforeAction($action){
$name=$action->id;
$user=\Yii::$app->user;
if(!$user->can($name)){
echo $this->renderContent('没有权限执行该操作');die;
}
return true;
}
public function actionAdd()
{
return $this->renderContent('添加');
}
public function actionDelete()
{
return $this->renderContent('删除');
}
public function actionUpdate()
{
return $this->renderContent('更新');
}
public function actionSelect()
{
return $this->renderContent('查询');
}
public function actionIndex(){
// 获取authMananger组件对象
$auth = \Yii::$app->authManager;
// 增加角色
$author = $auth->createRole('院长');
$auth->add($author);
//$auth->assign($author, 1);//用户添加角色
// 增加权限
$postAdd = $auth->createPermission('add');
$postAdd->description = '文章添加';
$auth->add($postAdd);
$postDelete = $auth->createPermission('delete');
$postDelete->description = '文章删除';
$auth->add($postDelete);
$postUpdate = $auth->createPermission('update');
$postUpdate->description = '文章编辑';
$auth->add($postUpdate);
$postSelect = $auth->createPermission('select');
$postSelect->description = '文章查看';
$auth->add($postSelect);
//$auth->addChild($author, $postAdd);// 给角色添加权限
}
}