Yii2限制对RBAC模块本身的访问
如何限制对RBAC模块本身的访问 我正在使用Yii2限制对RBAC模块本身的访问,yii2,yii2-advanced-app,Yii2,Yii2 Advanced App,如何限制对RBAC模块本身的访问 我正在使用yii\rbac\DbManager并且我已经在后端创建了一个模块(授权),用于权限分配,创建授权项目,现在我想确保只有管理员可以访问此模块 在控制器中,我使用了一些这样的东西,它工作得很好 use yii\filters\AccessControl; class MyController extends Controller { public function behaviors() { return [
yii\rbac\DbManager
并且我已经在后端创建了一个模块(授权),用于权限分配,创建授权项目,现在我想确保只有管理员可以访问此模块
在控制器中,我使用了一些这样的东西,它工作得很好
use yii\filters\AccessControl;
class MyController extends Controller
{
public function behaviors()
{
return [
'access' => [
'class' => AccessControl::className(),
'only' => ['index', 'view', 'create', 'update', 'delete'], //only be applied to
'rules' => [
[
'allow' => true,
'actions' => ['index', 'view', 'create', 'update','delete'],
'roles' => ['admin'],
],
],
],
.........
我已经把它放在Authorization.php init函数中,但是什么也没有发生,所有的auth控制器都是可访问的
public function init()
{
if(\Yii::$app->user->can('admin'))
parent::init();
// custom initialization code goes here
}
更新
backend/config/main.php
'modules' => [
'authorization' => [
'class' => 'backend\modules\authorization\Authorization',
],
],
在模块类中,可以添加此方法
public function beforeAction($action)
{
if (!parent::beforeAction($action)) {
return false;
}
if (!\Yii::$app->user->can('admin')) {
throw new \yii\web\ForbiddenHttpException('You are not allowed to access this page.');
}
return true;
}
您应该在需要的每个控制器中分配基于角色的重新定向。@scaisEdge,Bizley解决方案是完美的。我也试过:
'modules'=>['authorization'=>['class'=>'backend\modules\authorization\authorization','as access'=>['class'=>'yii\filters\AccessControl'],],,,,,,,,,,
谢谢。。但是我更喜欢在controller中设置角色pemission..Class'backend\modules\authorization\bankedenhttpexception'notfoundfix:yii\web\bankedenhttpexception
显然我没有添加完全限定名。使用任何好的IDE,您都可以在大约10秒钟内获得此结果。以防万一Yii::$app
应该是\Yii::$app
如果没有添加到语句中,请使用语句。