F2-10: 用户组/角色-菜单写端点 + 三维权限分配页 + menu-all 叶子层修复
platform-metadata 新增 AdminWriteController(用户组 CRUD、成员/角色加减、 角色菜单差量整存)及单测 14 条;sys-admin-assign.json amis 三维分配页入库; 修复 AdminController.buildMenuChildren 叶子层 menuroot=null 导致叶子不显示的 bug;SQL upsert 脚本两库均已执行。 Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
This commit is contained in:
@@ -210,11 +210,14 @@ public class AdminController {
|
||||
for (Map<String, Object> m : allMenus) {
|
||||
String menuRoot = (String) m.get("menuroot");
|
||||
String mParentId = (String) m.get("parentid");
|
||||
// 根节点匹配 + parentid 匹配(null 比对用 isEmpty 兼容空白字符)
|
||||
boolean rootMatch = rootId != null && rootId.equals(menuRoot);
|
||||
// 根节点匹配:顶层目录(parentId==null)要求 menuroot 与 rootId 一致;
|
||||
// 叶子层(parentId 非空)仅靠 parentId 定位,menuroot 可能为空,跳过 rootMatch。
|
||||
boolean parentMatch = (parentId == null || parentId.isBlank())
|
||||
? (mParentId == null || mParentId.isBlank())
|
||||
: parentId.equals(mParentId);
|
||||
boolean rootMatch = (parentId == null || parentId.isBlank())
|
||||
? (rootId != null && rootId.equals(menuRoot))
|
||||
: true; // 叶子层不依赖 menuroot 字段
|
||||
if (rootMatch && parentMatch) {
|
||||
Map<String, Object> node = new LinkedHashMap<>();
|
||||
node.put("id", m.get("id"));
|
||||
@@ -292,6 +295,117 @@ public class AdminController {
|
||||
return okList(items, total);
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------- 用户组只读(F2-10 三维分配页所需)
|
||||
|
||||
/**
|
||||
* 用户组分页列表(管理视角,isactive=1)。
|
||||
*
|
||||
* @param page 页码(从 1 开始),默认 1
|
||||
* @param perPage 每页条数,默认 20,上限 200
|
||||
* @return amis crud 约定格式 {@code {status,data:{items,total}}}
|
||||
*/
|
||||
@Operation(summary = "用户组分页列表(管理视角)",
|
||||
description = "返回 isactive=1 的用户组;格式 {status,data:{items,total}}")
|
||||
@GetMapping("/api/admin/usergroups")
|
||||
public Map<String, Object> usergroups(
|
||||
@Parameter(description = "页码,从 1 开始") @RequestParam(defaultValue = "1") int page,
|
||||
@Parameter(description = "每页条数,最大 200") @RequestParam(defaultValue = "20") int perPage) {
|
||||
|
||||
int safePerPage = Math.min(Math.max(perPage, 1), 200);
|
||||
int offset = (Math.max(page, 1) - 1) * safePerPage;
|
||||
|
||||
List<Map<String, Object>> items = jdbcTemplate.queryForList(
|
||||
"SELECT TRIM(sys_usergroup_id) AS id, TRIM(name) AS name, description"
|
||||
+ " FROM sys_usergroup WHERE isactive=1 ORDER BY name LIMIT ? OFFSET ?",
|
||||
safePerPage, offset);
|
||||
int total = jdbcTemplate.queryForObject(
|
||||
"SELECT COUNT(*) FROM sys_usergroup WHERE isactive=1", Integer.class);
|
||||
return okList(items, total);
|
||||
}
|
||||
|
||||
/**
|
||||
* 单个用户组详情(用于编辑表单 initApi)。
|
||||
*
|
||||
* @param groupId SYS_USERGROUP.SYS_USERGROUP_ID
|
||||
*/
|
||||
@Operation(summary = "用户组详情(编辑 initApi)")
|
||||
@GetMapping("/api/admin/usergroups/{groupId}")
|
||||
public Map<String, Object> usergroupDetail(
|
||||
@Parameter(description = "SYS_USERGROUP.SYS_USERGROUP_ID") @PathVariable String groupId) {
|
||||
|
||||
List<Map<String, Object>> rows = jdbcTemplate.queryForList(
|
||||
"SELECT TRIM(sys_usergroup_id) AS id, TRIM(name) AS name, description"
|
||||
+ " FROM sys_usergroup WHERE TRIM(sys_usergroup_id)=? AND isactive=1",
|
||||
groupId.trim());
|
||||
Map<String, Object> resp = new LinkedHashMap<>();
|
||||
resp.put("status", 0);
|
||||
resp.put("msg", "");
|
||||
resp.put("data", rows.isEmpty() ? null : rows.get(0));
|
||||
return resp;
|
||||
}
|
||||
|
||||
/**
|
||||
* 某用户组下的成员列表(经 sys_usergroupuser → sys_user)。
|
||||
*
|
||||
* @param groupId SYS_USERGROUP.SYS_USERGROUP_ID
|
||||
*/
|
||||
@Operation(summary = "某用户组的成员列表(密码不下发)")
|
||||
@GetMapping("/api/admin/usergroups/{groupId}/users")
|
||||
public Map<String, Object> usergroupUsers(
|
||||
@Parameter(description = "SYS_USERGROUP.SYS_USERGROUP_ID") @PathVariable String groupId) {
|
||||
|
||||
List<Map<String, Object>> items = jdbcTemplate.queryForList(
|
||||
"SELECT DISTINCT TRIM(u.sys_user_id) AS id, TRIM(u.account) AS account,"
|
||||
+ " TRIM(u.name) AS name, u.state"
|
||||
+ " FROM sys_usergroupuser ugu"
|
||||
+ " JOIN sys_user u ON TRIM(u.sys_user_id) = TRIM(ugu.userid)"
|
||||
+ " WHERE TRIM(ugu.usergroupid)=? ORDER BY account",
|
||||
groupId.trim());
|
||||
return okList(items, items.size());
|
||||
}
|
||||
|
||||
/**
|
||||
* 某用户组已关联的角色列表(经 sys_usergrouprole → sys_role)。
|
||||
*
|
||||
* @param groupId SYS_USERGROUP.SYS_USERGROUP_ID
|
||||
*/
|
||||
@Operation(summary = "某用户组已关联的角色列表")
|
||||
@GetMapping("/api/admin/usergroups/{groupId}/roles")
|
||||
public Map<String, Object> usergroupRoles(
|
||||
@Parameter(description = "SYS_USERGROUP.SYS_USERGROUP_ID") @PathVariable String groupId) {
|
||||
|
||||
List<Map<String, Object>> items = jdbcTemplate.queryForList(
|
||||
"SELECT DISTINCT TRIM(r.sys_role_id) AS id, TRIM(r.name) AS name,"
|
||||
+ " TRIM(r.description) AS description, r.roletype AS \"roleType\""
|
||||
+ " FROM sys_usergrouprole ugr"
|
||||
+ " JOIN sys_role r ON TRIM(r.sys_role_id) = TRIM(ugr.roleid)"
|
||||
+ " WHERE TRIM(ugr.usergroupid)=? AND r.isactive=1 ORDER BY r.name",
|
||||
groupId.trim());
|
||||
return okList(items, items.size());
|
||||
}
|
||||
|
||||
/**
|
||||
* 某角色已勾选的菜单 ID 列表(供 input-tree 回显)。
|
||||
*
|
||||
* @param roleId SYS_ROLE.SYS_ROLE_ID
|
||||
*/
|
||||
@Operation(summary = "某角色已勾选的菜单 ID 列表(input-tree 回显)")
|
||||
@GetMapping("/api/admin/roles/{roleId}/menus")
|
||||
public Map<String, Object> roleMenus(
|
||||
@Parameter(description = "SYS_ROLE.SYS_ROLE_ID") @PathVariable String roleId) {
|
||||
|
||||
List<String> checkedMenuIds = jdbcTemplate.queryForList(
|
||||
"SELECT TRIM(menuid) FROM sys_role_menu WHERE TRIM(roleid)=?",
|
||||
String.class, roleId.trim());
|
||||
Map<String, Object> data = new LinkedHashMap<>();
|
||||
data.put("checkedMenuIds", checkedMenuIds);
|
||||
Map<String, Object> resp = new LinkedHashMap<>();
|
||||
resp.put("status", 0);
|
||||
resp.put("msg", "");
|
||||
resp.put("data", data);
|
||||
return resp;
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------- 内部工具
|
||||
|
||||
private Map<String, Object> okList(List<Map<String, Object>> items, int total) {
|
||||
|
||||
@@ -0,0 +1,358 @@
|
||||
package com.eip4.platform.metadata.web;
|
||||
|
||||
import java.util.LinkedHashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.UUID;
|
||||
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import org.springframework.http.HttpStatus;
|
||||
import org.springframework.http.ResponseEntity;
|
||||
import org.springframework.jdbc.core.JdbcTemplate;
|
||||
import org.springframework.transaction.annotation.Transactional;
|
||||
import org.springframework.web.bind.annotation.PathVariable;
|
||||
import org.springframework.web.bind.annotation.PostMapping;
|
||||
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.server.ResponseStatusException;
|
||||
|
||||
import com.dsasp.server.common.user.service.LoggedInUser;
|
||||
import com.dsasp.server.common.util.UserSession;
|
||||
|
||||
import io.swagger.v3.oas.annotations.Operation;
|
||||
import io.swagger.v3.oas.annotations.tags.Tag;
|
||||
|
||||
/**
|
||||
* F2-10 系统管理写端点(用户组/角色-菜单分配)。
|
||||
*
|
||||
* <p>用户写端点(新建/编辑/停用)因需要 PasswordHasher 而拆到 platform-auth 的
|
||||
* {@code AdminUserController},避免循环依赖。本类负责:
|
||||
* <ul>
|
||||
* <li>{@code POST /api/admin/usergroups/create} — 新建用户组</li>
|
||||
* <li>{@code POST /api/admin/usergroups/{id}/update} — 编辑用户组</li>
|
||||
* <li>{@code POST /api/admin/usergroups/{id}/delete} — 删除用户组(级联清关系表)</li>
|
||||
* <li>{@code POST /api/admin/usergroups/{id}/add-user} — 用户组加成员</li>
|
||||
* <li>{@code POST /api/admin/usergroups/{id}/remove-user} — 用户组减成员</li>
|
||||
* <li>{@code POST /api/admin/usergroups/{id}/add-role} — 用户组加角色</li>
|
||||
* <li>{@code POST /api/admin/usergroups/{id}/remove-role} — 用户组减角色</li>
|
||||
* <li>{@code POST /api/admin/roles/{id}/set-menus} — 角色菜单整存(差量更新,事务)</li>
|
||||
* </ul>
|
||||
*
|
||||
* <p>安全:仅系统管理员(userId='ADMIN' 或 account='admin')可调,否则 403。
|
||||
* 审计:SLF4J 记录操作人+目标+动作。SQL 全参数化。
|
||||
*/
|
||||
@Tag(name = "系统管理(写-用户组/菜单)", description = "F2-10 用户组管理 + 角色菜单分配写端点")
|
||||
@RestController
|
||||
@RequestMapping("/api/admin")
|
||||
public class AdminWriteController {
|
||||
|
||||
private static final Logger log = LoggerFactory.getLogger(AdminWriteController.class);
|
||||
|
||||
private final JdbcTemplate jdbcTemplate;
|
||||
|
||||
public AdminWriteController(JdbcTemplate jdbcTemplate) {
|
||||
this.jdbcTemplate = jdbcTemplate;
|
||||
}
|
||||
|
||||
// ================================================================ 用户组写操作
|
||||
|
||||
/**
|
||||
* 新建用户组。
|
||||
*
|
||||
* @param body 包含 name(必填)、description(选填)的 JSON 对象
|
||||
*/
|
||||
@Operation(summary = "新建用户组",
|
||||
description = "仅系统管理员可调;组名唯一(sys_usergroup_groupname_key 约束)")
|
||||
@PostMapping("/usergroups/create")
|
||||
public ResponseEntity<Map<String, Object>> createUsergroup(@RequestBody Map<String, Object> body) {
|
||||
requireAdmin();
|
||||
String name = requireString(body, "name", "用户组名称不能为空");
|
||||
String description = (String) body.get("description");
|
||||
String newId = UUID.randomUUID().toString();
|
||||
long now = System.currentTimeMillis();
|
||||
String operator = currentUserId();
|
||||
try {
|
||||
jdbcTemplate.update(
|
||||
"INSERT INTO sys_usergroup(sys_usergroup_id, name, description, isactive, version, createtime, createby, updatetime, updateby)"
|
||||
+ " VALUES(?,?,?,1,0,?,?,?,?)",
|
||||
newId, name.trim(),
|
||||
description != null ? description.trim() : null,
|
||||
now, operator, now, operator);
|
||||
} catch (Exception e) {
|
||||
if (e.getMessage() != null && e.getMessage().contains("groupname_key")) {
|
||||
return badRequest("用户组名称已存在:" + name);
|
||||
}
|
||||
throw e;
|
||||
}
|
||||
log.info("[F2-10][用户组新建] operator={} groupId={} name={}", operator, newId, name.trim());
|
||||
return ResponseEntity.ok(okWithId("用户组新建成功", newId));
|
||||
}
|
||||
|
||||
/**
|
||||
* 编辑用户组(名称/描述)。
|
||||
*
|
||||
* @param id SYS_USERGROUP.SYS_USERGROUP_ID
|
||||
* @param body 包含 name(必填)、description(选填)的 JSON 对象
|
||||
*/
|
||||
@Operation(summary = "编辑用户组名称/描述", description = "仅系统管理员可调")
|
||||
@PostMapping("/usergroups/{id}/update")
|
||||
public ResponseEntity<Map<String, Object>> updateUsergroup(
|
||||
@PathVariable String id, @RequestBody Map<String, Object> body) {
|
||||
requireAdmin();
|
||||
String name = requireString(body, "name", "用户组名称不能为空");
|
||||
String description = (String) body.get("description");
|
||||
long now = System.currentTimeMillis();
|
||||
String operator = currentUserId();
|
||||
int rows = jdbcTemplate.update(
|
||||
"UPDATE sys_usergroup SET name=?, description=?, updatetime=?, updateby=?, version=version+1"
|
||||
+ " WHERE TRIM(sys_usergroup_id)=? AND isactive=1",
|
||||
name.trim(), description != null ? description.trim() : null,
|
||||
now, operator, id.trim());
|
||||
if (rows == 0) {
|
||||
return ResponseEntity.status(HttpStatus.NOT_FOUND).body(fail("用户组不存在: " + id));
|
||||
}
|
||||
log.info("[F2-10][用户组编辑] operator={} groupId={} name={}", operator, id.trim(), name.trim());
|
||||
return ResponseEntity.ok(ok("用户组更新成功"));
|
||||
}
|
||||
|
||||
/**
|
||||
* 删除用户组(软删 isactive=0,级联清 usergroupuser/usergrouprole)。
|
||||
*
|
||||
* @param id SYS_USERGROUP.SYS_USERGROUP_ID
|
||||
*/
|
||||
@Operation(summary = "删除用户组(软删+级联清成员/角色关系)",
|
||||
description = "仅系统管理员可调;级联删除 sys_usergroupuser + sys_usergrouprole 中该组的行")
|
||||
@PostMapping("/usergroups/{id}/delete")
|
||||
@Transactional
|
||||
public ResponseEntity<Map<String, Object>> deleteUsergroup(@PathVariable String id) {
|
||||
requireAdmin();
|
||||
String gid = id.trim();
|
||||
long now = System.currentTimeMillis();
|
||||
String operator = currentUserId();
|
||||
jdbcTemplate.update("DELETE FROM sys_usergroupuser WHERE TRIM(usergroupid)=?", gid);
|
||||
jdbcTemplate.update("DELETE FROM sys_usergrouprole WHERE TRIM(usergroupid)=?", gid);
|
||||
// 物理删除:sys_usergroup 有 name UNIQUE 约束,软删后同名组无法复建
|
||||
int rows = jdbcTemplate.update(
|
||||
"DELETE FROM sys_usergroup WHERE TRIM(sys_usergroup_id)=?", gid);
|
||||
if (rows == 0) {
|
||||
return ResponseEntity.status(HttpStatus.NOT_FOUND).body(fail("用户组不存在: " + id));
|
||||
}
|
||||
log.info("[F2-10][用户组删除] operator={} groupId={}", operator, gid);
|
||||
return ResponseEntity.ok(ok("用户组删除成功"));
|
||||
}
|
||||
|
||||
/**
|
||||
* 用户组加成员(幂等:已存在则忽略)。
|
||||
*
|
||||
* @param id SYS_USERGROUP.SYS_USERGROUP_ID
|
||||
* @param body {@code {userId: "..."}}
|
||||
*/
|
||||
@Operation(summary = "用户组加成员(幂等)", description = "仅系统管理员可调;已在组内则忽略")
|
||||
@PostMapping("/usergroups/{id}/add-user")
|
||||
public ResponseEntity<Map<String, Object>> addUserToGroup(
|
||||
@PathVariable String id, @RequestBody Map<String, Object> body) {
|
||||
requireAdmin();
|
||||
String userId = requireString(body, "userId", "userId 不能为空");
|
||||
String gid = id.trim();
|
||||
String uid = userId.trim();
|
||||
jdbcTemplate.update(
|
||||
"INSERT INTO sys_usergroupuser(usergroupid, userid) VALUES(?,?) ON CONFLICT DO NOTHING",
|
||||
gid, uid);
|
||||
log.info("[F2-10][用户组加成员] operator={} groupId={} userId={}", currentUserId(), gid, uid);
|
||||
return ResponseEntity.ok(ok("成员添加成功"));
|
||||
}
|
||||
|
||||
/**
|
||||
* 用户组减成员。
|
||||
*
|
||||
* @param id SYS_USERGROUP.SYS_USERGROUP_ID
|
||||
* @param body {@code {userId: "..."}}
|
||||
*/
|
||||
@Operation(summary = "用户组减成员", description = "仅系统管理员可调")
|
||||
@PostMapping("/usergroups/{id}/remove-user")
|
||||
public ResponseEntity<Map<String, Object>> removeUserFromGroup(
|
||||
@PathVariable String id, @RequestBody Map<String, Object> body) {
|
||||
requireAdmin();
|
||||
String userId = requireString(body, "userId", "userId 不能为空");
|
||||
jdbcTemplate.update(
|
||||
"DELETE FROM sys_usergroupuser WHERE TRIM(usergroupid)=? AND TRIM(userid)=?",
|
||||
id.trim(), userId.trim());
|
||||
log.info("[F2-10][用户组减成员] operator={} groupId={} userId={}", currentUserId(), id.trim(), userId.trim());
|
||||
return ResponseEntity.ok(ok("成员移除成功"));
|
||||
}
|
||||
|
||||
/**
|
||||
* 用户组加角色(幂等:已存在则忽略)。
|
||||
*
|
||||
* @param id SYS_USERGROUP.SYS_USERGROUP_ID
|
||||
* @param body {@code {roleId: "..."}}
|
||||
*/
|
||||
@Operation(summary = "用户组加角色(幂等)", description = "仅系统管理员可调;已关联则忽略")
|
||||
@PostMapping("/usergroups/{id}/add-role")
|
||||
public ResponseEntity<Map<String, Object>> addRoleToGroup(
|
||||
@PathVariable String id, @RequestBody Map<String, Object> body) {
|
||||
requireAdmin();
|
||||
String roleId = requireString(body, "roleId", "roleId 不能为空");
|
||||
jdbcTemplate.update(
|
||||
"INSERT INTO sys_usergrouprole(usergroupid, roleid) VALUES(?,?) ON CONFLICT DO NOTHING",
|
||||
id.trim(), roleId.trim());
|
||||
log.info("[F2-10][用户组加角色] operator={} groupId={} roleId={}", currentUserId(), id.trim(), roleId.trim());
|
||||
return ResponseEntity.ok(ok("角色关联成功"));
|
||||
}
|
||||
|
||||
/**
|
||||
* 用户组减角色。
|
||||
*
|
||||
* @param id SYS_USERGROUP.SYS_USERGROUP_ID
|
||||
* @param body {@code {roleId: "..."}}
|
||||
*/
|
||||
@Operation(summary = "用户组减角色", description = "仅系统管理员可调")
|
||||
@PostMapping("/usergroups/{id}/remove-role")
|
||||
public ResponseEntity<Map<String, Object>> removeRoleFromGroup(
|
||||
@PathVariable String id, @RequestBody Map<String, Object> body) {
|
||||
requireAdmin();
|
||||
String roleId = requireString(body, "roleId", "roleId 不能为空");
|
||||
jdbcTemplate.update(
|
||||
"DELETE FROM sys_usergrouprole WHERE TRIM(usergroupid)=? AND TRIM(roleid)=?",
|
||||
id.trim(), roleId.trim());
|
||||
log.info("[F2-10][用户组减角色] operator={} groupId={} roleId={}", currentUserId(), id.trim(), roleId.trim());
|
||||
return ResponseEntity.ok(ok("角色关联移除成功"));
|
||||
}
|
||||
|
||||
// ================================================================ 角色-菜单整存
|
||||
|
||||
/**
|
||||
* 角色菜单整存(差量更新,事务)。
|
||||
*
|
||||
* <p>对比 {@code sys_role_menu} 中该角色现有的菜单集合与传入的目标集合做差量:
|
||||
* 目标有现有无 → INSERT;现有有目标无 → DELETE;交集不变。
|
||||
*
|
||||
* @param id SYS_ROLE.SYS_ROLE_ID
|
||||
* @param body {@code {menuIds: ["id1","id2",...]}}(空数组表示清空)
|
||||
*/
|
||||
@Operation(summary = "角色菜单整存(差量更新,事务)",
|
||||
description = "仅系统管理员可调;差量 INSERT/DELETE;空 menuIds 表示清空")
|
||||
@PostMapping("/roles/{id}/set-menus")
|
||||
@Transactional
|
||||
public ResponseEntity<Map<String, Object>> setRoleMenus(
|
||||
@PathVariable String id, @RequestBody Map<String, Object> body) {
|
||||
requireAdmin();
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
List<String> menuIds = (List<String>) body.get("menuIds");
|
||||
if (menuIds == null) {
|
||||
return badRequest("menuIds 字段不能为空(传空数组表示清空)");
|
||||
}
|
||||
|
||||
String rid = id.trim();
|
||||
String operator = currentUserId();
|
||||
|
||||
List<String> current = jdbcTemplate.queryForList(
|
||||
"SELECT TRIM(menuid) FROM sys_role_menu WHERE TRIM(roleid)=?", String.class, rid);
|
||||
|
||||
List<String> target = menuIds.stream()
|
||||
.filter(m -> m != null && !m.isBlank())
|
||||
.map(String::trim)
|
||||
.distinct()
|
||||
.toList();
|
||||
|
||||
int inserted = 0;
|
||||
for (String mid : target) {
|
||||
if (!current.contains(mid)) {
|
||||
jdbcTemplate.update(
|
||||
"INSERT INTO sys_role_menu(roleid, menuid) VALUES(?,?) ON CONFLICT DO NOTHING",
|
||||
rid, mid);
|
||||
inserted++;
|
||||
}
|
||||
}
|
||||
int deleted = 0;
|
||||
for (String mid : current) {
|
||||
if (!target.contains(mid)) {
|
||||
jdbcTemplate.update(
|
||||
"DELETE FROM sys_role_menu WHERE TRIM(roleid)=? AND TRIM(menuid)=?",
|
||||
rid, mid);
|
||||
deleted++;
|
||||
}
|
||||
}
|
||||
|
||||
log.info("[F2-10][角色菜单整存] operator={} roleId={} inserted={} deleted={} total={}",
|
||||
operator, rid, inserted, deleted, target.size());
|
||||
|
||||
Map<String, Object> data = new LinkedHashMap<>();
|
||||
data.put("inserted", inserted);
|
||||
data.put("deleted", deleted);
|
||||
data.put("total", target.size());
|
||||
return ResponseEntity.ok(ok("角色菜单保存成功", data));
|
||||
}
|
||||
|
||||
// ================================================================ 内部工具
|
||||
|
||||
/**
|
||||
* 管理员身份检查:userId='ADMIN' 或其对应 account='admin',否则 403。
|
||||
*/
|
||||
private void requireAdmin() {
|
||||
java.util.Properties ctx = UserSession.get();
|
||||
if (ctx == null) {
|
||||
throw new ResponseStatusException(HttpStatus.FORBIDDEN, "未登录,禁止访问系统管理接口");
|
||||
}
|
||||
String cu = ctx.getProperty(LoggedInUser.CURRENTUSER, "").trim();
|
||||
if (cu.isEmpty()) {
|
||||
throw new ResponseStatusException(HttpStatus.FORBIDDEN, "未登录,禁止访问系统管理接口");
|
||||
}
|
||||
if ("ADMIN".equalsIgnoreCase(cu)) {
|
||||
return;
|
||||
}
|
||||
Integer cnt = jdbcTemplate.queryForObject(
|
||||
"SELECT COUNT(*) FROM sys_user WHERE TRIM(sys_user_id)=? AND TRIM(account)='admin'",
|
||||
Integer.class, cu);
|
||||
if (cnt == null || cnt == 0) {
|
||||
throw new ResponseStatusException(HttpStatus.FORBIDDEN, "仅系统管理员可执行此操作");
|
||||
}
|
||||
}
|
||||
|
||||
private static String currentUserId() {
|
||||
java.util.Properties ctx = UserSession.get();
|
||||
if (ctx == null) return "UNKNOWN";
|
||||
String u = ctx.getProperty(LoggedInUser.CURRENTUSER, "UNKNOWN");
|
||||
return u.isBlank() ? "UNKNOWN" : u.trim();
|
||||
}
|
||||
|
||||
private String requireString(Map<String, Object> body, String key, String errMsg) {
|
||||
Object val = body.get(key);
|
||||
if (val == null || val.toString().isBlank()) {
|
||||
throw new ResponseStatusException(HttpStatus.BAD_REQUEST, errMsg);
|
||||
}
|
||||
return val.toString();
|
||||
}
|
||||
|
||||
private static ResponseEntity<Map<String, Object>> badRequest(String msg) {
|
||||
return ResponseEntity.status(HttpStatus.BAD_REQUEST).body(fail(msg));
|
||||
}
|
||||
|
||||
private static Map<String, Object> fail(String msg) {
|
||||
Map<String, Object> r = new LinkedHashMap<>();
|
||||
r.put("status", 1);
|
||||
r.put("msg", msg);
|
||||
return r;
|
||||
}
|
||||
|
||||
private static Map<String, Object> ok(String msg) {
|
||||
return ok(msg, null);
|
||||
}
|
||||
|
||||
private static Map<String, Object> ok(String msg, Map<String, Object> data) {
|
||||
Map<String, Object> r = new LinkedHashMap<>();
|
||||
r.put("status", 0);
|
||||
r.put("msg", msg);
|
||||
if (data != null) r.put("data", data);
|
||||
return r;
|
||||
}
|
||||
|
||||
private static Map<String, Object> okWithId(String msg, String id) {
|
||||
Map<String, Object> data = new LinkedHashMap<>();
|
||||
data.put("id", id);
|
||||
return ok(msg, data);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,470 @@
|
||||
{
|
||||
"type": "page",
|
||||
"title": "权限分配",
|
||||
"body": [
|
||||
{
|
||||
"type": "grid",
|
||||
"columns": [
|
||||
{
|
||||
"md": 4,
|
||||
"body": [
|
||||
{
|
||||
"type": "panel",
|
||||
"title": "用户组",
|
||||
"headerClassName": "bg-primary text-white",
|
||||
"bodyClassName": "p-0",
|
||||
"body": [
|
||||
{
|
||||
"type": "toolbar",
|
||||
"className": "p-xs",
|
||||
"body": [
|
||||
{
|
||||
"type": "button",
|
||||
"label": "新建用户组",
|
||||
"level": "primary",
|
||||
"size": "sm",
|
||||
"actionType": "dialog",
|
||||
"dialog": {
|
||||
"title": "新建用户组",
|
||||
"body": {
|
||||
"type": "form",
|
||||
"api": {
|
||||
"method": "post",
|
||||
"url": "/api/admin/usergroups/create"
|
||||
},
|
||||
"body": [
|
||||
{
|
||||
"type": "input-text",
|
||||
"name": "name",
|
||||
"label": "名称",
|
||||
"required": true
|
||||
},
|
||||
{
|
||||
"type": "input-text",
|
||||
"name": "description",
|
||||
"label": "描述"
|
||||
}
|
||||
]
|
||||
},
|
||||
"actions": [
|
||||
{
|
||||
"type": "button",
|
||||
"label": "取消",
|
||||
"actionType": "close"
|
||||
},
|
||||
{
|
||||
"type": "submit",
|
||||
"label": "保存",
|
||||
"level": "primary",
|
||||
"reload": "groupList"
|
||||
}
|
||||
]
|
||||
}
|
||||
}
|
||||
]
|
||||
},
|
||||
{
|
||||
"type": "crud",
|
||||
"name": "groupList",
|
||||
"syncLocation": false,
|
||||
"api": {
|
||||
"method": "get",
|
||||
"url": "/api/admin/usergroups",
|
||||
"adaptor": "var d=payload.data||{};return {status:0,data:{items:d.items||[],total:d.total||0}};"
|
||||
},
|
||||
"perPage": 20,
|
||||
"headerToolbar": [],
|
||||
"footerToolbar": ["pagination"],
|
||||
"selectable": true,
|
||||
"multiple": false,
|
||||
"keepItemSelectionOnPageChange": false,
|
||||
"onSelect": {
|
||||
"actionType": "setValue",
|
||||
"componentId": "selectedGroup",
|
||||
"args": {
|
||||
"value": "${event.data.selectedItems[0]}"
|
||||
}
|
||||
},
|
||||
"columns": [
|
||||
{
|
||||
"name": "name",
|
||||
"label": "用户组名称"
|
||||
},
|
||||
{
|
||||
"type": "operation",
|
||||
"label": "操作",
|
||||
"width": 100,
|
||||
"buttons": [
|
||||
{
|
||||
"type": "button",
|
||||
"label": "编辑",
|
||||
"level": "link",
|
||||
"actionType": "dialog",
|
||||
"dialog": {
|
||||
"title": "编辑用户组",
|
||||
"body": {
|
||||
"type": "form",
|
||||
"api": {
|
||||
"method": "post",
|
||||
"url": "/api/admin/usergroups/${id}/update"
|
||||
},
|
||||
"initApi": {
|
||||
"method": "get",
|
||||
"url": "/api/admin/usergroups/${id}"
|
||||
},
|
||||
"body": [
|
||||
{
|
||||
"type": "input-text",
|
||||
"name": "name",
|
||||
"label": "名称",
|
||||
"required": true
|
||||
},
|
||||
{
|
||||
"type": "input-text",
|
||||
"name": "description",
|
||||
"label": "描述"
|
||||
}
|
||||
]
|
||||
},
|
||||
"actions": [
|
||||
{
|
||||
"type": "button",
|
||||
"label": "取消",
|
||||
"actionType": "close"
|
||||
},
|
||||
{
|
||||
"type": "submit",
|
||||
"label": "保存",
|
||||
"level": "primary",
|
||||
"reload": "groupList"
|
||||
}
|
||||
]
|
||||
}
|
||||
},
|
||||
{
|
||||
"type": "button",
|
||||
"label": "删除",
|
||||
"level": "link",
|
||||
"className": "text-danger",
|
||||
"actionType": "ajax",
|
||||
"confirmText": "确认删除用户组「${name}」?",
|
||||
"api": {
|
||||
"method": "post",
|
||||
"url": "/api/admin/usergroups/${id}/delete"
|
||||
},
|
||||
"reload": "groupList"
|
||||
}
|
||||
]
|
||||
}
|
||||
]
|
||||
}
|
||||
]
|
||||
}
|
||||
]
|
||||
},
|
||||
{
|
||||
"md": 4,
|
||||
"body": [
|
||||
{
|
||||
"type": "panel",
|
||||
"title": "该组的角色",
|
||||
"headerClassName": "bg-info text-white",
|
||||
"bodyClassName": "p-0",
|
||||
"body": [
|
||||
{
|
||||
"type": "service",
|
||||
"name": "roleAssignPanel",
|
||||
"api": {
|
||||
"method": "get",
|
||||
"url": "/api/admin/usergroups/${selectedGroup.id}/roles",
|
||||
"sendOn": "${selectedGroup.id}",
|
||||
"adaptor": "var d=payload.data||{};return {status:0,data:{items:d.items||[],total:d.total||0}};"
|
||||
},
|
||||
"body": [
|
||||
{
|
||||
"type": "tpl",
|
||||
"tpl": "<div class='text-muted p-sm' style='font-size:0.85em'>请先在左侧选择用户组</div>",
|
||||
"visibleOn": "!${selectedGroup.id}"
|
||||
},
|
||||
{
|
||||
"type": "crud",
|
||||
"name": "roleList",
|
||||
"source": "${items}",
|
||||
"visibleOn": "${selectedGroup.id}",
|
||||
"headerToolbar": [
|
||||
{
|
||||
"type": "button",
|
||||
"label": "添加角色",
|
||||
"level": "success",
|
||||
"size": "sm",
|
||||
"actionType": "dialog",
|
||||
"disabledOn": "!${selectedGroup.id}",
|
||||
"dialog": {
|
||||
"title": "添加角色到「${selectedGroup.name}」",
|
||||
"body": {
|
||||
"type": "form",
|
||||
"api": {
|
||||
"method": "post",
|
||||
"url": "/api/admin/usergroups/${selectedGroup.id}/add-role"
|
||||
},
|
||||
"body": [
|
||||
{
|
||||
"type": "select",
|
||||
"name": "roleId",
|
||||
"label": "选择角色",
|
||||
"required": true,
|
||||
"source": {
|
||||
"method": "get",
|
||||
"url": "/api/org/roles",
|
||||
"adaptor": "var d=payload.data||{};return {status:0,data:{options:(d.items||[]).map(function(r){return {label:r.name,value:r.id};})}};"
|
||||
},
|
||||
"searchable": true
|
||||
}
|
||||
]
|
||||
},
|
||||
"actions": [
|
||||
{
|
||||
"type": "button",
|
||||
"label": "取消",
|
||||
"actionType": "close"
|
||||
},
|
||||
{
|
||||
"type": "submit",
|
||||
"label": "确认",
|
||||
"level": "primary",
|
||||
"reload": "roleAssignPanel"
|
||||
}
|
||||
]
|
||||
}
|
||||
}
|
||||
],
|
||||
"footerToolbar": [],
|
||||
"columns": [
|
||||
{
|
||||
"name": "name",
|
||||
"label": "角色名称"
|
||||
},
|
||||
{
|
||||
"type": "operation",
|
||||
"label": "操作",
|
||||
"width": 80,
|
||||
"buttons": [
|
||||
{
|
||||
"type": "button",
|
||||
"label": "移除",
|
||||
"level": "link",
|
||||
"className": "text-danger",
|
||||
"actionType": "ajax",
|
||||
"confirmText": "从「${selectedGroup.name}」移除角色「${name}」?",
|
||||
"api": {
|
||||
"method": "post",
|
||||
"url": "/api/admin/usergroups/${selectedGroup.id}/remove-role",
|
||||
"data": {
|
||||
"roleId": "${id}"
|
||||
}
|
||||
},
|
||||
"reload": "roleAssignPanel"
|
||||
}
|
||||
]
|
||||
},
|
||||
{
|
||||
"type": "button",
|
||||
"label": "查看菜单",
|
||||
"level": "link",
|
||||
"actionType": "setValue",
|
||||
"componentId": "selectedRole",
|
||||
"args": {
|
||||
"value": "${&}"
|
||||
}
|
||||
}
|
||||
]
|
||||
}
|
||||
]
|
||||
}
|
||||
]
|
||||
},
|
||||
{
|
||||
"type": "panel",
|
||||
"title": "该组的成员",
|
||||
"className": "mt-sm",
|
||||
"headerClassName": "bg-light",
|
||||
"bodyClassName": "p-0",
|
||||
"body": [
|
||||
{
|
||||
"type": "service",
|
||||
"name": "userAssignPanel",
|
||||
"api": {
|
||||
"method": "get",
|
||||
"url": "/api/admin/usergroups/${selectedGroup.id}/users",
|
||||
"sendOn": "${selectedGroup.id}",
|
||||
"adaptor": "var d=payload.data||{};return {status:0,data:{items:d.items||[],total:d.total||0}};"
|
||||
},
|
||||
"body": [
|
||||
{
|
||||
"type": "crud",
|
||||
"name": "memberList",
|
||||
"source": "${items}",
|
||||
"visibleOn": "${selectedGroup.id}",
|
||||
"headerToolbar": [
|
||||
{
|
||||
"type": "button",
|
||||
"label": "添加成员",
|
||||
"level": "default",
|
||||
"size": "sm",
|
||||
"actionType": "dialog",
|
||||
"disabledOn": "!${selectedGroup.id}",
|
||||
"dialog": {
|
||||
"title": "添加成员到「${selectedGroup.name}」",
|
||||
"body": {
|
||||
"type": "form",
|
||||
"api": {
|
||||
"method": "post",
|
||||
"url": "/api/admin/usergroups/${selectedGroup.id}/add-user"
|
||||
},
|
||||
"body": [
|
||||
{
|
||||
"type": "select",
|
||||
"name": "userId",
|
||||
"label": "选择用户",
|
||||
"required": true,
|
||||
"source": {
|
||||
"method": "get",
|
||||
"url": "/api/admin/users",
|
||||
"adaptor": "var d=payload.data||{};return {status:0,data:{options:(d.items||[]).map(function(u){return {label:u.account+'('+u.name+')',value:u.id};})}};"
|
||||
},
|
||||
"searchable": true
|
||||
}
|
||||
]
|
||||
},
|
||||
"actions": [
|
||||
{
|
||||
"type": "button",
|
||||
"label": "取消",
|
||||
"actionType": "close"
|
||||
},
|
||||
{
|
||||
"type": "submit",
|
||||
"label": "确认",
|
||||
"level": "primary",
|
||||
"reload": "userAssignPanel"
|
||||
}
|
||||
]
|
||||
}
|
||||
}
|
||||
],
|
||||
"footerToolbar": [],
|
||||
"columns": [
|
||||
{
|
||||
"name": "account",
|
||||
"label": "账号"
|
||||
},
|
||||
{
|
||||
"name": "name",
|
||||
"label": "姓名"
|
||||
},
|
||||
{
|
||||
"type": "operation",
|
||||
"label": "操作",
|
||||
"width": 80,
|
||||
"buttons": [
|
||||
{
|
||||
"type": "button",
|
||||
"label": "移除",
|
||||
"level": "link",
|
||||
"className": "text-danger",
|
||||
"actionType": "ajax",
|
||||
"confirmText": "从「${selectedGroup.name}」移除成员「${account}」?",
|
||||
"api": {
|
||||
"method": "post",
|
||||
"url": "/api/admin/usergroups/${selectedGroup.id}/remove-user",
|
||||
"data": {
|
||||
"userId": "${id}"
|
||||
}
|
||||
},
|
||||
"reload": "userAssignPanel"
|
||||
}
|
||||
]
|
||||
}
|
||||
]
|
||||
}
|
||||
]
|
||||
}
|
||||
]
|
||||
}
|
||||
]
|
||||
},
|
||||
{
|
||||
"md": 4,
|
||||
"body": [
|
||||
{
|
||||
"type": "panel",
|
||||
"title": "角色菜单权限",
|
||||
"headerClassName": "bg-warning",
|
||||
"bodyClassName": "p-sm",
|
||||
"body": [
|
||||
{
|
||||
"type": "tpl",
|
||||
"tpl": "<div class='text-muted mb-sm' style='font-size:0.85em'>请先在中栏点击角色的「查看菜单」</div>",
|
||||
"visibleOn": "!${selectedRole.id}"
|
||||
},
|
||||
{
|
||||
"type": "service",
|
||||
"name": "menuAssignPanel",
|
||||
"api": {
|
||||
"method": "get",
|
||||
"url": "/api/admin/roles/${selectedRole.id}/menus",
|
||||
"sendOn": "${selectedRole.id}",
|
||||
"adaptor": "var d=payload.data||{};return {status:0,data:{checkedMenuIds:d.checkedMenuIds||[]}};"
|
||||
},
|
||||
"visibleOn": "${selectedRole.id}",
|
||||
"body": [
|
||||
{
|
||||
"type": "tpl",
|
||||
"tpl": "<div class='mb-xs font-weight-bold'>角色:${selectedRole.name}</div>"
|
||||
},
|
||||
{
|
||||
"type": "form",
|
||||
"name": "menuForm",
|
||||
"api": {
|
||||
"method": "post",
|
||||
"url": "/api/admin/roles/${selectedRole.id}/set-menus"
|
||||
},
|
||||
"body": [
|
||||
{
|
||||
"type": "input-tree",
|
||||
"name": "menuIds",
|
||||
"label": "菜单权限",
|
||||
"multiple": true,
|
||||
"cascade": false,
|
||||
"withChildren": false,
|
||||
"joinValues": false,
|
||||
"extractValue": true,
|
||||
"value": "${checkedMenuIds}",
|
||||
"source": {
|
||||
"method": "get",
|
||||
"url": "/api/admin/menu-all",
|
||||
"adaptor": "var roots=payload.roots||[];var nodes=[];roots.forEach(function(r){r.children&&r.children.forEach(function(dir){var dirNode={value:dir.id,label:dir.name,children:[]};(dir.children||[]).forEach(function(leaf){dirNode.children.push({value:leaf.id,label:leaf.name});});nodes.push(dirNode);});});return {status:0,data:{options:nodes}};"
|
||||
},
|
||||
"showIcon": false,
|
||||
"className": "max-h-400 overflow-y-auto"
|
||||
}
|
||||
],
|
||||
"actions": [
|
||||
{
|
||||
"type": "submit",
|
||||
"label": "保存菜单权限",
|
||||
"level": "primary",
|
||||
"reload": "menuAssignPanel"
|
||||
}
|
||||
]
|
||||
}
|
||||
]
|
||||
}
|
||||
]
|
||||
}
|
||||
]
|
||||
}
|
||||
]
|
||||
}
|
||||
]
|
||||
}
|
||||
Reference in New Issue
Block a user