I tried to view or show button (CButtonColumn) in CGridView, in my /views/file/admin.php:
....
//getLevel()==1 means ADMIN, getLevel==2 means common users
array(
'class'=>'bootstrap.widgets.TbButtonColumn',
'template'=>'{view}{update}{delete}', 'visible'=> (Yii::app()->user->getLevel()==1),
'deleteConfirmation'=>"js: 'Are you want to delete '+$(this).parent().parent().children(':first-child').text()+ '?'",
//I tried to modify with this code below, but there's nothing happens, 'view' button not
//display when I access as common user (getLevel()==2)
'buttons'=>array(
'view' => array(
'visible'=> Yii::app()->user->getLevel()==2,
),
)
),
....
WebUser.php
<?php
class WebUser extends CWebUser{
protected $_model;
protected function loadUser()
{
if ( $this->_model === null ) {
$this->_model = User::model()->findByPk($this->id);
}
return $this->_model;
}
function getLevel()
{
$user=$this->loadUser();
if($user)
return $user->id_level;
return 100;
}
}
?>
I tried to do that code, but there 'view' button not display, and there's no error when I load the page. Can anyone help me solve this problem? Thanks a lot.
You are already setting the column visibility to admin users only on the line
'template'=>'{view}{update}{delete}', 'visible'=> (Yii::app()->user->getLevel()==1),
so
'view' => array(
'visible'=> Yii::app()->user->getLevel()==2,
),
has no effect. Remove the first setting for visible
. Also as @soju and @Rafay said visible
should be a php expression therefore it should read
'view' => array(
'visible'=> 'Yii::app()->user->getLevel()==2',
),
In your case you need to extend bootstrap.widgets.TbButtonColumn
.
Yii::import('zii.widgets.grid.CButtonColumn');
class EButtonColumnWithRightsCheck extends CButtonColumn{
public function init() {
//{view} {delete} {update}
$permissions = array();
// Client.User.View
$permissions['view'] = Yii::app()->user->checkAccess(ucfirst($this->grid->controller->module->id) . '.' . ucfirst($this->grid->controller->id) . '.View');
$permissions['delete'] = Yii::app()->user->checkAccess(ucfirst($this->grid->controller->module->id) . '.' . ucfirst($this->grid->controller->id) . '.Delete');
$permissions['update'] = Yii::app()->user->checkAccess(ucfirst($this->grid->controller->module->id) . '.' . ucfirst($this->grid->controller->id) . '.Update');
foreach ($permissions as $action => $permission) {
if ($permission === false) {
$this->template = str_replace('{' . $action . '}', '', $this->template);
}
}
// call parent to initialize other buttons
parent::init();
}
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.