首先,先創建Rbac那五張表(用戶表,角色表,節點表,權限表,角色-用戶表),後面四張能夠在thinkphp中Rbac類裏直接複製。php
第二步,根據需求往那五張表裏插入數據,注意:節點表裏的節點名稱必定要跟當前的應用,控制器和方法名稱一致,還有應用的level值等於1,不是等於0的,如下爲PHP代碼:node
<?php namespace Home\Controller; use Think\Controller; use Org\Util\Rbac; class RbacController extends CommonController{ public function index(){ $this->user=D('UserRelation')->relation(true)->select(); $this->display(); } public function addUser(){ $this->role=M('role')->select(); $this->display(); } public function addUserHandle(){ $arr=I('post.'); $data=array( 'userName'=>I('post.username'), 'password'=>I('post.password','','md5'), 'logintime' =>date('Y-m-d:H:i:s',time()), 'loginip' => get_client_ip() ); if ($uid=M('user')->add($data)) { foreach ($arr['rid'] as $v) { $id[]=array( 'role_id'=>$v, 'user_id'=>$uid ); } M('role_user')->addAll($id); $this->success('添加成功',U('Rbac/index')); }else{ $this->error('添加失敗'); } } public function deleteUser(){ if (M('user')->where(array('id'=>I('get.id')))->delete()) { M('role_user')->where(array('user_id'=>I('get.id')))->delete(); $this->success('刪除成功',U('Rbac/index')); }else{ $this->error('刪除失敗'); } } public function addRole(){ $this->display(); } public function addRoleHandle(){ if (M('role')->add(I('post.'))) { $this->success('添加成功',U('Rbac/roleList')); }else{ $this->error('添加失敗'); } } public function roleList(){ $this->role=M('role')->select(); $this->display(); } public function deleteRole(){ if (M('role')->where(array('id'=>I('get.id')))->delete()) { M('access')->where(array('role_id'=>I('get.id')))->delete(); $this->success('刪除成功',U('Rbac/roleList')); }else{ $this->error('刪除失敗'); } } public function addNode(){ $this->pid=I('get.pid',0); $this->level=I('get.level',1); switch ($this->level) { case '1': $this->string='應用'; break; case '2': $this->string='控制器'; break; case '3': $this->string='方法'; break; } $this->display(); } public function addNodeHandle(){ if (M('node')->add(I('post.'))) { $this->success('添加成功',U('Rbac/nodeList')); }else{ $this->error('添加失敗'); } } public function nodeList(){ $node=M('node')->select(); $this->node=order($node); $this->display(); } public function deleteNode(){ $node=M('node')->select(); $pid=I('get.id'); $node=getChildrenId($node,$pid); M('node')->where(array('id'=>$pid))->delete(); foreach ($node as $v) { M('node')->where(array('id'=>$v))->delete(); } $this->success('刪除成功',U('Rbac/nodeList')); } public function access(){ $node=M('node')->select(); $this->role_id=I('get.id'); $access=M('access')->where(array('role_id'=>$this->role_id))->getField('node_id',true); $this->node=order($node,$access); $this->display(); } public function accessHandle(){ $data=I('post.'); foreach ($data['access'] as $v) { $access=explode('_', $v); $arr[]=array( 'role_id'=>I('post.role_id'), 'node_id'=>$access['0'], 'level'=>$access['1'] ); } M('access')->where(array('role_id'=>I('post.role_id')))->delete(); if (M('access')->addAll($arr)) { $this->success('配置成功',U('Rbac/roleList')); }else{ $this->error('配置失敗'); } } } ?>
第三步,編寫配置項,如下代碼:thinkphp
<?php
return array(
'USER_AUTH_ON'=>true,
'USER_AUTH_TYPE'=>'1',
'USER_AUTH_KEY'=>'uid',
'ADMIN_AUTH_KEY'=>'superadmin',
'RBAC_SUPERADMIN'=>'admin',
'RBAC_ROLE_TABLE'=>'fsxb_role',
'RBAC_USER_TABLE'=>'fsxb_role_user',
'RBAC_ACCESS_TABLE'=>'fsxb_access',
'RBAC_NODE_TABLE'=>'fsxb_node',
'NOT_AUTH_CONTROLLER'=>'Public',
'NOT_AUTH_ACTION'=>'index,tree,consumerList,addUserHandle,addRoleHandle,addNodeHandle,accessHandle,agentList,modelList,download,addConsumerHandle,updateConsumerHandle,addAgentHandle,updateAgentHandle,addModelHandle,updateModelHandle'
);session
第四步,調用thinkphp中Rbac類,在登錄方法存儲session的時候,把權限也存儲進去,添加如下代碼:函數
if($username== C('RBAC_SUPERADMIN')) { session(C('ADMIN_AUTH_KEY'), true); } Rbac::saveAccessList();
第五步,在CommonController公共控制器的自動運行函數中調用Rbac權限認證方法,代碼以下:post
<?php namespace Home\Controller; use Think\Controller; use Org\Util\Rbac; class CommonController extends Controller{ public function _initialize(){ if(!session('uid')){ $this->redirect('Public/index'); } $notAuth = in_array(CONTROLLER_NAME , explode(',', C('NOT_AUTH_CONTROLLER'))) || in_array(ACTION_NAME, C('NOT_AUTH_ACTION')); //權限驗證 if(C('USER_AUTH_ON') && !$notAuth) { //使用了項目分組,則必須引入GROUP_NAME RBAC::AccessDecision() || $this->error("你沒有權限"); } } } ?>
到此爲止,thinkphp權限認證編寫完成ui