147 lines
		
	
	
		
			5.1 KiB
		
	
	
	
		
			PHP
		
	
	
		
		
			
		
	
	
			147 lines
		
	
	
		
			5.1 KiB
		
	
	
	
		
			PHP
		
	
	
| 
								 | 
							
								<?php
							 | 
						|||
| 
								 | 
							
								namespace app\controller\manager;
							 | 
						|||
| 
								 | 
							
								
							 | 
						|||
| 
								 | 
							
								use app\model\{AuthGroup, AuthRule, Log};
							 | 
						|||
| 
								 | 
							
								use app\validate\AuthGroup as VAuthGroup;
							 | 
						|||
| 
								 | 
							
								use think\exception\ValidateException;
							 | 
						|||
| 
								 | 
							
								
							 | 
						|||
| 
								 | 
							
								/**
							 | 
						|||
| 
								 | 
							
								 * 角色管理控制器
							 | 
						|||
| 
								 | 
							
								 */
							 | 
						|||
| 
								 | 
							
								class Group extends Base
							 | 
						|||
| 
								 | 
							
								{
							 | 
						|||
| 
								 | 
							
								    /**
							 | 
						|||
| 
								 | 
							
								     * 角色、分组删除
							 | 
						|||
| 
								 | 
							
								     */
							 | 
						|||
| 
								 | 
							
								    public function del()
							 | 
						|||
| 
								 | 
							
								    {
							 | 
						|||
| 
								 | 
							
								        if ($this->request->isPost()) {
							 | 
						|||
| 
								 | 
							
								            $id = input('post.id/d');
							 | 
						|||
| 
								 | 
							
								            if (is_numeric($id) === true && $id > 0) {
							 | 
						|||
| 
								 | 
							
								                $item = AuthGroup::getById($id);
							 | 
						|||
| 
								 | 
							
								                if(!empty($item)){
							 | 
						|||
| 
								 | 
							
								                    AuthGroup::destroy($id);
							 | 
						|||
| 
								 | 
							
								                    Log::write('group', 'del', '删除角色,ID:' . $id . ',名称:' . $item['title']);
							 | 
						|||
| 
								 | 
							
								                    return $this->json();
							 | 
						|||
| 
								 | 
							
								                }
							 | 
						|||
| 
								 | 
							
								            }
							 | 
						|||
| 
								 | 
							
								            return $this->json(2, '传入参数错误,请核对之后再操作!');
							 | 
						|||
| 
								 | 
							
								        }
							 | 
						|||
| 
								 | 
							
								        return $this->json(1, '非法请求!');
							 | 
						|||
| 
								 | 
							
								    }
							 | 
						|||
| 
								 | 
							
								    /**
							 | 
						|||
| 
								 | 
							
								     * 角色、分组权限分配
							 | 
						|||
| 
								 | 
							
								     */
							 | 
						|||
| 
								 | 
							
								    public function rule()
							 | 
						|||
| 
								 | 
							
								    {
							 | 
						|||
| 
								 | 
							
								        if($this->request->isPost()){
							 | 
						|||
| 
								 | 
							
								            $rules = input('post.rules/a');
							 | 
						|||
| 
								 | 
							
								            $groupId = input('post.group_id/d');
							 | 
						|||
| 
								 | 
							
								            if (is_array($rules) && (is_numeric($groupId) === true && $groupId > 0)) {
							 | 
						|||
| 
								 | 
							
								                $group = AuthGroup::getById($groupId);
							 | 
						|||
| 
								 | 
							
								                if(empty($group)){
							 | 
						|||
| 
								 | 
							
								                    return $this->json(2, '无此角色信息,请核对之后再操作!');
							 | 
						|||
| 
								 | 
							
								                }
							 | 
						|||
| 
								 | 
							
								                AuthGroup::updateRules($groupId, $rules);
							 | 
						|||
| 
								 | 
							
								                // 重置该角色对应的权限缓存
							 | 
						|||
| 
								 | 
							
								                AuthGroup::resetGroupRulesCache($groupId);
							 | 
						|||
| 
								 | 
							
								                Log::write('group', 'rule', '角色分配权限,ID:' . $groupId . ',名称:' . $group['title']);
							 | 
						|||
| 
								 | 
							
								                return $this->json();
							 | 
						|||
| 
								 | 
							
								            }else{
							 | 
						|||
| 
								 | 
							
								                return $this->json(3, '传入参数错误,请核对之后再操作!');
							 | 
						|||
| 
								 | 
							
								            }
							 | 
						|||
| 
								 | 
							
								        } else {
							 | 
						|||
| 
								 | 
							
								            $groupId = input('param.group_id/d');
							 | 
						|||
| 
								 | 
							
								            $group = AuthGroup::getById($groupId);
							 | 
						|||
| 
								 | 
							
								            if(!empty($group)){
							 | 
						|||
| 
								 | 
							
								                $rules = AuthRule::getListTree();
							 | 
						|||
| 
								 | 
							
								                $this->data['group_id'] = $groupId;
							 | 
						|||
| 
								 | 
							
								                $this->data['group'] = $group;
							 | 
						|||
| 
								 | 
							
								                $this->data['rules'] = $rules;
							 | 
						|||
| 
								 | 
							
								                return $this->view();
							 | 
						|||
| 
								 | 
							
								            }else{
							 | 
						|||
| 
								 | 
							
								                return $this->json(1, '无此角色信息,请核对之后再操作!');
							 | 
						|||
| 
								 | 
							
								            }
							 | 
						|||
| 
								 | 
							
								        }
							 | 
						|||
| 
								 | 
							
								    }
							 | 
						|||
| 
								 | 
							
								    /**
							 | 
						|||
| 
								 | 
							
								     * 角色、分组添加
							 | 
						|||
| 
								 | 
							
								     * @param int $status 1:正常;0:禁止
							 | 
						|||
| 
								 | 
							
								     */
							 | 
						|||
| 
								 | 
							
								    public function add()
							 | 
						|||
| 
								 | 
							
								    {
							 | 
						|||
| 
								 | 
							
								        if($this->request->isPost()){
							 | 
						|||
| 
								 | 
							
								            $title = trim(input('post.title'));
							 | 
						|||
| 
								 | 
							
								            $status = input('post.status/d');
							 | 
						|||
| 
								 | 
							
								            if (!empty($title) && (is_numeric($status) === true) && ($status == 1 || $status == 0)) {
							 | 
						|||
| 
								 | 
							
								                $item = [
							 | 
						|||
| 
								 | 
							
								                    'title' => $title,
							 | 
						|||
| 
								 | 
							
								                    'status' => $status
							 | 
						|||
| 
								 | 
							
								                ];
							 | 
						|||
| 
								 | 
							
								                try {
							 | 
						|||
| 
								 | 
							
								                    validate(VAuthGroup::class)->check($item);
							 | 
						|||
| 
								 | 
							
								                    $group = AuthGroup::create($item);
							 | 
						|||
| 
								 | 
							
								                    Log::write('group', 'add', "角色新增,ID:{$group->id} ,标题:{$group->title}");
							 | 
						|||
| 
								 | 
							
								                    return $this->json();
							 | 
						|||
| 
								 | 
							
								                } catch (ValidateException $e) {
							 | 
						|||
| 
								 | 
							
								                    return $this->json(2, $e->getError());
							 | 
						|||
| 
								 | 
							
								                }
							 | 
						|||
| 
								 | 
							
								            }
							 | 
						|||
| 
								 | 
							
								            return $this->json(1, '传入参数错误,请核对之后再操作!');
							 | 
						|||
| 
								 | 
							
								        }else{
							 | 
						|||
| 
								 | 
							
								            return $this->view();
							 | 
						|||
| 
								 | 
							
								        }
							 | 
						|||
| 
								 | 
							
								    }
							 | 
						|||
| 
								 | 
							
								
							 | 
						|||
| 
								 | 
							
								    /**
							 | 
						|||
| 
								 | 
							
								     * 角色、分组编辑
							 | 
						|||
| 
								 | 
							
								     */
							 | 
						|||
| 
								 | 
							
								    public function edit()
							 | 
						|||
| 
								 | 
							
								    {
							 | 
						|||
| 
								 | 
							
								        if($this->request->isPost()){
							 | 
						|||
| 
								 | 
							
								            $title = trim(input('post.title'));
							 | 
						|||
| 
								 | 
							
								            $status = input('post.status/d');
							 | 
						|||
| 
								 | 
							
								            $id  = input('post.id/d');
							 | 
						|||
| 
								 | 
							
								            if (!empty($title) && ($status == 1 || $status == 0) && (is_numeric($id) === true && $id > 0)) {
							 | 
						|||
| 
								 | 
							
								                $item = [
							 | 
						|||
| 
								 | 
							
								                    'title' => $title,
							 | 
						|||
| 
								 | 
							
								                    'status' => $status
							 | 
						|||
| 
								 | 
							
								                ];
							 | 
						|||
| 
								 | 
							
								                try {
							 | 
						|||
| 
								 | 
							
								                    validate(VAuthGroup::class)->check($item);
							 | 
						|||
| 
								 | 
							
								                    AuthGroup::updateById($id, $item);
							 | 
						|||
| 
								 | 
							
								                    Log::write('group', 'edit', "角色编辑,ID:{$id} ,标题:{$item['title']}");
							 | 
						|||
| 
								 | 
							
								                    return $this->json();
							 | 
						|||
| 
								 | 
							
								                } catch (ValidateException $e) {
							 | 
						|||
| 
								 | 
							
								                    return $this->json(2, $e->getError());
							 | 
						|||
| 
								 | 
							
								                }
							 | 
						|||
| 
								 | 
							
								            }
							 | 
						|||
| 
								 | 
							
								            return $this->json(1, '传入参数错误,请核对之后再操作!');
							 | 
						|||
| 
								 | 
							
								        }else{
							 | 
						|||
| 
								 | 
							
								            $id  = input('param.id/d');
							 | 
						|||
| 
								 | 
							
								            if (is_numeric($id) === true && $id > 0) {
							 | 
						|||
| 
								 | 
							
								                $item = AuthGroup::getById($id);
							 | 
						|||
| 
								 | 
							
								                $this->data['item'] = $item;
							 | 
						|||
| 
								 | 
							
								                return $this->view();
							 | 
						|||
| 
								 | 
							
								            }
							 | 
						|||
| 
								 | 
							
								            return $this->json(1, '传入参数错误,请核对之后再操作!');
							 | 
						|||
| 
								 | 
							
								        }
							 | 
						|||
| 
								 | 
							
								    }
							 | 
						|||
| 
								 | 
							
								
							 | 
						|||
| 
								 | 
							
								    /**
							 | 
						|||
| 
								 | 
							
								     * 所有角色分组信息
							 | 
						|||
| 
								 | 
							
								     * @return void
							 | 
						|||
| 
								 | 
							
								     */
							 | 
						|||
| 
								 | 
							
								    public function index()
							 | 
						|||
| 
								 | 
							
								    {
							 | 
						|||
| 
								 | 
							
								        $auth = session('auth');
							 | 
						|||
| 
								 | 
							
								        if ($auth['groupId'] == 1) {
							 | 
						|||
| 
								 | 
							
								            $list = AuthGroup::select()->toArray();
							 | 
						|||
| 
								 | 
							
								        } else {
							 | 
						|||
| 
								 | 
							
								            $list = AuthGroup::where('id', '<>', 1)->select()->toArray();
							 | 
						|||
| 
								 | 
							
								        }
							 | 
						|||
| 
								 | 
							
								        $this->data['list'] = $list;
							 | 
						|||
| 
								 | 
							
								        return $this->view();
							 | 
						|||
| 
								 | 
							
								    }
							 | 
						|||
| 
								 | 
							
								}
							 |