一、总控制补充
在根目录下app文件夹下controller文件夹中修改Base总控制文件。需要添加操作者权限验证、获取操作者权限、设置操作者权限。
1、权限验证
//验证权限
protected function checkRoleMenu($auth){
if(empty($this->rules) || empty($this->key) || count($this->butts) == 0) $this->setAuth();
if(!in_array($auth, $this->butts))throw new BaseError("权限不足,请联系管理员",50000,200);//赋值错误信息 权限验证
}
2、获取权限
//获取权限
protected function getRoleMenu(){
if(empty($this->rules) || empty($this->key) || count($this->butts) == 0) $this->setAuth();
$data['key'] = $this->key;
$data['butts'] = $this->butts;
return $data;
}
3、设置权限
//设置权限
protected function setAuth(){
$resRole = Role::dataFind(['id' => $this->roleId],'role_key,menu_id,status',true);
if(empty($resRole) || $resRole['status'] !== 1)throw new BaseError("该用户没有权限",50000,200);//赋值错误信息
$rules = explode(',',$resRole['menu_id']);
$butt = Redis::select(config('cache.stores.redis.cache_db'))->get('butt_list'.$this->userId);
if(empty($butt)){
$this->isUnserialize = false;
$list = Menu::getAll([['id', 'in', $rules], ['status', '=', 1], ['is_menu', '=', 0]], 'menuname','sort DESC,id DESC');
if(empty($list))throw new BaseError("该用户没有权限",50000,200);//赋值错误信息
$butt = array_column($list,'menuname');
Redis::select(config('cache.stores.redis.cache_db'))->setex('butt_list'.$this->userId,rand(40,80),serialize($butt));
}
$this->rules = $rules;
$this->key = $resRole['role_key'];
$this->butts = $this->isUnserialize == true ? unserialize($butt) : $butt;
}
二、给后端添加、编辑、删除操作设置权限访问
所有的启禁用控制接口、保存接口、删除接口都需要调用权限验证方法。代码如下
$this->checkRoleMenu('PermissionMenuIndexSave');//验证权限