【问题标题】:Authorization with rbac yii使用 rbac yii 授权
【发布时间】:2014-04-06 13:36:34
【问题描述】:

我正在尝试了解 rbac 中的授权,但对一些事情感到有些困惑。

在 accessControl 规则中,我使用了这样的角色:

return array(
                    array('allow',  // allow all users to perform 'index' and 'view' actions
                            'actions'=>array('index', 'view'),
                            'roles'=>array('user'),
                    ),
                    array('allow', // allow authenticated user to perform 'create' and 'update' actions
                            'actions'=>array('create','update'),
                            'roles'=>array('author'),
                    ),
                    array('allow', // allow admin user to perform 'admin' and 'delete' actions
                            'actions'=>array('admin','delete'),
                             'roles'=>array('admin'),
                    ),
                    array('deny',  // deny all users
                            'users'=>array('*'),
                    ),
            );

我也在使用以下设置:

    $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');        


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

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

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


    $auth->assign('user', 3);
    $auth->assign('author', 2);
    $auth->assign('admin', 1);

    $auth->save();

有 4 种不同的操作名称(createPost、deletePost、readPost、udpatePost)。但是在控制器中,动作名称是不同的,例如 actionIndex、actionView、actionCreate、actionDelete、actionUpdate 和 actionAdmin。

问题:

如何将操作映射到控制器操作。

是否应该创建更多操作,例如 IndexPost、ViewPost 等?

在使用 rbac 时,我们是否仍应像我在这里所做的那样保留访问控制过滤器和规则?

我不确定我是否做得对。很多困惑和迷失。请阐明一些观点。干杯。

【问题讨论】:

    标签: yii


    【解决方案1】:
    1. 它们没有被映射,在每个操作中您需要手动检查它

      if (Yii::app()->authManager->checkAccess('updatePost'))
          thorow new HttpException(404);
      
    2. 如果某些用户看不到这些操作,您可以创建 IndexPost、ViewPost。

    3. 在 accessControl 中,您可以仅在需要时检查用户是否已登录。

    4. 欲了解更多信息,请查看这篇文章:Simple RBACGetting to Understand Hierarchical RBAC Scheme

    【讨论】:

    • 再问一个问题,因为我在 accessControl 中访问“角色”并将其作为管理员、用户等进行检查。我不明白我如何能够在这里访问角色。是不是因为rbac。我只认为用户 => '@' 可以在 accessControl 中访问。非常感谢您提供的其余信息。干杯
    • 在过滤器中你不需要使用 rbac 检查。
    猜你喜欢
    • 2012-10-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-03-25
    • 2013-04-22
    • 2020-07-23
    相关资源
    最近更新 更多