Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/visual-studio-code/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Authentication Yii基于角色的访问,管理自己的帖子_Authentication_Yii_Rbac_Role Base Authorization - Fatal编程技术网

Authentication Yii基于角色的访问,管理自己的帖子

Authentication Yii基于角色的访问,管理自己的帖子,authentication,yii,rbac,role-base-authorization,Authentication,Yii,Rbac,Role Base Authorization,我在谷歌上搜索、阅读教程、博客,还做了很多实验。因此,我能够定义对控制器操作的基于角色的访问。 一切正常。 我想问的是。如何编写规则来显示、编辑和删除用户自己的帖子 默认情况下,它显示所有post。但是,我们可以将数据提供程序标准设置为显示自己的帖子。但是我怎样才能控制积垢呢?? 请帮帮我。我的密码是贝娄 public function accessRules() { return array( array('allow', // allow all u

我在谷歌上搜索、阅读教程、博客,还做了很多实验。因此,我能够定义对控制器操作的基于角色的访问。 一切正常。 我想问的是。如何编写规则来显示、编辑和删除用户自己的帖子

默认情况下,它显示所有post。但是,我们可以将数据提供程序标准设置为显示自己的帖子。但是我怎样才能控制积垢呢?? 请帮帮我。我的密码是贝娄

 public function accessRules() {
        return array(
            array('allow', // allow all users to perform 'index' and 'view' actions
                'actions' => array('index', 'view'),
                'users' => array('*'),
            ),
            array('allow', // allow authenticated user to perform 'create' and 'update' actions
                'actions' => array('create', 'update'),
                'expression' => 'Yii::app()->controller->HaveAccess()',
                //'users' => array('@'),
            ),
            array('allow', // allow admin user to perform 'admin' and 'delete' actions
                'actions' => array('admin', 'delete'),
                'expression' => 'Yii::app()->controller->HaveAccess()',
            ),
            array('deny', // deny all users
                'users' => array('*'),
            ),
        );
    }
对于后期显示:

 public function actionIndex() {
        $dataProvider = new CActiveDataProvider('Advertisment');
        if (!$this->IsAdmin()) {
            $dataProvider = new CActiveDataProvider('Advertisment', array(
                        'criteria' => array(
                            'condition' => 'added_by='.$this->userId,
                            'order' => 'id DESC',
                        ),
                        'pagination' => array(
                            'pageSize' => 20,
                        ),
                    ));
        }
        $this->render('index', array(
            'dataProvider' => $dataProvider,
        ));
    }

要将更新和删除操作限制到用户自己的帖子,您必须检查控制器操作内的权限(这在控制器的
accessRules
afaik中是不可能的,因为在计算
accessRules
时,要检查权限的帖子的id是未知的。)

例如:

public function actionUpdate($id){
    $model = $this->loadModel($id);
    if($model->added_by === $this->userId){
        // your code here
    }else
        throw new CHttpException(401,'You are not authorized to edit this post.');
}

我能想到的唯一方法是首先修改激活操作的链接:update、delete、view以发送
added\u by
字段以及帖子id(默认情况下发送帖子id)。然后在您的
“表达式”
中,您可以检查由
添加的
是否与您的
用户ID匹配。下面是一个视图示例(假设您在问题中所说的“显示”是指视图):

  • accessRule修改,要使其生效,请确保在计算accessRules之前,在
    $this->userId
    中有一个值,该值可以通过以下方法完成:

  • 查看修改,将
    added\u by
    id添加到url参数。假设您有默认生成的crud视图,即index.php、_view.php、_form.php、_view.php、update.php等,_view.php有指向文章详细视图的链接,这也是index.php中listview的itemView。这里我们做了一些更改(_view.php):

    
    :
    
    :
  • 您必须修改删除、更新操作的链接,以同时传递添加的_by字段

  • 使用范围:

    在您的模型中:

    public function scopes()
    {
    return array(
      'own'=>array(
        'condition'=>'userid=' . Yii::app()->user->id,
      ),
    );
    }
    
    然后像这样选择您的数据:

    Post::model()->own()->findAll();
    

    只需将userid添加到这些操作中,就像您对acitonidex
    $this->userid
    所做的一样,您在控制器中的何处设置了
    $this->userid
    值?但我在某个地方看到,我们可以在accessrul中添加一些内容,并自动为用户修改列表。您是否引用了此内容?
    public function scopes()
    {
    return array(
      'own'=>array(
        'condition'=>'userid=' . Yii::app()->user->id,
      ),
    );
    }
    
    Post::model()->own()->findAll();