specialnot 2015-10-13 11:12:14 7053次浏览 1条评论 4 2 0
  • 权限节点名称为 controllerid.''.action_id

1、控制器

<?php
namespace backend\controllers;

use common\models\NodeForm;

class NodeController extends BaseController{
      #权限管理首页
     public function actionIndex(){
        $authManager = \Yii::$app->authManager;
        $nodes = $authManager->getPermissions();

        return $this->render('index',[
            'nodes'=>$nodes,
        ]);
    }
    #创建权限
    public function actionCreate(){
        $model = new NodeForm();

        if($model->load(\Yii::$app->request->post()) && $model->save()){
            \Yii::$app->session->setFlash('success','节点['.$model->name.']添加成功');
            return $this->redirect(['/node/index']);
        }else{
            return $this->render('create',[
                'model'=>$model,
            ]);
        }
    }
    #更新权限
    public function actionUpdate($name){
        $authManager = \Yii::$app->authManager;
        $child = $authManager->getChildren($name);
        if($child){
            \Yii::$app->session->setFlash('success','节点['.$name.']有子节点,不能修改');
            return $this->redirect(['/node/index']);
        }

        $node = $authManager->getPermission($name);
        if(!$node) return false;
        $model = new NodeForm();
        $model->name = $node->name;
        $model->description = $node->description;

        if($model->load(\Yii::$app->request->post()) && $model->update($name)){
            \Yii::$app->session->setFlash('success','节点['.$name.']修改成功');
            return $this->redirect(['/node/index']);
        }else{
            return $this->render('update',[
                'model'=>$model,
            ]);
        }
    }
    #删除权限
    public function actionDelete($name){
        $authManager = \Yii::$app->authManager;
        $child = $authManager->getChildren($name);
        if($child){
            \Yii::$app->session->setFlash('success','节点['.$name.']有子节点,不能删除');
            return $this->redirect(['/node/index']);
        }
        $node = $authManager->getPermission($name);
        if(!$node) return false;
        if($authManager->remove($node)){
            \Yii::$app->session->setFlash('success','节点['.$name.']删除成功');
        }else{
            \Yii::$app->session->setFlash('error','节点['.$name.']删除失败');
        }
        return $this->redirect(['/node/index']);
    }

}

2、视图

2.1. node/index.php

<?php

use yii\helpers\Html;
use yii\grid\GridView;


$this->title = '权限节点';
$this->params['breadcrumbs'][] = $this->title;
?>
<div class="node-index">


    <p>
        <?= Html::a('新建节点', ['create'], ['class' => 'btn btn-success']) ?>
    </p>
    <table class="table table-striped">
        <thead>
            <tr>
                <th>节点名称</th>
                <th>节点描述</th>
                <th>操 作</th>
            </tr>
        </thead>
        <tbody>
        <?php foreach($nodes as $v){?>
            <tr>
                <td><?= $v->name?></td>
                <td><?= $v->description?></td>
                <td style="width: 60px;">
                    <?= Html::a('<span class="glyphicon glyphicon-pencil"></span>',['/node/update','name'=>$v->name])?>
                    &nbsp;
                    <?= Html::a('<span class="glyphicon glyphicon-trash"></span>',['/node/delete','name'=>$v->name],[
                        'data' => [
                            'confirm' => '确认删除吗?',
                            'method' => 'post',
                        ]
                    ])?>
                </td>
            </tr>
        <?php }?>
        </tbody>
    </table>


</div>

2.2. node/create.php

<?php
    use yii\widgets\ActiveForm;
    use yii\helpers\Html;

    $this->title = '创建节点';
    $this->params['breadcrumbs'][] = ['label'=>'节点','url'=>['/node/index']];
    $this->params['breadcrumbs'][] = $this->title;
?>
<div class="row">
    <?php $form=ActiveForm::begin()?>
        <div class="col-md-6">
            <?= $form->field($model,'name')->textInput(['maxlength'=>true])->hint('节点名称由小写字母开头,3-20位字符(小写字母-_)组成')?>
        </div>
        <div class="col-md-8">
            <?= $form->field($model,'description')->textarea(['rows'=>3])?>
        </div>
        <div class="col-md-6">
            <?= $form->field($model,'parent')->textInput(['maxlength'=>true])->hint('节点名称由小写字母开头,3-20位字符(a-z-_)组成')?>
        </div>
        <div class="col-md-8">
            <?=Html::submitButton((Yii::$app->controller->action->id == 'create')?'新建':'更新',['class'=>'btn btn-primary'])?>
        </div>
    <?php ActiveForm::end()?>
