yii rbac管理

Posted 一个木头人

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了yii rbac管理相关的知识,希望对你有一定的参考价值。

以下是Controller代码

<?php
/**
 * Created by PhpStorm.
 * User: zhoukang
 * Date: 2017/6/11
 * Time: 19:31
 */

namespace frontend\controllers;

use yii\web\Controller;
use frontend\models\Rbac;
use yii;
use frontend\models\Loginform;

class RbacController extends Controller
{
        public function actionIndex(){
            return $this->render(index,[message=>您正在使用的是权限控制]);
        }
    //创建角色
        public function actionCreaterole(){
            $model = new Rbac();
            return $this->render(role,[model=>$model]);
        }
    //将创建的角色入库
        public function actionDoadd(){
            $role = Yii::$app->request->post(Rbac);
            $item = $role[role];
            $auth = Yii::$app->authManager;
            $role = $auth->createRole($item);
            $role->description = 创建了  . $item .  角色;
            $auth->add($role);
            return $this->render(index,[message=>创建角色成功,您可以继续操作]);
        }
    //创建权限
        public function actionCreatepower(){
            $model = new Rbac();
            return $this->render(power,[model=>$model]);
        }
    //将创建的权限入库
        public function  actionDopower(){
            $power = Yii::$app->request->post(Rbac);
            $item = $power[power];
            $auth = Yii::$app->authManager;
            $createPost = $auth->createPermission($item);
            $createPost->description = 创建了  . $item .  许可;
            $auth->add($createPost);
            return $this->render(index,[message=>创建权限成功,您可以继续操作]);
        }
    //给角色分配权限
        public function actionRp(){
            $models  = new Loginform();
            $model   = new Rbac();
            //查出所有的角色
            $role = Rbac::getrole(auth_item);
            //查出所有的权限
            $power = Rbac::getpower(auth_item);
            $roles = $models->arraytostring($role,name,name);
            $powers = $models->arraytostring($power,name,name);
            return $this->render(rp,[model=>$model,role=>$roles,power=>$powers]);
        }
    //将分配好的角色和权限入库
            public  function  actionDorp(){
                $data = Yii::$app->request->post(Rbac);
                $power = $data[power];
                $role  = $data[role];
                $item = Rbac::rolepower($role,$power);
                foreach($item as $items){
                    $auth = Yii::$app->authManager;
                    $parent = $auth->createRole($items[0]);
                    $child = $auth->createPermission($items[1]);
                    $auth->addChild($parent, $child);
                }
                return $this->render(index,[message=>分配权限成功,您可以继续操作]);
            }
            //给用户分配角色
            public function actionUr(){
                $models  = new Loginform();
                $model   = new Rbac();
                //查出所有的角色
                $role = Rbac::getrole(auth_item);
                //查出所有的权限
                $user = Rbac::getuser(user);
                $roles = $models->arraytostring($role,name,name);
                $users = $models->arraytostring($user,id,username);
                return $this->render(ur,[model=>$model,role=>$roles,user=>$users]);
            }
            public function actionDour(){
                $data = Yii::$app->request->post(Rbac);
                $user = $data[user];
                $role  = $data[role];
                $item = Rbac::rolepower($role,$user);
                foreach($item as $items){
                    $auth = Yii::$app->authManager;
                    $reader = $auth->createRole($items[0]);
                    $auth->assign($reader, $items[1]);
                }
                return $this->render(index,[message=>分配角色成功,您可以继续操作]);
            }
            public function beforeAction($action)
            {
                $action = Yii::$app->controller->action->id;
                if(\Yii::$app->user->can($action)){
                    return true;
                }else{
                    throw new \yii\web\UnauthorizedHttpException(对不起,您现在还没获此操作的权限);
                }
            }
}

以下是model 代码

<?php
/**
 * Created by PhpStorm.
 * User: zhoukang
 * Date: 2017/6/11
 * Time: 18:41
 */

namespace frontend\models;

use yii\base\Model;
use yii;
use db;
class Rbac extends Model
{
        public $role;
        public $power;
        public $user;
        public function rules(){
            return[

            ];
        }
        public function attributeLabels()
        {
          return[
              ‘role‘=>‘角色‘,
              ‘power‘=>‘权限‘,
              ‘user‘=>‘用户‘
          ];
        }
        static public function getrole($tablename){
            $sql = "select name from $tablename where TYPE = 1";
            $data = Yii::$app->db->createCommand($sql)->queryAll();
            return $data;
        }
        static public function getpower($tablename){
            $sql = "select name from $tablename where TYPE = 2";
            $data = Yii::$app->db->createCommand($sql)->queryAll();
            return $data;
        }
        static public function rolepower($role,$power){
            $arr = array();
            foreach($role as $value) {
                foreach ($power as $v) {
                    $arr[] = array($value, $v);
                }
            }
            return $arr;
        }
        static public function getuser($tablename){
            $sql = "select id,username from $tablename ";
            $data = Yii::$app->db->createCommand($sql)->queryAll();
            return $data;
        }

}

view 层代码就没必要写了吧

 

以上是关于yii rbac管理的主要内容,如果未能解决你的问题,请参考以下文章

Yii2全面解析之权限管理RBAC

Yii-backadmin|Yii2做的rbac后台通用权限的管理系统

yii2.0中Rbac 怎么添加超加管理员

Yii Framework 2.0 基于角色的访问控制 RBAC

yii2 后台怎么直接用/admin

Yii2 yii2mod/yii2-rbac