refactor(role): refactor role management API and service
- Remove unused imports and simplify controller annotations - Change request mapping path from /api/role to /api/roles - Update assignRolesToUser endpoint to use POST /users - Update revokeRolesFromUser endpoint to use DELETE /users - Modify getAllRoles method to return OptionResponse instead of RoleResponse - Simplify database query in RoleServiceImpl using lambda query wrapper - Add proper authorization checks for all role management endpoints - Remove redundant getUserRole method with duplicate logic
This commit is contained in:
@@ -1,5 +1,6 @@
|
|||||||
package cn.nopj.chaos_api.service;
|
package cn.nopj.chaos_api.service;
|
||||||
|
|
||||||
|
import cn.nopj.chaos_api.dto.response.OptionResponse;
|
||||||
import cn.nopj.chaos_api.dto.response.RoleResponse;
|
import cn.nopj.chaos_api.dto.response.RoleResponse;
|
||||||
|
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
@@ -9,5 +10,5 @@ public interface RoleService {
|
|||||||
* 获取所有角色
|
* 获取所有角色
|
||||||
* @return 所有角色
|
* @return 所有角色
|
||||||
*/
|
*/
|
||||||
List<RoleResponse> getAllRoles();
|
List<OptionResponse> getAllRoles();
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -27,7 +27,6 @@ public class DeviceTypeServiceImpl extends ServiceImpl<DriveTypeMapper, DeviceTy
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public List<OptionResponse> getDeviceTypeOptions() {
|
public List<OptionResponse> getDeviceTypeOptions() {
|
||||||
LambdaQueryWrapper<DeviceType> wrapper = new LambdaQueryWrapper<>();
|
|
||||||
return this.lambdaQuery()
|
return this.lambdaQuery()
|
||||||
.select(DeviceType::getId, DeviceType::getName)
|
.select(DeviceType::getId, DeviceType::getName)
|
||||||
.list()
|
.list()
|
||||||
|
|||||||
@@ -1,6 +1,7 @@
|
|||||||
package cn.nopj.chaos_api.service.impl;
|
package cn.nopj.chaos_api.service.impl;
|
||||||
|
|
||||||
import cn.nopj.chaos_api.domain.entity.Role;
|
import cn.nopj.chaos_api.domain.entity.Role;
|
||||||
|
import cn.nopj.chaos_api.dto.response.OptionResponse;
|
||||||
import cn.nopj.chaos_api.dto.response.RoleResponse;
|
import cn.nopj.chaos_api.dto.response.RoleResponse;
|
||||||
import cn.nopj.chaos_api.mapper.RoleMapper;
|
import cn.nopj.chaos_api.mapper.RoleMapper;
|
||||||
import cn.nopj.chaos_api.service.RoleService;
|
import cn.nopj.chaos_api.service.RoleService;
|
||||||
@@ -12,7 +13,13 @@ import java.util.List;
|
|||||||
@Service
|
@Service
|
||||||
public class RoleServiceImpl extends ServiceImpl<RoleMapper, Role> implements RoleService {
|
public class RoleServiceImpl extends ServiceImpl<RoleMapper, Role> implements RoleService {
|
||||||
@Override
|
@Override
|
||||||
public List<RoleResponse> getAllRoles() {
|
public List<OptionResponse> getAllRoles() {
|
||||||
return this.baseMapper.selectList(null).stream().map(RoleResponse::new).toList();
|
|
||||||
|
return this.lambdaQuery()
|
||||||
|
.select(Role::getId, Role::getName)
|
||||||
|
.list()
|
||||||
|
.stream()
|
||||||
|
.map(role -> new OptionResponse(role.getName(), role.getId().toString()))
|
||||||
|
.toList();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -2,6 +2,7 @@ package cn.nopj.chaos_api.controller;
|
|||||||
|
|
||||||
|
|
||||||
import cn.nopj.chaos_api.dto.request.SetUserRoleRequest;
|
import cn.nopj.chaos_api.dto.request.SetUserRoleRequest;
|
||||||
|
import cn.nopj.chaos_api.dto.response.OptionResponse;
|
||||||
import cn.nopj.chaos_api.dto.response.RoleResponse;
|
import cn.nopj.chaos_api.dto.response.RoleResponse;
|
||||||
import cn.nopj.chaos_api.model.ApiResult;
|
import cn.nopj.chaos_api.model.ApiResult;
|
||||||
import cn.nopj.chaos_api.service.RoleService;
|
import cn.nopj.chaos_api.service.RoleService;
|
||||||
@@ -10,10 +11,7 @@ import lombok.extern.slf4j.Slf4j;
|
|||||||
import org.springframework.beans.factory.annotation.Autowired;
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
import org.springframework.security.access.prepost.PreAuthorize;
|
import org.springframework.security.access.prepost.PreAuthorize;
|
||||||
import org.springframework.validation.annotation.Validated;
|
import org.springframework.validation.annotation.Validated;
|
||||||
import org.springframework.web.bind.annotation.GetMapping;
|
import org.springframework.web.bind.annotation.*;
|
||||||
import org.springframework.web.bind.annotation.RequestBody;
|
|
||||||
import org.springframework.web.bind.annotation.RequestMapping;
|
|
||||||
import org.springframework.web.bind.annotation.RestController;
|
|
||||||
|
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
@@ -23,7 +21,7 @@ import java.util.List;
|
|||||||
*/
|
*/
|
||||||
@Slf4j
|
@Slf4j
|
||||||
@RestController
|
@RestController
|
||||||
@RequestMapping("/api/role")
|
@RequestMapping("/api/roles")
|
||||||
public class RoleController {
|
public class RoleController {
|
||||||
|
|
||||||
@Autowired
|
@Autowired
|
||||||
@@ -32,13 +30,24 @@ public class RoleController {
|
|||||||
@Autowired
|
@Autowired
|
||||||
private RoleService roleService;
|
private RoleService roleService;
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 获取用户角色
|
||||||
|
*/
|
||||||
|
@PreAuthorize("hasAuthority('admin')")
|
||||||
|
@GetMapping
|
||||||
|
public ApiResult<List<OptionResponse>> getUserRole() {
|
||||||
|
|
||||||
|
return ApiResult.success(roleService.getAllRoles());
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 设置用户角色
|
* 设置用户角色
|
||||||
* @param request 请求参数
|
* @param request 请求参数
|
||||||
* @return 处理结果
|
* @return 处理结果
|
||||||
*/
|
*/
|
||||||
@PreAuthorize("hasAuthority('admin')")
|
@PreAuthorize("hasAuthority('admin')")
|
||||||
@RequestMapping("/setUserRole")
|
@PostMapping("/users")
|
||||||
public ApiResult<String> assignRolesToUser(@RequestBody @Validated SetUserRoleRequest request) {
|
public ApiResult<String> assignRolesToUser(@RequestBody @Validated SetUserRoleRequest request) {
|
||||||
userRoleService.assignRolesToUser(request);
|
userRoleService.assignRolesToUser(request);
|
||||||
return ApiResult.success("用户角色设置成功");
|
return ApiResult.success("用户角色设置成功");
|
||||||
@@ -49,17 +58,10 @@ public class RoleController {
|
|||||||
* @return 处理结果
|
* @return 处理结果
|
||||||
*/
|
*/
|
||||||
@PreAuthorize("hasAuthority('admin')")
|
@PreAuthorize("hasAuthority('admin')")
|
||||||
@RequestMapping("/cancelUserRole")
|
@DeleteMapping("/users")
|
||||||
public ApiResult<?> revokeRolesFromUser(@RequestBody @Validated SetUserRoleRequest request) {
|
public ApiResult<?> revokeRolesFromUser(@RequestBody @Validated SetUserRoleRequest request) {
|
||||||
return ApiResult.success("用户角色取消成功",userRoleService.revokeRolesFromUser(request));
|
return ApiResult.success("用户角色取消成功",userRoleService.revokeRolesFromUser(request));
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* 获取用户角色
|
|
||||||
*/
|
|
||||||
@PreAuthorize("hasAuthority('admin')")
|
|
||||||
@GetMapping("/")
|
|
||||||
public ApiResult<List<RoleResponse>> getUserRole() {
|
|
||||||
return ApiResult.success(roleService.getAllRoles());
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user