【发布时间】:2014-04-26 03:14:35
【问题描述】:
我正在尝试自定义 RBAC,因此我为用户创建了多个角色。
现在我正在尝试了解如何告诉控制器哪个操作应该由哪个角色访问。
在控制器代码中我看到了这个
public function accessRules()
{
return array(
array('allow', // allow all users to perform 'index' and 'view' actions
'actions'=>array('index','view'),
'users'=>array('*'),
),
array('allow', // allow authenticated user to perform 'create' and 'update' actions
'actions'=>array('create','update'),
'users'=>array('@'),
),
array('allow', // allow admin user to perform 'admin' and 'delete' actions
'actions'=>array('admin','delete'),
'users'=>array('admin'),
),
array('deny', // deny all users
'users'=>array('*'),
),
现在我认为“用户”是指 RBAC 的用户角色,但我想我完全错了。 所以一方面我有这个 accessRules,另一方面我有 RBAC 的几个角色。如何告诉控制器使用我的角色?
Jonny 更新
听起来很有趣.... 我已经做了测试动作
public function actionNew()
{
echo 'TEST'; die;
然后我让所有人都可以访问规则,只是为了测试
public function accessRules()
{
return array(
array('allow', // allow all users to perform 'index' and 'view' actions
'actions'=>array('index','view'),
'users'=>array('*'),
),
array('allow', // allow authenticated user to perform 'create' and 'update' actions
'actions'=>array('create','update'),
'users'=>array('@'),
),
array('allow', // allow admin user to perform 'admin' and 'delete' actions
'actions'=>array('admin','delete'),
'users'=>array('admin'),
),
array('deny', // deny all users
'users'=>array('*'),
),
array('allow',
'actions'=>array('new'),
'users'=>array('*'),
),
);
}
但它不起作用:(有什么想法吗?
我来了
Error 403
You are not authorized to perform this action.
更新 2
Ok 测试操作适用于 * 用户。
现在我正试图将它与我的角色联系起来,但我被困在那里:(
array('allow',
'actions'=>array('new'),
'roles'=>array('role1'),
),
不工作:(
在带有调用此操作的按钮的页面上,我有角色检查代码
if(Yii::app()->user->checkAccess('role1')){
echo "hello, I'm role1";
}
Jonny 的最新更新 谢谢你的帮助,我终于做到了。 我不知道为什么,但问题是我必须将所有这些新操作放在拒绝数组之前。
这样
public function accessRules()
{
return array(
array('allow', // allow all users to perform 'index' and 'view' actions
'actions'=>array('index','view'),
'users'=>array('*'),
),
array('allow', // allow authenticated user to perform 'create' and 'update' actions
'actions'=>array('create','update'),
'users'=>array('@'),
),
array('allow', // allow admin user to perform 'admin' and 'delete' actions
'actions'=>array('admin','delete'),
'users'=>array('admin'),
),
array('allow',
'actions'=>array('new'),
'roles'=>array('role1'),
),
array('deny', // deny all users
'users'=>array('*'),
),
);
}
在这种情况下它可以工作。之前我的新操作位于“拒绝”错误后的代码中,您可以检查上层更新中的代码片段。这对我来说很奇怪,但现在它工作正常:)
【问题讨论】:
-
这些是完整的规则吗?我通常只会将“新”添加到您的索引和视图所在的现有数组中。检查是否有效
-
更新了完整的规则列表。还是不行:(
-
您已经为
*用户提供了该声明。删除它并将new添加到这一行'actions'=>array('index','view'),规则的第一个数组 -
请检查更新 2
-
我终于成功了。代码正在运行。我已经在更新中发布了。
标签: yii controller rbac access-rules