feat(permission): 添加权限和审批控制器
- 添加 PermissionController: 权限CRUD、当前用户权限查询 - 添加 ApprovalController: 审批流API接口 - 添加 UserController: 用户角色分配接口 - 修复类型兼容性问题
This commit is contained in:
@@ -0,0 +1,66 @@
|
||||
package com.mosquito.project.permission;
|
||||
|
||||
import org.springframework.http.ResponseEntity;
|
||||
import org.springframework.web.bind.annotation.*;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* 用户权限控制器 - 处理用户角色分配
|
||||
*/
|
||||
@RestController
|
||||
@RequestMapping("/api/users")
|
||||
public class UserController {
|
||||
|
||||
private final UserRoleRepository userRoleRepository;
|
||||
private final RoleRepository roleRepository;
|
||||
|
||||
public UserController(UserRoleRepository userRoleRepository, RoleRepository roleRepository) {
|
||||
this.userRoleRepository = userRoleRepository;
|
||||
this.roleRepository = roleRepository;
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取用户角色
|
||||
*/
|
||||
@GetMapping("/{userId}/roles")
|
||||
public ResponseEntity<List<String>> getUserRoles(@PathVariable Long userId) {
|
||||
List<Long> roleIds = userRoleRepository.findRoleIdsByUserId(userId);
|
||||
List<String> roleCodes = roleIds.stream()
|
||||
.map(id -> roleRepository.findById(id))
|
||||
.filter(java.util.Optional::isPresent)
|
||||
.map(java.util.Optional::get)
|
||||
.map(SysRole::getRoleCode)
|
||||
.toList();
|
||||
return ResponseEntity.ok(roleCodes);
|
||||
}
|
||||
|
||||
/**
|
||||
* 分配角色给用户
|
||||
*/
|
||||
@PostMapping("/{userId}/roles")
|
||||
public ResponseEntity<Void> assignRoles(@PathVariable Long userId, @RequestBody AssignRolesRequest request) {
|
||||
// 删除现有角色关联
|
||||
userRoleRepository.deleteByUserId(userId);
|
||||
|
||||
// 创建新的角色关联
|
||||
for (Long roleId : request.getRoleIds()) {
|
||||
SysUserRole userRole = new SysUserRole();
|
||||
userRole.setUserId(userId);
|
||||
userRole.setRoleId(roleId);
|
||||
userRoleRepository.save(userRole);
|
||||
}
|
||||
|
||||
return ResponseEntity.ok().build();
|
||||
}
|
||||
|
||||
/**
|
||||
* 角色分配请求
|
||||
*/
|
||||
public static class AssignRolesRequest {
|
||||
private List<Long> roleIds;
|
||||
|
||||
public List<Long> getRoleIds() { return roleIds; }
|
||||
public void setRoleIds(List<Long> roleIds) { this.roleIds = roleIds; }
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user