超简单的RBAC实现

jerry thinkphp 2015年11月18日 收藏
超简单的RBAC实现
将官方的RBAC进行改造,也现实强大的角色权限
  1. /**
  2.      +----------------------------------------------------------
  3.      * 取得当前认证号的所有权限列表
  4.      +----------------------------------------------------------
  5.      * @param integer $authId 用户ID
  6.      +----------------------------------------------------------
  7.      * @access public
  8.      +----------------------------------------------------------
  9.      */
  10.     static public function getAccessList($authId)
  11.     {
  12.         //echo "<pre>";print_r($authId);exit;
  13.         // Db方式权限数据
  14.         $db     =   Db::getInstance(C('RBAC_DB_DSN'));
  15.     $auth_type = C("AUTH_TYPE");
  16.         $table = array('role'=>C("DB_PREFIX").C('RBAC_ROLE_TABLE'),'user'=>C("DB_PREFIX").C('RBAC_USER_TABLE'),'access'=>C("DB_PREFIX").C('RBAC_ACCESS_TABLE'),'node'=>C("DB_PREFIX").C('RBAC_NODE_TABLE'));
  17.         $sql    =   "select node.auth_type,node.id,node.action,node.action_name,node.module,node.module_name from ".
  18.                     $table['role']." as role,".
  19.                     $table['user']." as user,".
  20.                     $table['access']." as access ,".
  21.                     $table['node']." as node ".
  22.                     "where user.u_id='{$authId}' and user.role_id=role.id and access.role_id=role.id and role.status=1 and access.node_id=node.id and node.status=1";

  23.         $apps =   $db->query($sql);
  24.         require_once('./Conf/Admin/authoritys.php');
  25.         $access =  array();
  26.         foreach($apps as $key=>$app)
  27.     {
  28.             $appId  = $app['id'];
  29.       $module_name   =   $app['module'];
  30.             $action_name   =   $app['action'];
  31.       $o_module_name = strtoupper($module_name);
  32.       $o_action_name = strtoupper($action_name);
  33.       $l_module_name = strtolower($module_name);
  34.       $l_action_name = strtolower($action_name);

  35.       // 读取项目的模块权限
  36.             if($app['auth_type']==0) //节点授权
  37.             {
  38.               $access[$o_module_name][$o_action_name] =  true;

  39.         if(isset($authoritys['all'][$l_action_name]))
  40.         {
  41.           $authoritys_list = $authoritys['all'][$l_action_name];
  42.           foreach($authoritys_list as $authority_item)
  43.           {
  44.             $access[$o_module_name][strtoupper($authority_item)] =  true;
  45.           }
  46.         }

  47.         if(isset($authoritys['actions'][$l_module_name][$l_action_name]))
  48.         {
  49.           $authoritys_list = $authoritys['actions'][$l_module_name][$l_action_name];
  50.           foreach($authoritys_list as $authority_item)
  51.           {
  52.             $access[$o_module_name][strtoupper($authority_item)] =  true;
  53.           }
  54.         }
  55.             }

  56.           if($app['auth_type']==1) //模块授权
  57.             {
  58.               $access[$o_module_name][strtoupper($auth_type[$app['auth_type']])] = true;
  59.             }

  60.           if($app['auth_type']==2) //操作授权
  61.             {
  62.               $access[strtoupper($auth_type[$app['auth_type']])][$o_action_name] = true;
  63.       }
  64.         }

  65.         //不需要认证的模块
  66.         if(!empty($authoritys['no']) && is_array($authoritys['no'])){
  67.           foreach($authoritys['no'] as $module_name=>$action_name){
  68.               $o_module_name = strtoupper($module_name);         
  69.               $o_action_name = strtoupper(key($action_name));
  70.               foreach($action_name as $keymodule=>$valaction){
  71.                 $keymodule = strtoupper($keymodule);
  72.                 $access[$o_module_name][$keymodule] =  true;
  73.               }
  74.           }

  75.         }
  76.         return $access;
  77.     }

附件huicms.zip ( 8.45 KB 下载:74 次 )