4

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

默认情况下,它显示所有帖子。但是,我们可以将数据提供者标准设置为显示自己的帖子。但是我怎样才能控制CRUD呢?请帮助我。我的代码如下。

 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,
        ));
    }
4

3 回答 3

3

要将更新和删除操作限制为用户自己的帖子,您必须检查控制器操作中的权限(这在控制器的accessRulesafaik 中是不可能的,因为要检查权限的帖子的 ID,在accessRules被评估。)

例子:

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.');
}
于 2012-06-26T15:38:01.070 回答
1

使用范围:

http://www.yiiframework.com/doc/api/1.1/CActiveRecord#scopes-detail

在您的模型中:

public function scopes()
{
return array(
  'own'=>array(
    'condition'=>'userid=' . Yii::app()->user->id,
  ),
);
}

然后像这样选择您的数据:

Post::model()->own()->findAll();
于 2014-10-28T04:45:54.143 回答
0

我能想到的唯一方法是首先修改激活操作的链接:更新、删除、查看以发送该added_by字段以及帖子的 ID(默认情况下发送帖子 ID)。然后在你的'expression'你可以检查是否added_by匹配你的userId. 以下是视图示例(假设您在问题中说显示时是指视图):

  1. accessRule 修改,为此请确保您在$this->userId评估 accessRules 之前有一个值,这可以在init()方法中完成:

    return array(
        array('allow', // allow all users to perform 'index' actions
            'actions' => array('index'),
            'users' => array('*'),
        ),
        array('allow', // allow authenticated user to perform 'create' and 'update' actions
            'actions' => array('create', 'admin'),
            'expression' => 'Yii::app()->controller->HaveAccess()',
            //'users' => array('@'),
        ),
        array('allow', // allow admin user to perform 'admin' and 'delete' actions
            'actions' => array('view', 'delete', 'update'),
            'expression' => 'Yii::app()->controller->HaveAccess() && ($_REQUEST["added_by"]=='.$this->userId.")",
        ),
        array('deny', // deny all users
            'users' => array('*'),
        ),
    );
    
  2. 查看修改以将added_byid 添加到 url 参数。假设您有默认生成的 crud 视图,即index.php、_view.php、_form.php、_view.php、update.php 等,其中_view.php具有转到帖子详细视图的链接,这也是index.php 中列表视图的 itemView。在这里我们进行一些更改(_view.php):

    <div class="view">
    
       <b><?php echo CHtml::encode($data->getAttributeLabel('id')); ?>:</b>
       <?php echo CHtml::link(CHtml::encode($data->id), array('view', 'id'=>$data->id,'added_by'=>$data->added_by)); ?>
       <br />
    
       <b><?php echo CHtml::encode($data->getAttributeLabel('someattribute')); ?>:</b>
       <?php echo CHtml::encode($data->someattribute); ?>
       <br />
    
       <!-- more attributes-->
    
    
    </div>
    
  3. 您必须修改删除链接、更新操作以传递 added_by 字段。

于 2012-06-27T10:44:56.627 回答