2014-03-25 49 views
0

我試圖在CGridView查看或顯示按鈕(CButtonColumn),在我/views/file/admin.php:Yii - 如何基於用戶級別隱藏/查看CGridview中的按鈕?

.... 
//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; 
} 
} 
?> 

我想這樣做的代碼,但「查看」按鈕不顯示,並且當我加載頁面時沒有錯誤。任何人都可以幫我解決這個問題嗎?非常感謝。

+1

嘗試將此語句放在逗號'Yii :: app() - > user-> getLevel()== 2'中。然後看看它是否工作 –

+0

我已經嘗試過它,但它引發錯誤'call_user_func_array()期望參數1是一個有效的回調,沒有數組或字符串給出' – gultomicro

+1

好吧你確定getLevel()返回正確的結果嗎? –

回答

2

您已經設置僅上線

'template'=>'{view}{update}{delete}', 'visible'=> (Yii::app()->user->getLevel()==1), 

列可視性管理員用戶,以便

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

沒有效果。刪除第一個設置visible。另外,作爲@soju和@Rafay說visible應該是一個PHP表達式,因此它應該閱讀

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

+1,但'visible'選項應該是一個PHP表達式。 http://www.yiiframework.com/doc/api/1.1/CButtonColumn#buttons-detail – soju

+0

我已經嘗試過了,但是它引發錯誤'call_user_func_array()期望參數1是一個有效的回調,沒有數組或字符串' – gultomicro

+0

好吧,抱歉我的誤解,謝謝它的作品100% – gultomicro

0

在你的情況需要延長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(); 
    } 

} 
+0

我應該在哪裏放這段代碼? – gultomicro

相關問題