wow6haka 2015-11-23 15:48:08 6757次浏览 2条评论 5 1 0

最近在做项目的时候,研究了半天 好像ActionColumn 只能在template和buttons里边来进行是否显示 view update delete 3大天王按钮,琢磨了下,觉得自带的做的不好,应该让用户在模板中可以根据规则来定义是否有权限来进行显示,代码其实挺简单,其实就是在initDefaultButtons中生成按钮的时候根据auth属性判断下,不过由于自己琢磨了挺长时间,所以发布出来,希望可以帮到有同样问题的小伙伴
ps:目前的无权限执行在显示层表现为图标还在,但是变为灰色,且不可点击,你也可以在initDefaultButtons中改为其他方式;
ps:template是第一层限制,是绝对控制,auth是第二层限制,属于权限控制

QQ截图20151123155202.png

方案:
在ActionColumn新增了auth属性,与buttons一样的玩法,同样是含有view update delete 3个元素的回调函数,函数中同样带有$url,$model,$key的参数,且默认都返回true 即有权限执行。

在gridview中使用方法:

['class' => 'common\components\ActionColumn',
				'template'=>'{update}{delete}',
				'auth'=>[
					'update'=>function($url, $model, $key){
						//这里任意判断,只要不return true,就会认为无权限
					}
				],
			],

代码:
记得放到common/components文件夹中


<?php
namespace common\components;

use Yii;
use Closure;
use yii\helpers\Html;
use yii\helpers\Url;


class ActionColumn extends \yii\grid\ActionColumn
{
	/**
	* auth和 buttons一样,都包含view update delete 3个元素,且都是回调函数
	* template 是第一层控制为完全是否显示,此为第二层是否有权限显示 
	* 这3个属性是可否操作,当不可操作的时候 会显示为灰色(详细见initDefaultButtons)
	*/
	
	public $auth=[];
    

    /**
     * @inheritdoc
     */
    public function init()
    {
        parent::init();
        $this->initDefaultAuth();
		$this->initDefaultButtons();
    }

    /**
     * Initializes the default button rendering callbacks.
     */
    protected function initDefaultAuth()
    {
        if (!isset($this->auth['view'])) {
            $this->auth['view'] = function ($url, $model, $key) {
                return true;
            };
        }
        if (!isset($this->auth['update'])) {
            $this->auth['update'] = function ($url, $model, $key) {
                return true;
            };
        }
        if (!isset($this->auth['delete'])) {
            $this->auth['delete'] = function ($url, $model, $key) {
               return true;
            };
        }
    }
	
	/**
     * Initializes the default button rendering callbacks.
     */
    protected function initDefaultButtons()
    {
        if (!isset($this->buttons['view'])) {
            $this->buttons['view'] = function ($url, $model, $key) {
				$auth_class='';
				if(call_user_func($this->auth['view'], $url, $model, $key)!==true)
				{
					$url='javascript:void(0)';
					$auth_class='text-muted';
				}
                $options = array_merge([
                    'title' => Yii::t('yii', 'View'),
                    'aria-label' => Yii::t('yii', 'View'),
                    'data-pjax' => '0',
                ], $this->buttonOptions);
                return Html::a('<span class="glyphicon glyphicon-eye-open '.$auth_class.'"></span>', $url, $options);
            };
        }
        if (!isset($this->buttons['update'])) {
            $this->buttons['update'] = function ($url, $model, $key) {
				$auth_class='';
				if(call_user_func($this->auth['update'], $url, $model, $key)!==true)
				{
					$url='javascript:void(0)';
					$auth_class='text-muted';
				}
                $options = array_merge([
                    'title' => Yii::t('yii', 'Update'),
                    'aria-label' => Yii::t('yii', 'Update'),
                    'data-pjax' => '0',
                ], $this->buttonOptions);
                return Html::a('<span class="glyphicon glyphicon-pencil gray-dark '.$auth_class.'"></span>', $url, $options);
            };
        }
        if (!isset($this->buttons['delete'])) {
            $this->buttons['delete'] = function ($url, $model, $key) {
				$auth_class='';
				if(call_user_func($this->auth['delete'], $url, $model, $key)!==true)
				{
					$url='javascript:void(0)';
					$auth_class='text-muted';
				}
                $options = array_merge([
                    'title' => Yii::t('yii', 'Delete'),
                    'aria-label' => Yii::t('yii', 'Delete'),
                    'data-confirm' => Yii::t('yii', 'Are you sure you want to delete this item?'),
                    'data-method' => 'post',
                    'data-pjax' => '0',
                ], $this->buttonOptions);
                return Html::a('<span class="glyphicon glyphicon-trash '.$auth_class.'"></span>', $url, $options);
            };
        }
    }

 
}
觉得很赞
  • 评论于 2015-11-25 10:07 举报

    不错 但是我也可以这样做

    $template = '';  
    if(有更新权限){  
       $template.= '{update}'  
    }  
    if(有删除权限){  
       $template.= '{delete}'  
    }  
    'template'=> $template,  
    
    2 条回复
    评论于 2015-11-25 11:51 回复

    嗯,是的。一开始一般都会考虑这么做。其实我是不知道在grid的组件中的ActionColumn的初始化配置时,如何调用到$model变量,所以才在ActionColumn动手。。不过想来改代码也是有点好处的,后期配置rbac,直接就在ActionColumn处理了,直接省过模板。

    评论于 2015-12-14 11:14 回复
  • 评论于 2015-12-01 20:24 举报

    显示不显示很重要吗?没权限不能操作不就够了!

    1 条回复
    评论于 2015-12-15 19:14 回复

    啊啊啊啊啊

您需要登录后才可以评论。登录 | 立即注册