2013-11-22 42 views
1

我试图在我的应用中使用RBAC和Auth扩展。我能够通过扩展喜欢这里提供 GUI添加层次: enter image description hereyii-auth:我应该在哪里添加bizrule到新任务?

什么我试着是增加bizRule到updateOwnPost现在要做的。 据link我需要添加一些代码:

$auth=Yii::app()->authManager; 

$auth->createOperation('createPost','create a post'); 
$auth->createOperation('readPost','read a post'); 
$auth->createOperation('updatePost','update a post'); 
$auth->createOperation('deletePost','delete a post'); 

$bizRule='return Yii::app()->user->id==$params["post"]->authID;'; 
$task=$auth->createTask('updateOwnPost','update a post by author himself',$bizRule); 
$task->addChild('updatePost'); 

$role=$auth->createRole('reader'); 
$role->addChild('readPost'); 

$role=$auth->createRole('author'); 
$role->addChild('reader'); 
$role->addChild('createPost'); 
$role->addChild('updateOwnPost'); 

$role=$auth->createRole('editor'); 
$role->addChild('reader'); 
$role->addChild('updatePost'); 

$role=$auth->createRole('admin'); 
$role->addChild('editor'); 
$role->addChild('author'); 
$role->addChild('deletePost'); 

但是我们可以看到这个代码是创建新的操作/任务/角色。我已经通过GUI添加了它们。 所以我现在需要的是地方到另一个地方是这样的:

$bizRule='return Yii::app()->user->id==$params["post"]->authID;'; 

创建较早updateOwnPost任务。

OFC我的运气为首我只是为了对这个话题:link其中m-NEL说:

这个问题是在扩展的 页面的评论部分回答。阅读:http://www.yiiframework.com/extension/auth/#c11261

和OFC评论我需要的是冲出来的空间:)

有人能回答我该怎么办,提供bizrule到任务? 谢谢;) (和对不起4我的英语)

回答

2

这增加了一个bizrule任务,并添加一个孩子(操作)。

$bizRule='return Yii::app()->user->id==$params["post"]->authID;'; // here goes the bizrule 
$task=$auth->createTask('updateOwnPost','update a post by author himself',$bizRule); // here we create a task and attach the bizrule 
$task->addChild('updatePost'); // here we attach the operation to it 

所以,你需要做一个测试你的代码来执行访问检查,是这样的:

$params=array('post'=>$this->loadPost($id)); 
if(Yii::app()->user->checkAccess('updateOwnPost',$params)) 
{ 
    // update post 
} 

可以将此片段添加到过滤器或您的行动中也进行检查。 我认为做一些小的检查以确保用户可以编辑帖子会更容易。首先检查用户是否是作者,然后检查他是否可以编辑该帖子。过滤器有点复杂。

相关问题