首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Yii2 - RBAC规则允许/查看自己的数据

Yii2 - RBAC规则允许/查看自己的数据
EN

Stack Overflow用户
提问于 2019-09-03 21:25:27
回答 1查看 1.6K关注 0票数 0

我已经安装了yii2mod/yii2-rbac从这个url - https://github.com/yii2mod/yii2-rbac在yii2-basic。

除了使用/允许所有者数据之外,一切都很正常。

从这个链接:https://www.yiiframework.com/doc/guide/2.0/en/security-authorization,我在根rbac和文件AuthorRule.php中创建了一个文件夹,并编写了以下代码:

代码语言:javascript
复制
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,我得到了错误,规则就不存在了)。

我在这里错过了什么?

EN

回答 1

Stack Overflow用户

发布于 2019-09-04 22:39:55

但是,当我尝试在权限中添加规则时( AuthorRule或isAuthor在我创建的权限下创建了updateOwnRecord ),我得到的是错误,规则不存在

由于没有相关的代码,所以不确定您在哪里得到了您提到的错误,但是查看您的详细信息,我发现您还没有正确地理解这个过程。

  • updatePost中创建权限auth_item
  • AuthorRule类的序列化实例添加到auth_rule表中。
  • 创建一个新的权限updateOwnPost并指定规则名,即isAuthor
  • 将权限updatePost作为子权限添加到auth_item_child表中的UpdateOwnPost中。
    • isAuthor将是您将提供给updateOwnPost权限的rule_name列的规则的名称。

  • updatePost添加为您希望使用该规则的role的子级,如为标准用户角色创建的user或其他任何角色。

请参阅下面的代码,您现在可以通过任何临时操作运行它,稍后我们将在下面的答案中讨论它的位置。

代码语言:javascript
复制
$auth = Yii::$app->authManager;
$updatePost = $auth->getPermission('updatePost');

//change it to whichever role you want to assign it like `user` `admin` or any other role
$role = $auth->getRole('user');

// add the rule
$rule = new \app\rbac\AuthorRule;
$auth->add($rule);

// add the "updateOwnPost" permission and associate the rule with it.
$updateOwnPost = $auth->createPermission('updateOwnPost');
$updateOwnPost->description = 'Update own post';
$updateOwnPost->ruleName = $rule->name;
$auth->add($updateOwnPost);

// "updateOwnPost" will be used from "updatePost"
$auth->addChild($updateOwnPost, $updatePost);

// allow "author" to update their own posts
$auth->addChild($role, $updateOwnPost);

现在,如果一切顺利,并且可以通过运行上面的代码来添加规则

请记住,您需要检查updateOwnPost Yii::$app->user->can() 而不是中的Yii::$app->user->can()规则,并将Post模型实例作为第二个参数传递。

像这样

代码语言:javascript
复制
if (\Yii::$app->user->can('updatePost', ['post' => $post])) {
    // update post
}

关于当前应用程序中的代码放置

如果您希望有一个单独的接口,可以在其中添加create和一个表单,那么您可以按照已经可用的dektrium-rbac代码来提供完整的crud,您可以根据自己的需求使用crud。

有关参考资料,请参阅下面

注意:如果您有很多控制器,并且希望将此规则与控制器内的每个更新操作相关联(假设所有相关模型都有 console\Controller 字段),那么您可以使用console\Controller并通过控制台运行这些进程,这样每个新的 controller/update 都可以与循环中重复上述过程的规则相关联。有关basic-app中控制台控制器的使用情况,请参阅 https://stackoverflow.com/questions/29090479/can-you-run-console-jobs-from-yii2-basic

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/57778924

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档