zoukankan      html  css  js  c++  java
  • thinkphp5 rbac权限

    thinkphp 5 rbac权限

      先创建一个数据库;

    例如:创建一个test数据库;然后创建3个 表分别为:test_admin (管理员表), test_role,test_auth.

    这个是新创建的test库

    管理员表

    这个是新创建的admin表, 这个表是用户表是管理后台的用户。

    这个表的issuper这个字段代表是否是超级管理员 , 这个超级管理员可以管理全部的角色和执行所有的权限。

    admin_role_id 这个字段主要描述的是除了超级管理员之外的管理员所对应的角色表id 下面我们会给出角色表.

    角色表

    这个表是角色表,他的主id 和管理员的admin_role_id可以分出管理员都处于什么角色管理.

    权限表

    这个表是权限表,他的主id 所对应的是角色表的role_auth_id 可以得出不同的角色有着不同的权限可以执行.

      网站后台管理页面登陆不同的管理员对角色和角色权限的显示.

     

    在tinkphp的application的admin文件的model层创建Admin.php,Role.php,Auth.php进行业务处理.

    然后在controller层创建index.php

    <?php
    namespace appadmincontroller;
    use thinkController;
    use thinkUrl;
    use thinkRequest;
    use thinkSession;
    use appadminmodelAuth as AuthModel
    use appadminmodelRole as RoleModel
    
    class Index extends CommonController
    {
         public $role;
         public $auth;
         public $view;
    public funtion __construct()
    {
      $this->role = new RoleModel()
      $this->auth = new AuthModel()
      $this->view = new View();
    }   
    
      publci function auth()
    {
    //角色id;
      $admin_id = sesison('admin_id');
      $admin_name = session('admin_name');
    
      $resAdmin = $this->admin->where(['admin_id'=>$admin_id])->select();
      if($resAdmin[0]->issuper == 1){
    //超级管理员拥有全部权限;
    //一级权限;
      $authA = $this->auth->where(['auth_level']=>0)->select();
    //二级权限
      $authB = $this->auth->where(['auth_level'=>1])->select();
    } else {
      //权限ids;
      $role_auth_ids = $this->role->where(['role_id'=>$admin_role_id])->select();
      $authA = $this->auth->where('auth_level' , 0)->where('auth_id' , 'in' , $role_auth_ids)->select();
      $authB  = $this->auth->where('auth_level' , 1])->where('auth_id' , 'in' , $role_auth_ids)->select();
    }
    
      $auth = array('authA'=>$authA , 'authB'=>$authB);
      $this->redirect('admin/'.$auth['authA'][0]->auth_c.'/'.$auth['authA'][0]->auth_a);
    
    }
    
    public function leftnav()
    {
      $admin_id = session('admin_id');
      $amin_name = session('admin_name');
      //角色id;
      $resAdmin = $this->admin->where(['admin_id']=>$admin_id)->select();
      $admin_role_id = $resAdmin[0]->$admin_role_id;
      if($resAdmin[0]->issuper == 1){
      //超级管理员super拥有全部权限;
     //一级权限;
      $authA = $this->auth->where(['auth_level'=>0])->select();
     //二级权限;
      $authB = $this->auth->where(['auth_level'=>1])->select();
    }  else {
      //权限ids
      $role_auth_ids = $this->role->where(['role_id'=>$admin_role_id])->select();
      $role_auth_ids = $role_auth_ids[0]->role_auth_ids;
    
      $authA = $this->auth->where('auth_level' , 0)->where('auth_id' , 'in' , $role_auth_ids)->select();
      $authB = $this->auth->where('auth_level' , 1)->where('auth_id' , 'in' , $role_aut_ids)->select();
    }
    
      $auth = array('authA'=>$authA , 'authB'=>$authB);
      $this->view->assign('authA' , $auth['authA']);
      $this->view->assign('authB' , $auth['authB']);
    }
    
        
    }

    现在我来解释一下上面auth方法的作用是用来重定向的如果登陆的管理者向url地址输入了不属于他的权限的地址我们会让他重定向到他自己的管理页面.

    还有继承的CommonController 的内容;

    <?php
    namspace appadmincontroller;
    use thinkController;
    use thinkRequest;
    use appadminmodelCommon as Controller
    {
      public function __construct()
    {
      parent::__construct();
      $res = new CommonModel();
      $resquest = Request::instance();
    
      if(session('admin_id') == null){
      if(strtolower($resquest->controller()) == 'index' && strtolower($resquest->action()) == 'login'){
      return true;
    } else {
     $this->error('没有登陆!<br /><span style="color:gray;">...</span> ');
    }
    
     $resCommon = $res->auth();
     if(Request::instance()->isAjax()){
      $this->ajaxReturn(['msg'=>'没有操作权限!' , 'code'=>'201'] , 'json');
    } else {
     $this->error('没有操作权限!<br><span style="color:gray;">...</span>');
    }
    }
    }
    }

       权限控制

        管理员登陆后台 访问属于自己权限的操作业务 , 如果管理员想要越级查看不属于自己权限的业务 , 控制器 会让管理员重定向到自己的操作页面.

    <?php
    namespace appadminmodel;
    use thinkModel;
    use thinkDb;
    use thinkSession;
    use thinkRequest;
    use appadminmodelAdmin as AdminModel;
    use appadminmodelRole as RoleModel;
    use appadminmodelAuth as AuthModel;
    
    class Common extends Model
    {
    
        public function auth()
        {
            //当前控制器和操作方法;
            $request= Request::instance();
            $auth_ac = strtolower(trim($request->controller())).'/'.strtolower(trim($request->action()));
            //var_dump($auth_ac);
            $auth = array();
            $res = new AdminModel();
            $resRole = new RoleModel();
            $resAuth = new AuthModel();
            
            $resAdmin = $res->where(['admin_id'=>session('admin_id')])->select();
            //非超级管理员控制权限;
            if($resAdmin[0]->issuper != 1){
                $admin_role_id = $resAdmin[0]->admin_role_id;
                //$admin_role_id = $info['admin_role_id'];
                //$info = $this->info('Role' , ['role_id'=>$admin_role_id] , 'role_auth_ids');
                $info = $resRole->where('role_id' , $admin_role_id)->select();
                $role_auth_ids = $info[0]->role_auth_ids;
                $infos = $resAuth->where('auth_id' , 'in' , $role_auth_ids)->select();
                //$infos = $this->infos('Auth' , ['auth_id'=>['in' , $role_auth_ids] , 'auth_level'=>1] ,'auth_c , auth_a' );
                foreach($infos as $key=>$val){
                    $auth[] = $val['auth_c'].'/'.$val['auth_a'];
                } 
    
                $result = array_merge($auth , ['index/auth'] , ['index/login']);
                //var_dump($result);
                if(in_array($auth_ac , $result)){
                    return true;
                } else {
                    return false;
                } 
    
    
            } else {
                return true;
            }
    
        }
    
    
    }

    上面的CommonModel 在CommonController 中被调用 , 来进行管理员权限等级的判断.

  • 相关阅读:
    如何高效处理bug?
    如何获取字符串函数的实现?
    数据处理-csv,pandas
    CentOS 下安装SAC(Seismic Analysis Code)
    CentOS安装使用ffmpeg
    Tensorflow问题记录 --pydot Failed to import pydot. You must install pydot and graphviz for pydotprint to work
    Tensorflow学习记录 --TensorFlow高效读取数据tfrecord
    Committee to boost development of Chinese language teachers
    Consensus-based decentralized auctions for robust task allocation
    Chinese market critical to US firms: US-China Business Council
  • 原文地址:https://www.cnblogs.com/whrTC/p/9944656.html
Copyright © 2011-2022 走看看