</div>

2.3. node/update.php

<?php
    use yii\widgets\ActiveForm;
    use yii\helpers\Html;

    $this->title = '更新节点';
    $this->params['breadcrumbs'][] = ['label'=>'节点','url'=>['/node/index']];
    $this->params['breadcrumbs'][] = $this->title;
?>
<div class="row">
    <?php $form=ActiveForm::begin()?>
        <div class="col-md-6">
            <?= $form->field($model,'name')->textInput(['maxlength'=>true,'value'=>$model->name])->hint('节点名称由小写字母开头,3-20位字符(小写字母-_)组成')?>
        </div>
        <div class="col-md-8">
            <?= $form->field($model,'description')->textarea(['rows'=>3,'value'=>$model->description])?>
        </div>
        <div class="col-md-6">
            <?= $form->field($model,'parent')->textInput(['maxlength'=>true,'parent'=>$model->parent])->hint('节点名称由小写字母开头,3-20位字符(a-z-_)组成')?>
        </div>
        <div class="col-md-8">
            <?=Html::submitButton((Yii::$app->controller->action->id == 'create')?'新建':'更新',['class'=>'btn btn-primary'])?>
        </div>
    <?php ActiveForm::end()?>
</div>

3、模型

<?php

namespace common\models;

use Yii;
use yii\base\Model;
use yii\helpers\Html;

class NodeForm extends Model
{
    public $name;
    public $description;
    public $parent;

    public function rules(){
        return [
            [['name','parent'],'string','max'=>20],
            [['name','description'],'required','message'=>'name属性不能为空'],
            ['name','match','pattern'=>'/^[a-z][a-z-_]{2,20}$/','message'=>'name属性不合法'],
            ['parent','match','pattern'=>'/^[a-z-_][a-z-_]{2,20}$/','message'=>'parent属性不合法'],
            ['parent','validateParent'],
            ['description','filter','filter'=>function($value){
                return Html::encode($value);
            }],
        ];
    }

    public function validateParent($attribute,$params){
        if(!$this->hasErrors()){
            $authManager = Yii::$app->authManager;
            $node = $authManager->getPermission($this->parent);
            if(empty($node)){
                $this->addError($attribute,'上级节点不存在');
            }
        }
    }

    public function attributeLabels(){
        return [
            'name'=>'节点名称',
            'description'=>'节点描述',
            'parent'=>'父级节点',
        ];
    }

    public function save(){
        if($this->validate()){
            $authManager = Yii::$app->authManager;
            $node = $authManager->createPermission($this->name);
            $node->description = $this->description;
            $authManager->add($node);

            if(!empty($this->parent)){
                $parent = $authManager->getPermission($this->parent);
                $authManager->addChild($parent,$node);
            }
            return true;
        }else{
            return false;
        }
    }

    public function update($name){
        if($this->validate()){
            $authManager = Yii::$app->authManager;
            $node = $authManager->getPermission($name);
            if(!$node) return false;
            $authManager->remove($node);

            $node = $authManager->createPermission($this->name);
            $node->description = $this->description;
            $authManager->add($node);
            if(!empty($this->parent)){
                $parent = $authManager->getPermission($this->parent);
                $authManager->addChild($parent,$node);
            }
            return true;
        }
        return false;
    }

  
}
觉得很赞
  • 评论于 2016-01-21 10:38 举报

    baseController是什么

    4 条回复
    评论于 2016-01-21 10:47 回复

    对基本权限、过滤之类公共部分进行的封装

    评论于 2016-03-19 16:08 回复

    baseController里面是什么内容啊

    评论于 2016-03-21 10:11 回复

    BaseController中是对用户权限的判断,还有其他东西。权限可以是超级管理员权限、登陆未登录权限、以及分配的权限,甚至是可以根据需求自定义权限

    评论于 2016-03-21 19:04 回复

    哦,好的。谢谢

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