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:
@@ -2,6 +2,7 @@ package cn.nopj.chaos_api.controller;
|
||||
|
||||
|
||||
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.model.ApiResult;
|
||||
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.security.access.prepost.PreAuthorize;
|
||||
import org.springframework.validation.annotation.Validated;
|
||||
import org.springframework.web.bind.annotation.GetMapping;
|
||||
import org.springframework.web.bind.annotation.RequestBody;
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
import org.springframework.web.bind.annotation.*;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
@@ -23,7 +21,7 @@ import java.util.List;
|
||||
*/
|
||||
@Slf4j
|
||||
@RestController
|
||||
@RequestMapping("/api/role")
|
||||
@RequestMapping("/api/roles")
|
||||
public class RoleController {
|
||||
|
||||
@Autowired
|
||||
@@ -32,13 +30,24 @@ public class RoleController {
|
||||
@Autowired
|
||||
private RoleService roleService;
|
||||
|
||||
|
||||
/**
|
||||
* 获取用户角色
|
||||
*/
|
||||
@PreAuthorize("hasAuthority('admin')")
|
||||
@GetMapping
|
||||
public ApiResult<List<OptionResponse>> getUserRole() {
|
||||
|
||||
return ApiResult.success(roleService.getAllRoles());
|
||||
}
|
||||
|
||||
/**
|
||||
* 设置用户角色
|
||||
* @param request 请求参数
|
||||
* @return 处理结果
|
||||
*/
|
||||
@PreAuthorize("hasAuthority('admin')")
|
||||
@RequestMapping("/setUserRole")
|
||||
@PostMapping("/users")
|
||||
public ApiResult<String> assignRolesToUser(@RequestBody @Validated SetUserRoleRequest request) {
|
||||
userRoleService.assignRolesToUser(request);
|
||||
return ApiResult.success("用户角色设置成功");
|
||||
@@ -49,17 +58,10 @@ public class RoleController {
|
||||
* @return 处理结果
|
||||
*/
|
||||
@PreAuthorize("hasAuthority('admin')")
|
||||
@RequestMapping("/cancelUserRole")
|
||||
@DeleteMapping("/users")
|
||||
public ApiResult<?> revokeRolesFromUser(@RequestBody @Validated SetUserRoleRequest 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