树形RBAC权限管理基于TP5

该文章介绍了一个基于TP5框架实现的树形RBAC权限管理方案,适用于中小型项目。作者指出,虽然存在优化空间,但在小型环境中可以直接应用,而在中型项目中可能需要进一步封装。
摘要由CSDN通过智能技术生成

这个权限管理类可以投入到中小环境中,当然在中型环境中,还需要对相关内容进行封装,而对于一些小环境则可以直接使用。当时花了半天写的,很多需要优化的地方。

<?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
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值