Custom views for cgridview based on role

Hey, i’m new to yii and in my project after login i should redirect to manage page and based on the role (ie. admin and user) he should see custom views for cgridview.

for admin he should see all entries made by user.

for user he should see only his entries.

thnx for any help in advance!


It is easy to achieve

goto your search method inside model

		if(!Yii::app()->user->isAdmin()) {

			$ids = array();

			$list = Yii::app()->db->createCommand()->select('id')->from('tbl_entries')

			->where('user_id = :userId', array(':userId' => Yii::app()->user->id))->queryAll();

			foreach($list as $i=>$row) {

				$ids[] = $row['id'];


			$criteria->addInCondition('id', $ids);

		}else {

			$list = Yii::app()->db->createCommand()->select('id')->from('tbl_entries')->queryAll();

			foreach($list as $i=>$row) {

				$ids[] = $row['id'];


			$criteria->addInCondition('id', $ids);



If you are not admin

get the records only belong to logged in user

if you are admin

getallrecords belongs to all users

Another way to direct filter userwise listing form CGridView

Add below code in View file:


 $useriId = ''; 

 if(!Yii::app()->user->isAdmin()) {

         $useriId = Yii::app()->user->id;


$this->widget('zii.widgets.grid.CGridView', array(



Make changes in Model file for Search method:

public function search($useriId = '')


                // Warning: Please modify the following code to remove attributes that

                // should not be searched.

                $criteria=new CDbCriteria;

                if ($useriId != '') {

                        $criteria->condition = 't.user_id = ' . $useriId;



Hope this helps!