You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
85 lines
3.0 KiB
85 lines
3.0 KiB
package com.wok.supportbot.controller;
|
|
|
|
import com.wok.supportbot.service.SysRoleService;
|
|
import lombok.extern.slf4j.Slf4j;
|
|
import org.springframework.http.ResponseEntity;
|
|
import org.springframework.security.access.prepost.PreAuthorize;
|
|
import org.springframework.web.bind.annotation.*;
|
|
|
|
import java.util.Map;
|
|
|
|
/**
|
|
* 系统角色管理控制器
|
|
*/
|
|
@Slf4j
|
|
@RestController
|
|
@RequestMapping("/sys-role")
|
|
public class SysRoleController {
|
|
|
|
private final SysRoleService sysRoleService;
|
|
|
|
public SysRoleController(SysRoleService sysRoleService) {
|
|
this.sysRoleService = sysRoleService;
|
|
}
|
|
|
|
/**
|
|
* 获取所有角色列表
|
|
*/
|
|
@GetMapping("/list")
|
|
@PreAuthorize("hasRole('admin')")
|
|
public ResponseEntity<Map<String, Object>> list() {
|
|
try {
|
|
var roles = sysRoleService.listAll();
|
|
return ResponseEntity.ok(Map.of("success", true, "data", roles));
|
|
} catch (Exception e) {
|
|
log.error("查询角色列表失败", e);
|
|
return ResponseEntity.internalServerError().body(Map.of(
|
|
"success", false, "message", "查询失败: " + e.getMessage()
|
|
));
|
|
}
|
|
}
|
|
|
|
/**
|
|
* 创建角色
|
|
*/
|
|
@PostMapping
|
|
@PreAuthorize("hasRole('admin')")
|
|
public ResponseEntity<Map<String, Object>> create(@RequestBody Map<String, String> body) {
|
|
try {
|
|
String roleKey = body.get("roleKey");
|
|
String name = body.get("name");
|
|
String description = body.get("description");
|
|
sysRoleService.createRole(roleKey, name, description);
|
|
return ResponseEntity.ok(Map.of("success", true, "message", "创建成功"));
|
|
} catch (IllegalArgumentException e) {
|
|
return ResponseEntity.ok(Map.of("success", false, "message", e.getMessage()));
|
|
} catch (Exception e) {
|
|
log.error("创建角色失败", e);
|
|
return ResponseEntity.internalServerError().body(Map.of(
|
|
"success", false, "message", "创建失败: " + e.getMessage()
|
|
));
|
|
}
|
|
}
|
|
|
|
/**
|
|
* 更新角色
|
|
*/
|
|
@PutMapping("/{id}")
|
|
@PreAuthorize("hasRole('admin')")
|
|
public ResponseEntity<Map<String, Object>> update(@PathVariable Long id, @RequestBody Map<String, Object> body) {
|
|
try {
|
|
String name = (String) body.get("name");
|
|
String description = (String) body.get("description");
|
|
Boolean enabled = body.get("enabled") != null ? (Boolean) body.get("enabled") : null;
|
|
sysRoleService.updateRole(id, name, description, enabled);
|
|
return ResponseEntity.ok(Map.of("success", true, "message", "更新成功"));
|
|
} catch (IllegalArgumentException e) {
|
|
return ResponseEntity.ok(Map.of("success", false, "message", e.getMessage()));
|
|
} catch (Exception e) {
|
|
log.error("更新角色失败", e);
|
|
return ResponseEntity.internalServerError().body(Map.of(
|
|
"success", false, "message", "更新失败: " + e.getMessage()
|
|
));
|
|
}
|
|
}
|
|
}
|