我用谷歌搜索,阅读教程,博客并进行了很多实验。所以我能够定义对控制器操作的基于角色的访问。一切正常。我想问的是。如何编写规则来显示、编辑和删除用户自己的帖子?
默认情况下,它显示所有帖子。但是,我们可以将数据提供者标准设置为显示自己的帖子。但是我怎样才能控制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,
));
}