RBAC 管理后台对齐老系统:用户/角色/部门/菜单/数据权限 增删改 + amis 管理页(A1–A5)
A1 SYS_MENU CRUD(软删+清role_menu+有子拒删)+menuAll isGroup+sys-admin-menu crud树
A2 用户 reset-password(MD5 110)+sys-admin-user 新建/编辑/停用/重置按钮
A3 SYS_ROLE CRUD(删清三关联表+evictAll)+sys-admin-role 新建/编辑/删除
A4 SYS_DEPART CRUD(有子拒删)+departs/tree 读端点+新页 sys-admin-depart
A5 sys_filterrule CRUD+set-roles+filterrules/{id}/roles+sys-admin-filterrule 授权UI
AdminWriteControllerTest 35测 + AdminUserControllerTest 3测;全部 API e2e 对真实库 + 浏览器验收
Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
This commit is contained in:
@@ -190,6 +190,7 @@ public class AdminController {
|
||||
List<Map<String, Object>> children = buildMenuChildren(menus, rootId, null);
|
||||
Map<String, Object> rootNode = new LinkedHashMap<>(root);
|
||||
rootNode.put("children", children);
|
||||
rootNode.put("isGroup", true); // A1:根分组行(sys_menu_root,非 sys_menu)→ 前端隐藏编辑/删除
|
||||
result.add(rootNode);
|
||||
}
|
||||
|
||||
@@ -235,6 +236,69 @@ public class AdminController {
|
||||
return children;
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------- 部门树(A4 管理视角)
|
||||
|
||||
/**
|
||||
* 全部门递归树(管理视角):返回 {@code {status,items:[{id,name,leader,seq,children:[...]}]}}。
|
||||
*
|
||||
* <p>按 parentid 链父子(顶层 = parentid 为空),供 A4 部门管理页 crud 树表消费。
|
||||
* 与 {@code /api/org/depart-tree/{orgId}/amis-tree}({label,value} 选择器格式)区别:
|
||||
* 本端点保留 leader/seq 管理字段、无需先选 org。
|
||||
*/
|
||||
@Operation(summary = "全部门递归树(管理视角,含 leader/seq)")
|
||||
@GetMapping("/api/admin/departs/tree")
|
||||
public Map<String, Object> departTree() {
|
||||
List<Map<String, Object>> all = jdbcTemplate.query(
|
||||
"SELECT TRIM(sys_depart_id) AS id, TRIM(name) AS name, TRIM(parentid) AS parentid,"
|
||||
+ " TRIM(leader) AS leader, seq FROM sys_depart WHERE isactive=1 ORDER BY seq",
|
||||
(rs, i) -> {
|
||||
Map<String, Object> m = new LinkedHashMap<>();
|
||||
m.put("id", rs.getString("id"));
|
||||
m.put("name", rs.getString("name"));
|
||||
m.put("parentid", rs.getString("parentid"));
|
||||
m.put("leader", rs.getString("leader"));
|
||||
m.put("seq", rs.getObject("seq"));
|
||||
return m;
|
||||
});
|
||||
Map<String, Object> resp = new LinkedHashMap<>();
|
||||
resp.put("status", 0);
|
||||
resp.put("msg", "");
|
||||
resp.put("items", buildDepartChildren(all, null));
|
||||
return resp;
|
||||
}
|
||||
|
||||
private List<Map<String, Object>> buildDepartChildren(List<Map<String, Object>> all, String parentId) {
|
||||
List<Map<String, Object>> children = new ArrayList<>();
|
||||
for (Map<String, Object> m : all) {
|
||||
String p = (String) m.get("parentid");
|
||||
boolean match = (parentId == null || parentId.isBlank())
|
||||
? (p == null || p.isBlank())
|
||||
: parentId.equals(p);
|
||||
if (match) {
|
||||
Map<String, Object> node = new LinkedHashMap<>(m);
|
||||
node.put("children", buildDepartChildren(all, (String) m.get("id")));
|
||||
children.add(node);
|
||||
}
|
||||
}
|
||||
return children;
|
||||
}
|
||||
|
||||
/**
|
||||
* 取某条数据权限规则当前绑定的角色 ID 列表(A5 角色配置回填)。
|
||||
*/
|
||||
@Operation(summary = "规则当前绑定的角色 ID 列表")
|
||||
@GetMapping("/api/admin/filterrules/{id}/roles")
|
||||
public Map<String, Object> filterRuleRoles(@PathVariable String id) {
|
||||
List<String> roleIds = jdbcTemplate.queryForList(
|
||||
"SELECT TRIM(roleid) FROM sys_filterrulerole WHERE TRIM(filterruleid)=?",
|
||||
String.class, id.trim());
|
||||
Map<String, Object> resp = new LinkedHashMap<>();
|
||||
resp.put("status", 0);
|
||||
resp.put("msg", "");
|
||||
resp.put("roleIds", roleIds);
|
||||
return resp;
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------- 数据权限规则
|
||||
|
||||
/**
|
||||
|
||||
@@ -301,8 +301,364 @@ public class AdminWriteController {
|
||||
return ResponseEntity.ok(ok("角色菜单保存成功", data));
|
||||
}
|
||||
|
||||
// ================================================================ 菜单 CRUD(A1)
|
||||
|
||||
/**
|
||||
* 新建菜单。
|
||||
*
|
||||
* @param body {@code {name*, parentId?, viewTemplateId?, typename?, menuroot?, seq?}}
|
||||
*/
|
||||
@Operation(summary = "新建菜单", description = "仅系统管理员可调;挂在 parentId 下(顶级则空)")
|
||||
@PostMapping("/menus/create")
|
||||
public ResponseEntity<Map<String, Object>> createMenu(@RequestBody Map<String, Object> body) {
|
||||
requireAdmin();
|
||||
String name = requireString(body, "name", "菜单名称不能为空");
|
||||
String newId = UUID.randomUUID().toString();
|
||||
long now = System.currentTimeMillis();
|
||||
String operator = currentUserId();
|
||||
jdbcTemplate.update(
|
||||
"INSERT INTO sys_menu(sys_menu_id, name, parentid, view_templateid, typename, menuroot, seq,"
|
||||
+ " isactive, version, createtime, createby, updatetime, updateby)"
|
||||
+ " VALUES(?,?,?,?,?,?,?,1,0,?,?,?,?)",
|
||||
newId, name.trim(),
|
||||
trimOrNull(body.get("parentId")),
|
||||
trimOrNull(body.get("viewTemplateId")),
|
||||
trimOrNull(body.get("typename")),
|
||||
trimOrNull(body.get("menuroot")),
|
||||
toInt(body.get("seq"), 0),
|
||||
now, operator, now, operator);
|
||||
log.info("[A1][菜单新建] operator={} menuId={} name={} parentId={}",
|
||||
operator, newId, name.trim(), trimOrNull(body.get("parentId")));
|
||||
return ResponseEntity.ok(okWithId("菜单新建成功", newId));
|
||||
}
|
||||
|
||||
/**
|
||||
* 编辑菜单(名称/上级/模板/类型/排序)。
|
||||
*
|
||||
* @param id SYS_MENU.SYS_MENU_ID
|
||||
* @param body {@code {name*, parentId?, viewTemplateId?, typename?, seq?}}
|
||||
*/
|
||||
@Operation(summary = "编辑菜单(名称/模板/类型/排序,不改父子层级)", description = "仅系统管理员可调")
|
||||
@PostMapping("/menus/{id}/update")
|
||||
public ResponseEntity<Map<String, Object>> updateMenu(
|
||||
@PathVariable String id, @RequestBody Map<String, Object> body) {
|
||||
requireAdmin();
|
||||
String name = requireString(body, "name", "菜单名称不能为空");
|
||||
long now = System.currentTimeMillis();
|
||||
String operator = currentUserId();
|
||||
// 不更新 parentid/menuroot:编辑表单不带这些字段时不应把菜单误移到顶层。
|
||||
// 改变父子层级属“移动”操作,留作单独端点。
|
||||
int rows = jdbcTemplate.update(
|
||||
"UPDATE sys_menu SET name=?, view_templateid=?, typename=?, seq=?,"
|
||||
+ " updatetime=?, updateby=?, version=version+1"
|
||||
+ " WHERE TRIM(sys_menu_id)=? AND isactive=1",
|
||||
name.trim(),
|
||||
trimOrNull(body.get("viewTemplateId")),
|
||||
trimOrNull(body.get("typename")),
|
||||
toInt(body.get("seq"), 0),
|
||||
now, operator, id.trim());
|
||||
if (rows == 0) {
|
||||
return ResponseEntity.status(HttpStatus.NOT_FOUND).body(fail("菜单不存在: " + id));
|
||||
}
|
||||
log.info("[A1][菜单编辑] operator={} menuId={} name={}", operator, id.trim(), name.trim());
|
||||
return ResponseEntity.ok(ok("菜单更新成功"));
|
||||
}
|
||||
|
||||
/**
|
||||
* 删除菜单(软删 isactive=0,级联清 sys_role_menu)。
|
||||
*
|
||||
* <p>有未删子菜单时拒绝(防孤儿节点)——须先删子级。
|
||||
*
|
||||
* @param id SYS_MENU.SYS_MENU_ID
|
||||
*/
|
||||
@Operation(summary = "删除菜单(软删+清角色授权)",
|
||||
description = "仅系统管理员可调;有子菜单时拒绝,须先删子级")
|
||||
@PostMapping("/menus/{id}/delete")
|
||||
@Transactional
|
||||
public ResponseEntity<Map<String, Object>> deleteMenu(@PathVariable String id) {
|
||||
requireAdmin();
|
||||
String mid = id.trim();
|
||||
Integer childCount = jdbcTemplate.queryForObject(
|
||||
"SELECT COUNT(*) FROM sys_menu WHERE TRIM(parentid)=? AND isactive=1",
|
||||
Integer.class, mid);
|
||||
if (childCount != null && childCount > 0) {
|
||||
return badRequest("该菜单下还有 " + childCount + " 个子菜单,请先删除子菜单");
|
||||
}
|
||||
// 清角色授权(否则软删菜单仍残留在 sys_role_menu)
|
||||
jdbcTemplate.update("DELETE FROM sys_role_menu WHERE TRIM(menuid)=?", mid);
|
||||
long now = System.currentTimeMillis();
|
||||
String operator = currentUserId();
|
||||
int rows = jdbcTemplate.update(
|
||||
"UPDATE sys_menu SET isactive=0, updatetime=?, updateby=? WHERE TRIM(sys_menu_id)=?",
|
||||
now, operator, mid);
|
||||
if (rows == 0) {
|
||||
return ResponseEntity.status(HttpStatus.NOT_FOUND).body(fail("菜单不存在: " + id));
|
||||
}
|
||||
log.info("[A1][菜单删除] operator={} menuId={}", operator, mid);
|
||||
return ResponseEntity.ok(ok("菜单删除成功"));
|
||||
}
|
||||
|
||||
// ================================================================ 角色 CRUD(A3)
|
||||
|
||||
@Operation(summary = "新建角色", description = "仅系统管理员可调;roletype 1=功能 2=数据")
|
||||
@PostMapping("/roles/create")
|
||||
public ResponseEntity<Map<String, Object>> createRole(@RequestBody Map<String, Object> body) {
|
||||
requireAdmin();
|
||||
String name = requireString(body, "name", "角色名称不能为空");
|
||||
String newId = UUID.randomUUID().toString();
|
||||
long now = System.currentTimeMillis();
|
||||
String operator = currentUserId();
|
||||
jdbcTemplate.update(
|
||||
"INSERT INTO sys_role(sys_role_id, name, description, roletype, isactive, version,"
|
||||
+ " createtime, createby, updatetime, updateby) VALUES(?,?,?,?,1,0,?,?,?,?)",
|
||||
newId, name.trim(), trimOrNull(body.get("description")),
|
||||
toInt(body.get("roletype"), 1), now, operator, now, operator);
|
||||
log.info("[A3][角色新建] operator={} roleId={} name={}", operator, newId, name.trim());
|
||||
return ResponseEntity.ok(okWithId("角色新建成功", newId));
|
||||
}
|
||||
|
||||
@Operation(summary = "编辑角色", description = "仅系统管理员可调")
|
||||
@PostMapping("/roles/{id}/update")
|
||||
public ResponseEntity<Map<String, Object>> updateRole(
|
||||
@PathVariable String id, @RequestBody Map<String, Object> body) {
|
||||
requireAdmin();
|
||||
String name = requireString(body, "name", "角色名称不能为空");
|
||||
long now = System.currentTimeMillis();
|
||||
String operator = currentUserId();
|
||||
int rows = jdbcTemplate.update(
|
||||
"UPDATE sys_role SET name=?, description=?, roletype=?, updatetime=?, updateby=?,"
|
||||
+ " version=version+1 WHERE TRIM(sys_role_id)=? AND isactive=1",
|
||||
name.trim(), trimOrNull(body.get("description")),
|
||||
toInt(body.get("roletype"), 1), now, operator, id.trim());
|
||||
if (rows == 0) {
|
||||
return ResponseEntity.status(HttpStatus.NOT_FOUND).body(fail("角色不存在: " + id));
|
||||
}
|
||||
log.info("[A3][角色编辑] operator={} roleId={} name={}", operator, id.trim(), name.trim());
|
||||
return ResponseEntity.ok(ok("角色更新成功"));
|
||||
}
|
||||
|
||||
@Operation(summary = "删除角色(软删+清菜单/用户组/数据权限关联)", description = "仅系统管理员可调")
|
||||
@PostMapping("/roles/{id}/delete")
|
||||
@Transactional
|
||||
public ResponseEntity<Map<String, Object>> deleteRole(@PathVariable String id) {
|
||||
requireAdmin();
|
||||
String rid = id.trim();
|
||||
jdbcTemplate.update("DELETE FROM sys_role_menu WHERE TRIM(roleid)=?", rid);
|
||||
jdbcTemplate.update("DELETE FROM sys_usergrouprole WHERE TRIM(roleid)=?", rid);
|
||||
jdbcTemplate.update("DELETE FROM sys_filterrulerole WHERE TRIM(roleid)=?", rid);
|
||||
long now = System.currentTimeMillis();
|
||||
String operator = currentUserId();
|
||||
int rows = jdbcTemplate.update(
|
||||
"UPDATE sys_role SET isactive=0, updatetime=?, updateby=? WHERE TRIM(sys_role_id)=?",
|
||||
now, operator, rid);
|
||||
if (rows == 0) {
|
||||
return ResponseEntity.status(HttpStatus.NOT_FOUND).body(fail("角色不存在: " + id));
|
||||
}
|
||||
roleResolver.evictAll();
|
||||
log.info("[A3][角色删除] operator={} roleId={}", operator, rid);
|
||||
return ResponseEntity.ok(ok("角色删除成功"));
|
||||
}
|
||||
|
||||
// ================================================================ 部门 CRUD(A4)
|
||||
|
||||
@Operation(summary = "新建部门", description = "仅系统管理员可调;挂在 parentId 下")
|
||||
@PostMapping("/departs/create")
|
||||
public ResponseEntity<Map<String, Object>> createDepart(@RequestBody Map<String, Object> body) {
|
||||
requireAdmin();
|
||||
String name = requireString(body, "name", "部门名称不能为空");
|
||||
String newId = UUID.randomUUID().toString();
|
||||
long now = System.currentTimeMillis();
|
||||
String operator = currentUserId();
|
||||
jdbcTemplate.update(
|
||||
"INSERT INTO sys_depart(sys_depart_id, name, parentid, parentorgid, leader, description,"
|
||||
+ " seq, isactive, version, createtime, createby, updatetime, updateby)"
|
||||
+ " VALUES(?,?,?,?,?,?,?,1,0,?,?,?,?)",
|
||||
newId, name.trim(),
|
||||
trimOrNull(body.get("parentId")),
|
||||
trimOrNull(body.get("parentOrgId")),
|
||||
trimOrNull(body.get("leader")),
|
||||
trimOrNull(body.get("description")),
|
||||
toInt(body.get("seq"), 0),
|
||||
now, operator, now, operator);
|
||||
log.info("[A4][部门新建] operator={} departId={} name={} parentId={}",
|
||||
operator, newId, name.trim(), trimOrNull(body.get("parentId")));
|
||||
return ResponseEntity.ok(okWithId("部门新建成功", newId));
|
||||
}
|
||||
|
||||
@Operation(summary = "编辑部门(名称/负责人/描述/排序,不改父子层级)", description = "仅系统管理员可调")
|
||||
@PostMapping("/departs/{id}/update")
|
||||
public ResponseEntity<Map<String, Object>> updateDepart(
|
||||
@PathVariable String id, @RequestBody Map<String, Object> body) {
|
||||
requireAdmin();
|
||||
String name = requireString(body, "name", "部门名称不能为空");
|
||||
long now = System.currentTimeMillis();
|
||||
String operator = currentUserId();
|
||||
int rows = jdbcTemplate.update(
|
||||
"UPDATE sys_depart SET name=?, leader=?, description=?, seq=?, updatetime=?, updateby=?,"
|
||||
+ " version=version+1 WHERE TRIM(sys_depart_id)=? AND isactive=1",
|
||||
name.trim(), trimOrNull(body.get("leader")), trimOrNull(body.get("description")),
|
||||
toInt(body.get("seq"), 0), now, operator, id.trim());
|
||||
if (rows == 0) {
|
||||
return ResponseEntity.status(HttpStatus.NOT_FOUND).body(fail("部门不存在: " + id));
|
||||
}
|
||||
log.info("[A4][部门编辑] operator={} departId={} name={}", operator, id.trim(), name.trim());
|
||||
return ResponseEntity.ok(ok("部门更新成功"));
|
||||
}
|
||||
|
||||
@Operation(summary = "删除部门(软删;有子部门时拒绝)", description = "仅系统管理员可调")
|
||||
@PostMapping("/departs/{id}/delete")
|
||||
@Transactional
|
||||
public ResponseEntity<Map<String, Object>> deleteDepart(@PathVariable String id) {
|
||||
requireAdmin();
|
||||
String did = id.trim();
|
||||
Integer childCount = jdbcTemplate.queryForObject(
|
||||
"SELECT COUNT(*) FROM sys_depart WHERE TRIM(parentid)=? AND isactive=1",
|
||||
Integer.class, did);
|
||||
if (childCount != null && childCount > 0) {
|
||||
return badRequest("该部门下还有 " + childCount + " 个子部门,请先删除子部门");
|
||||
}
|
||||
long now = System.currentTimeMillis();
|
||||
String operator = currentUserId();
|
||||
int rows = jdbcTemplate.update(
|
||||
"UPDATE sys_depart SET isactive=0, updatetime=?, updateby=? WHERE TRIM(sys_depart_id)=?",
|
||||
now, operator, did);
|
||||
if (rows == 0) {
|
||||
return ResponseEntity.status(HttpStatus.NOT_FOUND).body(fail("部门不存在: " + id));
|
||||
}
|
||||
log.info("[A4][部门删除] operator={} departId={}", operator, did);
|
||||
return ResponseEntity.ok(ok("部门删除成功"));
|
||||
}
|
||||
|
||||
// ================================================================ 数据权限规则 CRUD(A5)
|
||||
|
||||
@Operation(summary = "新建数据权限规则", description = "仅系统管理员可调;entityid=SYS_ENTITY 主键,filterrule=WHERE 子句")
|
||||
@PostMapping("/filterrules/create")
|
||||
@Transactional
|
||||
public ResponseEntity<Map<String, Object>> createFilterRule(@RequestBody Map<String, Object> body) {
|
||||
requireAdmin();
|
||||
String entityId = requireString(body, "entityid", "实体 ID 不能为空");
|
||||
String rule = requireString(body, "filterrule", "规则内容不能为空");
|
||||
String newId = UUID.randomUUID().toString();
|
||||
long now = System.currentTimeMillis();
|
||||
String operator = currentUserId();
|
||||
jdbcTemplate.update(
|
||||
"INSERT INTO sys_filterrule(sys_filterrule_id, entityid, filterrule, isactive, version,"
|
||||
+ " createtime, createby, updatetime, updateby) VALUES(?,?,?,1,0,?,?,?,?)",
|
||||
newId, entityId.trim(), rule.trim(), now, operator, now, operator);
|
||||
bindFilterRuleRoles(newId, body.get("roleIds"));
|
||||
log.info("[A5][规则新建] operator={} ruleId={} entityId={}", operator, newId, entityId.trim());
|
||||
return ResponseEntity.ok(okWithId("数据权限规则新建成功", newId));
|
||||
}
|
||||
|
||||
@Operation(summary = "编辑数据权限规则内容", description = "仅系统管理员可调")
|
||||
@PostMapping("/filterrules/{id}/update")
|
||||
public ResponseEntity<Map<String, Object>> updateFilterRule(
|
||||
@PathVariable String id, @RequestBody Map<String, Object> body) {
|
||||
requireAdmin();
|
||||
String rule = requireString(body, "filterrule", "规则内容不能为空");
|
||||
long now = System.currentTimeMillis();
|
||||
String operator = currentUserId();
|
||||
int rows = jdbcTemplate.update(
|
||||
"UPDATE sys_filterrule SET filterrule=?, updatetime=?, updateby=?, version=version+1"
|
||||
+ " WHERE TRIM(sys_filterrule_id)=? AND isactive=1",
|
||||
rule.trim(), now, operator, id.trim());
|
||||
if (rows == 0) {
|
||||
return ResponseEntity.status(HttpStatus.NOT_FOUND).body(fail("规则不存在: " + id));
|
||||
}
|
||||
log.info("[A5][规则编辑] operator={} ruleId={}", operator, id.trim());
|
||||
return ResponseEntity.ok(ok("规则更新成功"));
|
||||
}
|
||||
|
||||
@Operation(summary = "删除数据权限规则(软删+清角色关联)", description = "仅系统管理员可调")
|
||||
@PostMapping("/filterrules/{id}/delete")
|
||||
@Transactional
|
||||
public ResponseEntity<Map<String, Object>> deleteFilterRule(@PathVariable String id) {
|
||||
requireAdmin();
|
||||
String fid = id.trim();
|
||||
jdbcTemplate.update("DELETE FROM sys_filterrulerole WHERE TRIM(filterruleid)=?", fid);
|
||||
long now = System.currentTimeMillis();
|
||||
String operator = currentUserId();
|
||||
int rows = jdbcTemplate.update(
|
||||
"UPDATE sys_filterrule SET isactive=0, updatetime=?, updateby=? WHERE TRIM(sys_filterrule_id)=?",
|
||||
now, operator, fid);
|
||||
if (rows == 0) {
|
||||
return ResponseEntity.status(HttpStatus.NOT_FOUND).body(fail("规则不存在: " + id));
|
||||
}
|
||||
log.info("[A5][规则删除] operator={} ruleId={}", operator, fid);
|
||||
return ResponseEntity.ok(ok("规则删除成功"));
|
||||
}
|
||||
|
||||
@Operation(summary = "规则角色整存(差量)", description = "仅系统管理员可调;空 roleIds 表示清空")
|
||||
@PostMapping("/filterrules/{id}/set-roles")
|
||||
@Transactional
|
||||
public ResponseEntity<Map<String, Object>> setFilterRuleRoles(
|
||||
@PathVariable String id, @RequestBody Map<String, Object> body) {
|
||||
requireAdmin();
|
||||
if (body.get("roleIds") == null) {
|
||||
return badRequest("roleIds 字段不能为空(传空数组表示清空)");
|
||||
}
|
||||
Map<String, Object> data = bindFilterRuleRoles(id.trim(), body.get("roleIds"));
|
||||
log.info("[A5][规则角色整存] operator={} ruleId={} {}", currentUserId(), id.trim(), data);
|
||||
return ResponseEntity.ok(ok("规则角色保存成功", data));
|
||||
}
|
||||
|
||||
/** 差量整存规则↔角色(filterRuleId 为目标规则),返回 inserted/deleted/total。 */
|
||||
private Map<String, Object> bindFilterRuleRoles(String ruleId, Object roleIdsObj) {
|
||||
List<String> target = new java.util.ArrayList<>();
|
||||
if (roleIdsObj instanceof List<?> raw) {
|
||||
for (Object o : raw) {
|
||||
if (o != null && !o.toString().isBlank()) target.add(o.toString().trim());
|
||||
}
|
||||
}
|
||||
target = target.stream().distinct().toList();
|
||||
List<String> current = jdbcTemplate.queryForList(
|
||||
"SELECT TRIM(roleid) FROM sys_filterrulerole WHERE TRIM(filterruleid)=?", String.class, ruleId);
|
||||
int inserted = 0;
|
||||
for (String r : target) {
|
||||
if (!current.contains(r)) {
|
||||
jdbcTemplate.update(
|
||||
"INSERT INTO sys_filterrulerole(filterruleid, roleid) VALUES(?,?) ON CONFLICT DO NOTHING",
|
||||
ruleId, r);
|
||||
inserted++;
|
||||
}
|
||||
}
|
||||
int deleted = 0;
|
||||
for (String r : current) {
|
||||
if (!target.contains(r)) {
|
||||
jdbcTemplate.update(
|
||||
"DELETE FROM sys_filterrulerole WHERE TRIM(filterruleid)=? AND TRIM(roleid)=?",
|
||||
ruleId, r);
|
||||
deleted++;
|
||||
}
|
||||
}
|
||||
Map<String, Object> data = new LinkedHashMap<>();
|
||||
data.put("inserted", inserted);
|
||||
data.put("deleted", deleted);
|
||||
data.put("total", target.size());
|
||||
return data;
|
||||
}
|
||||
|
||||
// ================================================================ 内部工具
|
||||
|
||||
/** 取字符串并 trim,空白/缺失返 null(写入可空列)。 */
|
||||
private static String trimOrNull(Object v) {
|
||||
if (v == null) return null;
|
||||
String s = v.toString().trim();
|
||||
return s.isEmpty() ? null : s;
|
||||
}
|
||||
|
||||
/** 宽松解析整型(amis 表单可能送字符串/数字),失败回退默认值。 */
|
||||
private static int toInt(Object v, int dft) {
|
||||
if (v == null) return dft;
|
||||
if (v instanceof Number n) return n.intValue();
|
||||
try {
|
||||
String s = v.toString().trim();
|
||||
return s.isEmpty() ? dft : Integer.parseInt(s);
|
||||
} catch (NumberFormatException e) {
|
||||
return dft;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 管理员身份检查:userId='ADMIN' 或其对应 account='admin',否则 403。
|
||||
*/
|
||||
|
||||
@@ -255,6 +255,261 @@ class AdminWriteControllerTest {
|
||||
assertEquals(HttpStatus.BAD_REQUEST, resp.getStatusCode());
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------- 菜单 CRUD(A1)
|
||||
|
||||
@Test
|
||||
@DisplayName("15. 新建菜单 → status=0, id 非空")
|
||||
void createMenu_ok() {
|
||||
mockAdmin();
|
||||
ResponseEntity<Map<String, Object>> resp =
|
||||
controller.createMenu(Map.of("name", "测试菜单", "parentId", "p-1", "seq", 3));
|
||||
assertEquals(HttpStatus.OK, resp.getStatusCode());
|
||||
assertEquals(0, resp.getBody().get("status"));
|
||||
assertNotNull(((Map<String, Object>) resp.getBody().get("data")).get("id"));
|
||||
}
|
||||
|
||||
@Test
|
||||
@DisplayName("16. 非管理员新建菜单 → 403")
|
||||
void createMenu_nonAdmin_403() {
|
||||
mockNonAdmin();
|
||||
ResponseStatusException ex = assertThrows(ResponseStatusException.class,
|
||||
() -> controller.createMenu(Map.of("name", "x")));
|
||||
assertEquals(HttpStatus.FORBIDDEN, ex.getStatusCode());
|
||||
}
|
||||
|
||||
@Test
|
||||
@DisplayName("17. 新建菜单缺 name → 400")
|
||||
void createMenu_missingName_400() {
|
||||
mockAdmin();
|
||||
ResponseStatusException ex = assertThrows(ResponseStatusException.class,
|
||||
() -> controller.createMenu(Map.of("seq", 1)));
|
||||
assertEquals(HttpStatus.BAD_REQUEST, ex.getStatusCode());
|
||||
}
|
||||
|
||||
@Test
|
||||
@DisplayName("18. 编辑菜单 → status=0")
|
||||
void updateMenu_ok() {
|
||||
mockAdmin();
|
||||
when(jdbc.update(contains("UPDATE sys_menu SET name"), any(), any(), any(),
|
||||
any(), any(), any(), any())).thenReturn(1);
|
||||
ResponseEntity<Map<String, Object>> resp =
|
||||
controller.updateMenu("m-1", Map.of("name", "改名", "seq", 5));
|
||||
assertEquals(HttpStatus.OK, resp.getStatusCode());
|
||||
assertEquals(0, resp.getBody().get("status"));
|
||||
}
|
||||
|
||||
@Test
|
||||
@DisplayName("19. 编辑菜单不存在 → 404")
|
||||
void updateMenu_notFound_404() {
|
||||
mockAdmin();
|
||||
when(jdbc.update(contains("UPDATE sys_menu SET name"), any(), any(), any(),
|
||||
any(), any(), any(), any())).thenReturn(0);
|
||||
ResponseEntity<Map<String, Object>> resp =
|
||||
controller.updateMenu("no-such", Map.of("name", "改名"));
|
||||
assertEquals(HttpStatus.NOT_FOUND, resp.getStatusCode());
|
||||
}
|
||||
|
||||
@Test
|
||||
@DisplayName("20. 删除菜单(无子菜单)→ status=0,清 role_menu + 软删")
|
||||
void deleteMenu_ok() {
|
||||
mockAdmin();
|
||||
when(jdbc.queryForObject(contains("COUNT(*) FROM sys_menu"), eq(Integer.class), anyString()))
|
||||
.thenReturn(0);
|
||||
when(jdbc.update(contains("DELETE FROM sys_role_menu"), anyString())).thenReturn(0);
|
||||
when(jdbc.update(contains("UPDATE sys_menu SET isactive=0"), any(), any(), anyString()))
|
||||
.thenReturn(1);
|
||||
ResponseEntity<Map<String, Object>> resp = controller.deleteMenu("m-1");
|
||||
assertEquals(HttpStatus.OK, resp.getStatusCode());
|
||||
verify(jdbc, atLeastOnce()).update(contains("DELETE FROM sys_role_menu"), anyString());
|
||||
}
|
||||
|
||||
@Test
|
||||
@DisplayName("21. 删除菜单(有子菜单)→ 400,拒绝软删")
|
||||
void deleteMenu_withChildren_blocked() {
|
||||
mockAdmin();
|
||||
when(jdbc.queryForObject(contains("COUNT(*) FROM sys_menu"), eq(Integer.class), anyString()))
|
||||
.thenReturn(2);
|
||||
ResponseEntity<Map<String, Object>> resp = controller.deleteMenu("parent-1");
|
||||
assertEquals(HttpStatus.BAD_REQUEST, resp.getStatusCode());
|
||||
verify(jdbc, never()).update(contains("UPDATE sys_menu SET isactive=0"), any(), any(), anyString());
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------- 角色 CRUD(A3)
|
||||
|
||||
@Test
|
||||
@DisplayName("22. 新建角色 → status=0, id 非空")
|
||||
void createRole_ok() {
|
||||
mockAdmin();
|
||||
ResponseEntity<Map<String, Object>> resp =
|
||||
controller.createRole(Map.of("name", "测试角色", "roletype", 1));
|
||||
assertEquals(HttpStatus.OK, resp.getStatusCode());
|
||||
assertEquals(0, resp.getBody().get("status"));
|
||||
assertNotNull(((Map<String, Object>) resp.getBody().get("data")).get("id"));
|
||||
}
|
||||
|
||||
@Test
|
||||
@DisplayName("23. 编辑角色 → status=0")
|
||||
void updateRole_ok() {
|
||||
mockAdmin();
|
||||
when(jdbc.update(contains("UPDATE sys_role SET name"), any(), any(), any(),
|
||||
any(), any(), any())).thenReturn(1);
|
||||
ResponseEntity<Map<String, Object>> resp =
|
||||
controller.updateRole("r-1", Map.of("name", "改名"));
|
||||
assertEquals(HttpStatus.OK, resp.getStatusCode());
|
||||
assertEquals(0, resp.getBody().get("status"));
|
||||
}
|
||||
|
||||
@Test
|
||||
@DisplayName("24. 编辑角色不存在 → 404")
|
||||
void updateRole_notFound_404() {
|
||||
mockAdmin();
|
||||
when(jdbc.update(contains("UPDATE sys_role SET name"), any(), any(), any(),
|
||||
any(), any(), any())).thenReturn(0);
|
||||
ResponseEntity<Map<String, Object>> resp =
|
||||
controller.updateRole("no-such", Map.of("name", "改名"));
|
||||
assertEquals(HttpStatus.NOT_FOUND, resp.getStatusCode());
|
||||
}
|
||||
|
||||
@Test
|
||||
@DisplayName("25. 删除角色 → status=0,清三张关联表 + evictAll")
|
||||
void deleteRole_ok() {
|
||||
mockAdmin();
|
||||
when(jdbc.update(contains("DELETE FROM sys_role_menu"), anyString())).thenReturn(0);
|
||||
when(jdbc.update(contains("DELETE FROM sys_usergrouprole"), anyString())).thenReturn(0);
|
||||
when(jdbc.update(contains("DELETE FROM sys_filterrulerole"), anyString())).thenReturn(0);
|
||||
when(jdbc.update(contains("UPDATE sys_role SET isactive=0"), any(), any(), anyString()))
|
||||
.thenReturn(1);
|
||||
ResponseEntity<Map<String, Object>> resp = controller.deleteRole("r-1");
|
||||
assertEquals(HttpStatus.OK, resp.getStatusCode());
|
||||
verify(jdbc, atLeastOnce()).update(contains("DELETE FROM sys_filterrulerole"), anyString());
|
||||
verify(roleResolver).evictAll();
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------- 部门 CRUD(A4)
|
||||
|
||||
@Test
|
||||
@DisplayName("26. 新建部门 → status=0, id 非空")
|
||||
void createDepart_ok() {
|
||||
mockAdmin();
|
||||
ResponseEntity<Map<String, Object>> resp =
|
||||
controller.createDepart(Map.of("name", "测试部门", "parentId", "d-0"));
|
||||
assertEquals(HttpStatus.OK, resp.getStatusCode());
|
||||
assertEquals(0, resp.getBody().get("status"));
|
||||
assertNotNull(((Map<String, Object>) resp.getBody().get("data")).get("id"));
|
||||
}
|
||||
|
||||
@Test
|
||||
@DisplayName("27. 编辑部门 → status=0")
|
||||
void updateDepart_ok() {
|
||||
mockAdmin();
|
||||
when(jdbc.update(contains("UPDATE sys_depart SET name"), any(), any(), any(),
|
||||
any(), any(), any(), any())).thenReturn(1);
|
||||
ResponseEntity<Map<String, Object>> resp =
|
||||
controller.updateDepart("d-1", Map.of("name", "改名", "leader", "张三"));
|
||||
assertEquals(HttpStatus.OK, resp.getStatusCode());
|
||||
assertEquals(0, resp.getBody().get("status"));
|
||||
}
|
||||
|
||||
@Test
|
||||
@DisplayName("28. 编辑部门不存在 → 404")
|
||||
void updateDepart_notFound_404() {
|
||||
mockAdmin();
|
||||
when(jdbc.update(contains("UPDATE sys_depart SET name"), any(), any(), any(),
|
||||
any(), any(), any(), any())).thenReturn(0);
|
||||
ResponseEntity<Map<String, Object>> resp =
|
||||
controller.updateDepart("no-such", Map.of("name", "改名"));
|
||||
assertEquals(HttpStatus.NOT_FOUND, resp.getStatusCode());
|
||||
}
|
||||
|
||||
@Test
|
||||
@DisplayName("29. 删除部门(无子部门)→ status=0")
|
||||
void deleteDepart_ok() {
|
||||
mockAdmin();
|
||||
when(jdbc.queryForObject(contains("COUNT(*) FROM sys_depart"), eq(Integer.class), anyString()))
|
||||
.thenReturn(0);
|
||||
when(jdbc.update(contains("UPDATE sys_depart SET isactive=0"), any(), any(), anyString()))
|
||||
.thenReturn(1);
|
||||
ResponseEntity<Map<String, Object>> resp = controller.deleteDepart("d-1");
|
||||
assertEquals(HttpStatus.OK, resp.getStatusCode());
|
||||
}
|
||||
|
||||
@Test
|
||||
@DisplayName("30. 删除部门(有子部门)→ 400,拒绝软删")
|
||||
void deleteDepart_withChildren_blocked() {
|
||||
mockAdmin();
|
||||
when(jdbc.queryForObject(contains("COUNT(*) FROM sys_depart"), eq(Integer.class), anyString()))
|
||||
.thenReturn(3);
|
||||
ResponseEntity<Map<String, Object>> resp = controller.deleteDepart("parent-d");
|
||||
assertEquals(HttpStatus.BAD_REQUEST, resp.getStatusCode());
|
||||
verify(jdbc, never()).update(contains("UPDATE sys_depart SET isactive=0"), any(), any(), anyString());
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------- 数据权限规则 CRUD(A5)
|
||||
|
||||
@Test
|
||||
@DisplayName("31. 新建数据权限规则 → status=0, id 非空")
|
||||
void createFilterRule_ok() {
|
||||
mockAdmin();
|
||||
when(jdbc.queryForList(contains("sys_filterrulerole"), eq(String.class), anyString()))
|
||||
.thenReturn(List.of());
|
||||
ResponseEntity<Map<String, Object>> resp = controller.createFilterRule(
|
||||
Map.of("entityid", "ent-1", "filterrule", "DEPTID = :dept"));
|
||||
assertEquals(HttpStatus.OK, resp.getStatusCode());
|
||||
assertEquals(0, resp.getBody().get("status"));
|
||||
assertNotNull(((Map<String, Object>) resp.getBody().get("data")).get("id"));
|
||||
}
|
||||
|
||||
@Test
|
||||
@DisplayName("32. 编辑规则内容 → status=0")
|
||||
void updateFilterRule_ok() {
|
||||
mockAdmin();
|
||||
when(jdbc.update(contains("UPDATE sys_filterrule SET filterrule"), any(), any(), any(), anyString()))
|
||||
.thenReturn(1);
|
||||
ResponseEntity<Map<String, Object>> resp =
|
||||
controller.updateFilterRule("f-1", Map.of("filterrule", "X = 2"));
|
||||
assertEquals(HttpStatus.OK, resp.getStatusCode());
|
||||
assertEquals(0, resp.getBody().get("status"));
|
||||
}
|
||||
|
||||
@Test
|
||||
@DisplayName("33. 编辑规则不存在 → 404")
|
||||
void updateFilterRule_notFound_404() {
|
||||
mockAdmin();
|
||||
when(jdbc.update(contains("UPDATE sys_filterrule SET filterrule"), any(), any(), any(), anyString()))
|
||||
.thenReturn(0);
|
||||
ResponseEntity<Map<String, Object>> resp =
|
||||
controller.updateFilterRule("no-such", Map.of("filterrule", "X = 2"));
|
||||
assertEquals(HttpStatus.NOT_FOUND, resp.getStatusCode());
|
||||
}
|
||||
|
||||
@Test
|
||||
@DisplayName("34. 删除规则 → status=0,清 sys_filterrulerole")
|
||||
void deleteFilterRule_ok() {
|
||||
mockAdmin();
|
||||
when(jdbc.update(contains("DELETE FROM sys_filterrulerole"), anyString())).thenReturn(0);
|
||||
when(jdbc.update(contains("UPDATE sys_filterrule SET isactive=0"), any(), any(), anyString()))
|
||||
.thenReturn(1);
|
||||
ResponseEntity<Map<String, Object>> resp = controller.deleteFilterRule("f-1");
|
||||
assertEquals(HttpStatus.OK, resp.getStatusCode());
|
||||
verify(jdbc, atLeastOnce()).update(contains("DELETE FROM sys_filterrulerole"), anyString());
|
||||
}
|
||||
|
||||
@Test
|
||||
@DisplayName("35. 规则角色差量:现有[A,B] 目标[B,C] → inserted=1 deleted=1 total=2")
|
||||
void setFilterRuleRoles_delta() {
|
||||
mockAdmin();
|
||||
when(jdbc.queryForList(contains("sys_filterrulerole"), eq(String.class), anyString()))
|
||||
.thenReturn(List.of("role-A", "role-B"));
|
||||
when(jdbc.update(anyString(), anyString(), anyString())).thenReturn(1);
|
||||
ResponseEntity<Map<String, Object>> resp = controller.setFilterRuleRoles(
|
||||
"f-1", Map.of("roleIds", List.of("role-B", "role-C")));
|
||||
assertEquals(HttpStatus.OK, resp.getStatusCode());
|
||||
Map<String, Object> data = (Map<String, Object>) resp.getBody().get("data");
|
||||
assertEquals(1, data.get("inserted"));
|
||||
assertEquals(1, data.get("deleted"));
|
||||
assertEquals(2, data.get("total"));
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------- 工具
|
||||
|
||||
private void mockAdmin() {
|
||||
|
||||
Reference in New Issue
Block a user