代码优化

pull/308/head
Elune 2020-03-10 14:13:35 +08:00
parent 207e6fb1df
commit 0548d8486d
4 changed files with 7 additions and 13 deletions

View File

@ -109,8 +109,7 @@ public class AuthorizationController {
@ApiOperation("获取用户信息") @ApiOperation("获取用户信息")
@GetMapping(value = "/info") @GetMapping(value = "/info")
public ResponseEntity<Object> getUserInfo(){ public ResponseEntity<Object> getUserInfo(){
JwtUserDto jwtUserDto = (JwtUserDto)userDetailsService.loadUserByUsername(SecurityUtils.getCurrentUsername()); return ResponseEntity.ok(SecurityUtils.getCurrentUser());
return ResponseEntity.ok(jwtUserDto);
} }
@AnonymousAccess @AnonymousAccess

View File

@ -59,8 +59,7 @@ public class MenuController {
@ApiOperation("获取前端所需菜单") @ApiOperation("获取前端所需菜单")
@GetMapping(value = "/build") @GetMapping(value = "/build")
public ResponseEntity<Object> buildMenus(){ public ResponseEntity<Object> buildMenus(){
UserDto user = userService.findByName(SecurityUtils.getCurrentUsername()); List<MenuDto> menuDtoList = menuService.findByRoles(roleService.findByUsersId(SecurityUtils.getCurrentUserId()));
List<MenuDto> menuDtoList = menuService.findByRoles(roleService.findByUsersId(user.getId()));
List<MenuDto> menuDtos = (List<MenuDto>) menuService.buildTree(menuDtoList).get("content"); List<MenuDto> menuDtos = (List<MenuDto>) menuService.buildTree(menuDtoList).get("content");
return new ResponseEntity<>(menuService.buildMenus(menuDtos),HttpStatus.OK); return new ResponseEntity<>(menuService.buildMenus(menuDtos),HttpStatus.OK);
} }

View File

@ -139,8 +139,7 @@ public class RoleController {
* @return / * @return /
*/ */
private int getLevels(Integer level){ private int getLevels(Integer level){
UserDto user = userService.findByName(SecurityUtils.getCurrentUsername()); List<Integer> levels = roleService.findByUsersId(SecurityUtils.getCurrentUserId()).stream().map(RoleSmallDto::getLevel).collect(Collectors.toList());
List<Integer> levels = roleService.findByUsersId(user.getId()).stream().map(RoleSmallDto::getLevel).collect(Collectors.toList());
int min = Collections.min(levels); int min = Collections.min(levels);
if(level != null){ if(level != null){
if(level < min){ if(level < min){

View File

@ -128,8 +128,7 @@ public class UserController {
@ApiOperation("修改用户:个人中心") @ApiOperation("修改用户:个人中心")
@PutMapping(value = "center") @PutMapping(value = "center")
public ResponseEntity<Object> center(@Validated(User.Update.class) @RequestBody User resources){ public ResponseEntity<Object> center(@Validated(User.Update.class) @RequestBody User resources){
UserDto userDto = userService.findByName(SecurityUtils.getCurrentUsername()); if(!resources.getId().equals(SecurityUtils.getCurrentUserId())){
if(!resources.getId().equals(userDto.getId())){
throw new BadRequestException("不能修改他人资料"); throw new BadRequestException("不能修改他人资料");
} }
userService.updateCenter(resources); userService.updateCenter(resources);
@ -141,12 +140,11 @@ public class UserController {
@DeleteMapping @DeleteMapping
@PreAuthorize("@el.check('user:del')") @PreAuthorize("@el.check('user:del')")
public ResponseEntity<Object> delete(@RequestBody Set<Long> ids){ public ResponseEntity<Object> delete(@RequestBody Set<Long> ids){
UserDto user = userService.findByName(SecurityUtils.getCurrentUsername());
for (Long id : ids) { for (Long id : ids) {
Integer currentLevel = Collections.min(roleService.findByUsersId(user.getId()).stream().map(RoleSmallDto::getLevel).collect(Collectors.toList())); Integer currentLevel = Collections.min(roleService.findByUsersId(SecurityUtils.getCurrentUserId()).stream().map(RoleSmallDto::getLevel).collect(Collectors.toList()));
Integer optLevel = Collections.min(roleService.findByUsersId(id).stream().map(RoleSmallDto::getLevel).collect(Collectors.toList())); Integer optLevel = Collections.min(roleService.findByUsersId(id).stream().map(RoleSmallDto::getLevel).collect(Collectors.toList()));
if (currentLevel > optLevel) { if (currentLevel > optLevel) {
throw new BadRequestException("角色权限不足,不能删除:" + userService.findByName(SecurityUtils.getCurrentUsername()).getUsername()); throw new BadRequestException("角色权限不足,不能删除:" + userService.findById(id).getUsername());
} }
} }
userService.delete(ids); userService.delete(ids);
@ -200,8 +198,7 @@ public class UserController {
* @param resources / * @param resources /
*/ */
private void checkLevel(User resources) { private void checkLevel(User resources) {
UserDto user = userService.findByName(SecurityUtils.getCurrentUsername()); Integer currentLevel = Collections.min(roleService.findByUsersId(SecurityUtils.getCurrentUserId()).stream().map(RoleSmallDto::getLevel).collect(Collectors.toList()));
Integer currentLevel = Collections.min(roleService.findByUsersId(user.getId()).stream().map(RoleSmallDto::getLevel).collect(Collectors.toList()));
Integer optLevel = roleService.findByRoles(resources.getRoles()); Integer optLevel = roleService.findByRoles(resources.getRoles());
if (currentLevel > optLevel) { if (currentLevel > optLevel) {
throw new BadRequestException("角色权限不足"); throw new BadRequestException("角色权限不足");