Access to Yii-based roles, administering your own messages

I googled, read tutorials, blogs and experimented a lot. Therefore, I can determine role-based access to controller actions. Everything is working fine. I want to ask. How can I write a rule to display, edit and delete a user's own message?

By default, all messages are displayed. However, we can set the criteria for the data provider to display our own message. But how can I control CRUD for this? Please help me. My code is below.

 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('*'),
            ),
        );
    }

to display messages:

 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,
        ));
    }
+5
source share
3 answers

, ( accessRules afaik, , 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.');
}
+3

:

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();
+1

, , - , : , , , added_by ( ). 'expression' , added_by userId. ( , ):

  • 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('*'),
        ),
    );
    
  • , added_by id url. , crud, index.php, _view.php, _form.php, _view.php, update.php .., _view.php , , itemView index.php. (_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>
    
  • , , added_by.

0

All Articles