2014-04-06 80 views
1

我想了解rbac中的授权,并与一些事情混淆了一下。授权与rbac yii

在我使用的角色,这样的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(); 

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

问题:

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

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

在使用rbac时,我们是否应该像在这里一样保持accesscontrol过滤器和规则?

我不知道我是否做得对。很多困惑和失落。请说明一下。干杯。

回答

2
  1. 他们没有被映射,在每个动作你需要检查这个手动

    if (Yii::app()->authManager->checkAccess('updatePost')) 
        thorow new HttpException(404); 
    
  2. 你可以创建IndexPost,ViewPost如果一些用户无法看到这些行动。

  3. 在accessControl中,只有在需要时才能检查用户是否已登录。

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

+0

还有一个问题,因为我在AccessControl的访问“角色”,并检查它为管理员,用户etc.I不明白我怎么了能够在这里访问角色。是因为rbac吗?我只认为users =>'@'可以在accessControl中访问。其他信息非常感谢。干杯 – user96675

+0

在过滤器中,您不需要使用rbac检查。 – Alex