role = $role; } /** * * @time 2019年12月09日 * @return string */ public function index() { return CatchResponse::success($this->role->getList()); } /** *获取部分角色列表 * @time 2019年12月09日 * @return string */ public function getPartRoles() { return CatchResponse::success($this->role->getPartList()); } /** *获取当前用户数据权限 * @time 2019年12月09日 * @return string */ public function isAllDataRange() { return CatchResponse::success($this->role->isAllDataRange()); } /** * * @time 2019年12月11日 * @param Request $request * @return Json * @throws \think\db\exception\DbException */ public function save(Request $request) { $params = $request->param(); if (Roles::where('identify', $params['identify'])->find()) { throw new FailedException('角色标识 [' . $params['identify'] . ']已存在'); } Db::startTrans(); // 保存角色 $role_id = $this->role->storeBy($params); if (!$role_id) { return CatchResponse::fail('保存角色失败'); } // 保存权限 try { $this->role->attachPermissions(array_unique($params['permissions'])); } catch (\Exception $e) { Db::rollback(); return CatchResponse::fail('保存权限失败'); } // 追加角色 暂时关闭 // if ($params['identify'] != 'admin' && $params['identify'] != 'manage') { // $user = request()->user(); // $save_data = ['uid'=>$user['id'], 'role_id'=>$role_id]; // try { // Db::table('user_has_roles')->save($save_data); // } catch (\Exception $e) { // Db::rollback(); // return CatchResponse::fail('保存失败'); // } // } Db::commit(); return CatchResponse::success('保存成功'); } /** * * @time 2019年12月11日 * @param id */ public function read($id) { $role = $this->role->findBy($id); $role->permissions = $role->getPermissions(); return CatchResponse::success($role); } /** * * @time 2019年12月11日 * @param $id * @param Request $request * @return Json * @throws \think\db\exception\DbException */ public function update($id, Request $request): Json { if (Roles::where('identify', $request->param('identify'))->where('id', '<>', $id)->find()) { throw new FailedException('角色标识 [' . $request->param('identify') . ']已存在'); } $this->role->updateBy($id, $request->param()); $role = $this->role->findBy($id); $hasPermissionIds = $role->getPermissions()->column('id'); $permissionIds = $request->param('permissions'); // 已存在权限 IDS $existedPermissionIds = []; foreach ($hasPermissionIds as $hasPermissionId) { if (in_array($hasPermissionId, $permissionIds)) { $existedPermissionIds[] = $hasPermissionId; } } $attachIds = array_diff($permissionIds, $existedPermissionIds); $detachIds = array_diff($hasPermissionIds, $existedPermissionIds); if (!empty($detachIds)) { $role->detachPermissions($detachIds); } if (!empty($attachIds)) { $role->attachPermissions(array_unique($attachIds)); } return CatchResponse::success(); } /** * * @time 2019年12月11日 * @param $id * @throws FailedException * @throws \think\db\exception\DataNotFoundException * @throws \think\db\exception\DbException * @throws \think\db\exception\ModelNotFoundException * @return Json */ public function delete($id): Json { if ($this->role->where('parent_id', $id)->find()) { throw new FailedException('存在子角色,无法删除'); } if ( Db::table('user_has_roles')->where('role_id', $id)->where('uid','<>',1)->find()) { throw new FailedException('存在该角色用户'); } $role = $this->role->findBy($id); // 删除权限 $role->detachPermissions(); // 删除用户关联 $role->users()->detach(); // 删除 $this->role->deleteBy($id,true); return CatchResponse::success(); } /** * * @time 2019年12月11日 * @param Request $request * @param \catchAdmin\permissions\model\Permissions $permission * @return Json */ public function getPermissions(Request $request, \catchAdmin\permissions\model\Permissions $permission): Json { $parentRoleHasPermissionIds = []; if ($request->param('parent_id')) { $permissions = $this->role->findBy($request->param('parent_id'))->getPermissions(); foreach ($permissions as $_permission) { $parentRoleHasPermissionIds[] = $_permission->pivot->permission_id; } } $permissions = Tree::done(Permissions::whereIn('id', $parentRoleHasPermissionIds)->select()->toArray()); $permissionIds = []; if ($request->param('role_id')) { $roleHasPermissions = $this->role->findBy($request->param('role_id'))->getPermissions(); foreach ($roleHasPermissions as $_permission) { $permissionIds[] = $_permission->pivot->permission_id; } } return CatchResponse::success([ 'permissions' => $permissions, 'hasPermissions' => $permissionIds, ]); } }