Home > php教程 > php手册 > 超简单的RBAC实现

超简单的RBAC实现

WBOY
Release: 2016-06-07 11:43:53
Original
1237 people have browsed it

超简单的RBAC实现
将官方的RBAC进行改造,也现实强大的角色权限/**<br>      +----------------------------------------------------------<br>      * 取得当前认证号的所有权限列表<br>      +----------------------------------------------------------<br>      * @param integer $authId 用户ID<br>      +----------------------------------------------------------<br>      * @access public<br>      +----------------------------------------------------------<br>      */<br>     static public function getAccessList($authId)<br>     {<br>         //echo "<div class="code" style="position:relative; padding:0px; margin:0px;"><pre class="brush:php;toolbar:false">&quot;;print_r($authId);exit;&lt;br&gt;         // Db方式权限数据&lt;br&gt;         $db     =   Db::getInstance(C('RBAC_DB_DSN'));&lt;br&gt;     $auth_type = C(&quot;AUTH_TYPE&quot;);&lt;br&gt;         $table = array('role'=&gt;C(&quot;DB_PREFIX&quot;).C('RBAC_ROLE_TABLE'),'user'=&gt;C(&quot;DB_PREFIX&quot;).C('RBAC_USER_TABLE'),'access'=&gt;C(&quot;DB_PREFIX&quot;).C('RBAC_ACCESS_TABLE'),'node'=&gt;C(&quot;DB_PREFIX&quot;).C('RBAC_NODE_TABLE'));&lt;br&gt;         $sql    =   &quot;select node.auth_type,node.id,node.action,node.action_name,node.module,node.module_name from &quot;.&lt;br&gt;                     $table['role'].&quot; as role,&quot;.&lt;br&gt;                     $table['user'].&quot; as user,&quot;.&lt;br&gt;                     $table['access'].&quot; as access ,&quot;.&lt;br&gt;                     $table['node'].&quot; as node &quot;.&lt;br&gt;                     &quot;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&quot;;&lt;br&gt; &lt;br&gt;         $apps =   $db-&gt;query($sql);&lt;br&gt;         require_once('./Conf/Admin/authoritys.php');&lt;br&gt;         $access =  array();&lt;br&gt;         foreach($apps as $key=&gt;$app)&lt;br&gt;     {&lt;br&gt;             $appId  = $app['id'];&lt;br&gt;       $module_name   =   $app['module'];&lt;br&gt;             $action_name   =   $app['action'];&lt;br&gt;       $o_module_name = strtoupper($module_name);&lt;br&gt;       $o_action_name = strtoupper($action_name);&lt;br&gt;       $l_module_name = strtolower($module_name);&lt;br&gt;       $l_action_name = strtolower($action_name);&lt;br&gt; &lt;br&gt;       // 读取项目的模块权限&lt;br&gt;             if($app['auth_type']==0) //节点授权&lt;br&gt;             {&lt;br&gt;               $access[$o_module_name][$o_action_name] =  true;&lt;br&gt; &lt;br&gt;         if(isset($authoritys['all'][$l_action_name]))&lt;br&gt;         {&lt;br&gt;           $authoritys_list = $authoritys['all'][$l_action_name];&lt;br&gt;           foreach($authoritys_list as $authority_item)&lt;br&gt;           {&lt;br&gt;             $access[$o_module_name][strtoupper($authority_item)] =  true;&lt;br&gt;           }&lt;br&gt;         }&lt;br&gt; &lt;br&gt;         if(isset($authoritys['actions'][$l_module_name][$l_action_name]))&lt;br&gt;         {&lt;br&gt;           $authoritys_list = $authoritys['actions'][$l_module_name][$l_action_name];&lt;br&gt;           foreach($authoritys_list as $authority_item)&lt;br&gt;           {&lt;br&gt;             $access[$o_module_name][strtoupper($authority_item)] =  true;&lt;br&gt;           }&lt;br&gt;         }&lt;br&gt;             }&lt;br&gt; &lt;br&gt;           if($app['auth_type']==1) //模块授权&lt;br&gt;             {&lt;br&gt;               $access[$o_module_name][strtoupper($auth_type[$app['auth_type']])] = true;&lt;br&gt;             }&lt;br&gt; &lt;br&gt;           if($app['auth_type']==2) //操作授权&lt;br&gt;             {&lt;br&gt;               $access[strtoupper($auth_type[$app['auth_type']])][$o_action_name] = true;&lt;br&gt;       }&lt;br&gt;         }&lt;br&gt; &lt;br&gt;         //不需要认证的模块&lt;br&gt;         if(!empty($authoritys['no']) &amp;&amp; is_array($authoritys['no'])){&lt;br&gt;           foreach($authoritys['no'] as $module_name=&gt;$action_name){&lt;br&gt;               $o_module_name = strtoupper($module_name);         &lt;br&gt;               $o_action_name = strtoupper(key($action_name));&lt;br&gt;               foreach($action_name as $keymodule=&gt;$valaction){&lt;br&gt;                 $keymodule = strtoupper($keymodule);&lt;br&gt;                 $access[$o_module_name][$keymodule] =  true;&lt;br&gt;               }&lt;br&gt;           }&lt;br&gt; &lt;br&gt;         }&lt;br&gt;         return $access;&lt;br&gt;     }</pre><div class="contentsignin">Copy after login</div></div>

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

AD:真正免费,域名+虚机+企业邮箱=0元

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Recommendations
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template