我想为创建帖子/类别的作者提供许可更新/删除帖子/类别
我不知道,我必须在这里作为第二个参数。我试过了:
$post=new Post();
if (Yii::$app->user->can('updatePost',['Post'=>$post]))
但获取错误获取未知属性:common \ models \ Post :: createdBy
我的班级作者规则:
class AuthorRule extends Rule
{
public $name = 'isAuthor';
/**
* @param string|integer $user the user ID.
* @param Item $item the role or permission that this rule is associated with
* @param array $params parameters passed to ManagerInterface::checkAccess().
* @return boolean a value indicating whether the rule permits the role or permission it is associated with.
*/
public function execute($user, $item, $params)
{
return isset($params['Post']) ? $params['Post']->CreatedBy->id == $user : false;
}
}
更新:
答案 0 :(得分:1)
你正在做正确的事情,将对象传递给规则。
您确定Post
模型确实具有createdBy
属性吗?在您的另一段代码中,您有CreatedBy
。
很可能是拼写错误,或者您的Post
模型没有该字段,或者它的调用方式不同(created_by
?)
哦,还有一件事,如果CreatedBy
是关系,并且该对象不存在,尝试获取其属性(id
)将产生错误。尝试类似if (isset($params['Post']->CreatedBy) && $params['Post']->CreatedBy->id == $user)
的内容。