简体   繁体   English

Yii - 如何根据用户级别隐藏/查看CGridview中的按钮?

[英]Yii - How to Hide/View Button in CGridview Based on User's Level?

I tried to view or show button (CButtonColumn) in CGridView, in my /views/file/admin.php: 我试图在我的/views/file/admin.php中查看或显示CGridView中的按钮(CButtonColumn):

....
//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 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 . 删除visible的第一个设置。 Also as @soju and @Rafay said visible should be a php expression therefore it should read 另外@soju和@Rafay说visible应该是一个php表达式因此它应该读取

    'view' => array(
        'visible'=> 'Yii::app()->user->getLevel()==2',
    ),

In your case you need to extend bootstrap.widgets.TbButtonColumn . 在您的情况下,您需要扩展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();
    }

}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM