这个权限管理类可以投入到中小环境中,当然在中型环境中,还需要对相关内容进行封装,而对于一些小环境则可以直接使用。当时花了半天写的,很多需要优化的地方。
<?php
namespace Admin\Controller;
use Think\Controller;
class RbacController extends Controller
{
public function _initialize()
{
$userid=D('user')->field('id')->where(array('name'=>$_COOKIE['username']))
->select()[0]['id'];
if(!$userid){
$message=array(
'status'=>2,
'info'=>'请登录',
'userid'=>$userid
);
return $message;
}
//获取权限
$right_list=$this->getRightUser($userid);
if(!$right_list){
$message=array(
'status'=>3,
'info'=>'你没有权限操作'
);
return $message;
}
$right_route=array();
for($i=0;$i<count($right_list);$i++){
array_push($right_route,$right_list[$i]['route']);
}
unset($right_list);
$menu_list=$this->getMenuUser($userid);
$base_path_array=split('/',$_SERVER['REQUEST_URI']);
$user_op_path=$base_path_array[1].'/'.$base_path_array[2].'/'.$base_path_array[3];
if(!in_array($user_op_path,$right_route)){
$message=array(
'status'=>3,
'info'=>'你没有权限操作'
);
return $message;
}
$message=array(
'status'=>1,
'info'=>$menu_list
);
unset($right_route);
unset($menu_list);
return $message;
}
public function addRight()
{
$right_name=I('name');
$right_route=I('route');
$right_description=I('description');
$add_right=array(
'name'=>$right_name,
'route'=>$right_route,
'description'=>$description
);
$effect=D('rights')->add($add_right);
if($effect){
$message=array(
'status'=>1,
'info'=>'添加权限成功'.$effect
);
}else{
$message=array(
'status'=>2,
'info'=>'添加权限失败'
);
}
$this->ajaxReturn($message);
}
public function delRight()
{
$right_id=I('id');
$where['id']=$right_id;
$effect=D('rights')->where($where)->delete();
if($effect){
$message=array(
'status'=>1,
'info'=>$effect.'删除权限成功'
);
}else{
$message=array(
'status'=>2,
'info'=>'删除权限错误'
);
}
$this->ajaxReturn($message);
}
public function addRole()
{
$role_name=I('name');
$role_description=I('description');
$role_parent=I('parent');
$addRole=array(
'name'=>$role_name,
'description'=>$role_description,
'parent'=>$role_parent
);
$effect=D('role')->insert($addRole);
$message=array(
'status'=>1,
'info'=>'添加角色节点成功'
);
unset($role_name);
unset($role_description);
unset($role_parent);
$this->ajaxReturn($message);
}
public function _delrole()
{
$role_id=I('id');
$where['id']=$role_id;
//dump(111111);die;
$effect=D('role')->where($where)->delete();
if($effect){
$message=array(
'status'=>1,
'info'=>'删除角色成功'.$effect
);
}else{
$message=array(
'status'=>2,
'info'=>'删除角色失败'
);
}
$this->ajaxReturn($message);
}
public function updateRoleDesc()
{
$role_id=I('id');
$role_desc=I('desc');
$where['id']=$role_id;
$effect=D('role')->where($where)->delete();
if($effect){
$message=array(
'status'=>1,
'info'=>'更新角色成功'.$effect
);
}else{
$message=array(
'status'=>2,
'info'=>'更新角色失败'
);
}
$this->ajaxReturn($message);
}
public function updateRoleN()
{
$role_id=I('id');
$role_parent=I('parent');
$role_child=I('child');
$where['id']=array('in',ar