將所有action統一放在設定檔中XXX.php,將返回合并到params中:
return ['params' => ['rules' => [['n' => '許可權測試(一級菜單)','i' => 'fa fa-circle-o', 'tree' => ['perm1','perm2'], 's'=>[
['n' => '許可權測試(二級菜單)', 'c' => 'perm1','m' => 'index','i' => 'fa fa-circle-o','s'=>[
['n'=>'建立','m'=>'create','i'=>'fa fa-plus'],
['n'=>'查看)','m'=>'view','i'=>'fa fa-eye'],
['n'=>'更新','m'=>'update','i'=>'fa fa-pencil'],
]
],
['n' => '許可權測試(二級菜單)', 'c' => 'perm2','m' => 'index','i' => 'fa fa-circle-o','s'=>[
['n'=>'建立','m'=>'create','i'=>'fa fa-plus'],
['n'=>'查看)','m'=>'view','i'=>'fa fa-eye'],
['n'=>'更新','m'=>'update','i'=>'fa fa-pencil'],
]
],
]
],]]];
controller層的action主要處理:
$model = Admin::find()->where(['adminId' => $id])->one();
$post = Yii::$app->request->post();
if ($model->load($post)) {
$ret = [];
if(!empty($post['Admin']['rules'])){
$ret = explode(',',$post['Admin']['rules']);
}
$model->rules = serialize($ret);
if($model->save()){
return $this->redirect(['index']);
}
}
$rules = empty($model->rules)?[]:unserialize($model->rules);
return $this->render('perm', [
'model' => $model,
'rules' => $rules,
]);
視圖層檔案perm.php
$this->title = '使用權限設定 '.$model->userName;
$this->params['breadcrumbs'][] = ['label' => '管理員列表', 'url' => ['index']];
$this->params['breadcrumbs'][] = $this->title;
$menu = [];
if(isset(Yii::$app->params['rules']) and count(Yii::$app->params['rules'])>0){
foreach(Yii::$app->params['rules'] as $rule){
$menuItems = [
'text' => $rule['n'],
'icon' => $rule['i'],
'id' => "",
'state' => ['opened'=>true],
'children' =>[]
];
$menuItems["children"][] = [
];
foreach($rule['s'] as $child)
{
$children = [];
if(isset($child['tree']))
{
foreach($child["s"] as $second)
{
$secondChild = [];
$url = '/'.$second['c'].'/'.$second['m'];
$secondChild[] = [
'text' => $second['n'],
'icon' => $second['i'],
'id' => 'm_'.$url,
'children' => $secondChild,
'state' =>['checked'=>in_array($url, $rules) ? true : false],
];
// 第二級菜單處理
foreach($second['s'] as $secondSon)
{
$url = "/" . $second['c'] . '/' .$secondSon['m'];
$secondChild[] = [
'text' => $secondSon['n'],
'icon' => $secondSon['i'],
'id' => 'l_'.$url,
'children' => [],
'state' =>['checked'=>in_array($url, $rules) ? true : false],
];
}
$children[] = [
'text' => $second['n'],
'icon' => $second['i'],
'id' => 'm_'.$url,
'children' => $secondChild,
'state' =>['checked'=>in_array($url, $rules) ? true : false],
];
}
}
else
{
// 二級菜單處理
$url = '/'.$child['c'].'/'.$child['m'];
$children[] = [
'text' => $child['n'],
'icon' => $child['i'],
'id' => 'm_'.$url,
'children' => $children,
'state' =>['checked'=>in_array($url, $rules) ? true : false],
];
foreach($child['s'] as $son)
{
$url = "/" . $child['c'] . '/' .$son['m'];
$children[] = [
'text' => $son['n'],
'icon' => $son['i'],
'id' => 'l_'.$url,
'children' => [],
'state' =>['checked'=>in_array($url, $rules) ? true : false],
];
}
}
$menuItems['children'][] = [
'text' => $child['n'],
'icon' => $child['i'],
'id' => 'c_'.$url,
'children' => $children
];
unset($children);
unset($selfItem);
}
$menu[] = $menuItems;
unset($menuItems);
}
}
?>
<div class="row">
<div class="col-md-12">
<?php $form = ActiveForm::begin(['options'=>['class'=>'form-vertical form-perm','id'=>'perm_form']]); ?>
<div class="box-body">
<?=\talma\widgets\JsTree::widget([
'name' => 'jstree',
'core' => ['data'=>$menu,
'theme' => ['responsive'=>false]
],
'checkbox' => [
'tie_selection' => false
],
'plugins' => [ 'checkbox'],
]);
?>
<?= $form->field($model, 'rules')->hiddenInput()->label(false); ?>
</div>
<?php if (!Yii::$app->request->isAjax){ ?>
<div class="box-footer">
<?=Html::a('<i class="fa fa-save"></i> 儲存','javascript:submitMe()',['class' => 'btn btn-warning']); ?>
</div>
<?php } ?>
<?php ActiveForm::end(); ?>
</div>
</div>
<script type="text/javascript">
function submitMe() {
var $form = $('#perm_form');
$form.on('submit', function(e) {
return $form.yiiActiveForm('submitForm');
});
$('#admin-rules').val(getJsTreeIds());
$form.submit();
}
var getJsTreeIds = function() {
var list = $.jstree.reference(jsTree_w0).get_checked();
var ids = new Array();
$.each(list, function(idx, val) {
if(val.indexOf('m_') != -1) {
ids.push(val.substring(2));
}
if(val.indexOf('l_') != -1) {
ids.push(val.substring(2));
}
});
return ids.toString();
};
</script>
jsTree版本: "thiagotalma/yii2-jstree": "~1.0.0"
判斷許可權關鍵代碼,主要是繼承yii\web\Controller類,然後重寫actions(yii\web\Controller繼承了yii\base\Controller)函數:
$pathInfo = Yii::$app->request->pathInfo;
if (!\Yii::$app->user->isGuest)
{
if(使用者為非超級管理員)
{
if ($pathInfo !='XXX/logout' && $pathInfo !='XXXX/login')
{
list($rules, $trees) = \common\service\LAdminService::getAdminRules();
if ($pathInfo != "XXXX/denied" && !in_array("/" .$pathInfo, $rules))
{
// 許可權不夠,則跳轉到無許可權操作提示頁面
return $this->redirect("/XXXXX/denied");
}
}
}
}
else
{
if($pathInfo !='XXXX/login')
{
return $this->redirect("/XXXX/login");
}
}