我已经yii2mod/yii2-rbac
从这个 url 安装了 - https://github.com/yii2mod/yii2-rbac in yii2-basic。
除了使用/允许所有者数据外,一切正常。
从这个链接:https ://www.yiiframework.com/doc/guide/2.0/en/security-authorization
我在根目录rbac
和文件AuthorRule.php
和代码中创建了一个文件夹:
namespace app\rbac;
use yii\rbac\Rule;
//use app\models\Post;
/**
* Checks if authorID matches user passed via params
*/
class AuthorRule extends Rule
{
/**
* @var string
*/
public $name = 'isAuthor';
/**
* @param string|int $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 bool 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 == $user : false;
}
}
但是当我尝试在权限中添加规则时(AuthorRule
或者isAuthor
在我创建 updateOwnRecord 的权限下,我收到错误,规则不存在。
我在这里缺少什么?