feat(用户授权): 非超管授权树只下发自己可授予的菜单,保存按操作者范围替换
- 授权树读接口新增 grantableOnly:非超管只返回自己已持有的菜单,可授予项的 祖先分组随行但置灰不可勾;否则勾父会连带授予树上未下发的范围外后代 - 保存的整树替换把删除范围收窄到操作者可授予集,范围外既有授权(超管直接 分配的)原样保留——隐藏项不在提交里,全类型删除会把它们静默清掉 - 前端提交剔除置灰分组的派生勾选;菜单权限配置页仍走全量目录
This commit is contained in:
+5
-3
@@ -55,9 +55,11 @@ public class PermissionMenuController {
|
||||
@GetMapping("/permission-menus")
|
||||
@Operation(summary = "查询菜单权限列表")
|
||||
public ApiResponse<List<PermissionMenuItemVo>> listMenus(HttpServletRequest request,
|
||||
@Parameter(description = "菜单类型: app/admin") @RequestParam(required = false) String menuType) {
|
||||
// 传入操作者:授权树据此把「非超管无权授予」的菜单标记为不可勾选
|
||||
return ApiResponse.success(permissionMenuService.list(requireAdmin(request), menuType));
|
||||
@Parameter(description = "菜单类型: app/admin") @RequestParam(required = false) String menuType,
|
||||
@Parameter(description = "授权树语义: true 时非超管只返回自己可授予的菜单")
|
||||
@RequestParam(required = false, defaultValue = "false") boolean grantableOnly) {
|
||||
// 传入操作者:授权树据此只下发(并把范围外节点标成不可勾选)操作者有权授予的菜单
|
||||
return ApiResponse.success(permissionMenuService.list(requireAdmin(request), menuType, grantableOnly));
|
||||
}
|
||||
|
||||
@PostMapping("/permission-menus")
|
||||
|
||||
+50
-15
@@ -57,19 +57,34 @@ public class PermissionMenuService {
|
||||
|
||||
/** Returns the flat menu catalog, including each item's direct parent ID. */
|
||||
public List<PermissionMenuItemVo> list(String menuType) {
|
||||
return list(null, menuType);
|
||||
return list(null, menuType, false);
|
||||
}
|
||||
|
||||
/** 菜单目录(菜单权限配置页):全量下发,只按操作者标记 grantable。 */
|
||||
public List<PermissionMenuItemVo> list(AdminUserEntity operator, String menuType) {
|
||||
return list(operator, menuType, false);
|
||||
}
|
||||
|
||||
/**
|
||||
* 授权树用:非超管只能授予自己已持有的菜单(与 {@link #ensureGrantable} 同一判据),
|
||||
* 这里把结论标进 {@code grantable},让前端把不可授予的节点置灰,避免「勾了才在保存时
|
||||
* 整单被 403 回滚」——创建用户与保存权限共用这一棵树,勾到一个越权项会连建号一起失败。
|
||||
* operator 为空或超管时不限制。
|
||||
* @param grantableOnly 授权树语义:非超管只返回自己已持有的菜单,范围外的项不再下发
|
||||
* (判据与 {@link #ensureGrantable} 同一套 {@link #resolveGrantableMenuIds})。
|
||||
* 为了让树上仍保留「一级分组 + 子页面」层级,可授予项的祖先分组一并返回,
|
||||
* 但标成 {@code grantable=false} 让前端置灰——分组自身不在操作者范围内,
|
||||
* 一旦可勾,「勾父覆盖全部后代」就会把树上未展示的范围外菜单一起授出去。
|
||||
* 保存侧必须配套按操作者范围替换,见
|
||||
* {@link #updateUserColumnPermissions(AdminUserEntity, Long, UserColumnPermissionUpdateRequest, String, boolean)}。
|
||||
*/
|
||||
public List<PermissionMenuItemVo> list(AdminUserEntity operator, String menuType) {
|
||||
public List<PermissionMenuItemVo> list(AdminUserEntity operator, String menuType, boolean grantableOnly) {
|
||||
List<PermissionMenuEntity> menus = loadMenus(menuType);
|
||||
List<PermissionMenuItemVo> items = toItemVos(menus, menus);
|
||||
Set<Long> grantableIds = resolveGrantableMenuIds(operator);
|
||||
List<PermissionMenuEntity> visibleMenus = menus;
|
||||
if (grantableOnly && grantableIds != null) {
|
||||
Set<Long> visibleIds = includeAncestorIds(grantableIds, menus);
|
||||
visibleMenus = menus.stream()
|
||||
.filter(menu -> menu.getId() != null && visibleIds.contains(menu.getId()))
|
||||
.toList();
|
||||
}
|
||||
List<PermissionMenuItemVo> items = toItemVos(visibleMenus, menus);
|
||||
for (PermissionMenuItemVo item : items) {
|
||||
item.setGrantable(grantableIds == null || grantableIds.contains(item.getId()));
|
||||
}
|
||||
@@ -497,7 +512,7 @@ public class PermissionMenuService {
|
||||
/**
|
||||
* Replaces direct grants. A non-super admin may only grant effective menus
|
||||
* that the operator already has, and only to themself or a directly-created
|
||||
* normal user.
|
||||
* normal user. Grants outside the operator's set are left untouched.
|
||||
*/
|
||||
@Transactional
|
||||
public void updateUserColumnPermissions(AdminUserEntity operator,
|
||||
@@ -506,7 +521,10 @@ public class PermissionMenuService {
|
||||
updateUserColumnPermissions(operator, userId, request, null);
|
||||
}
|
||||
|
||||
/** Replaces direct grants only within menuType; a blank type keeps legacy full-replacement behavior. */
|
||||
/**
|
||||
* Replaces direct grants only within menuType; a blank type replaces every type.
|
||||
* For a non-super admin both cases narrow to the operator's grantable set.
|
||||
*/
|
||||
@Transactional
|
||||
public void updateUserColumnPermissions(AdminUserEntity operator,
|
||||
Long userId,
|
||||
@@ -533,6 +551,7 @@ public class PermissionMenuService {
|
||||
String normalizedType = menuType == null || menuType.isBlank()
|
||||
? null
|
||||
: normalizeMenuTypeRequired(menuType);
|
||||
Set<Long> grantableIds = resolveGrantableMenuIds(operator);
|
||||
Set<Long> scopedMenuIds = new LinkedHashSet<>();
|
||||
if (normalizedType == null) {
|
||||
if (!requestedIds.isEmpty()) {
|
||||
@@ -570,7 +589,7 @@ public class PermissionMenuService {
|
||||
.filter(id -> !protectedIds.contains(id))
|
||||
.toList();
|
||||
}
|
||||
ensureGrantable(operator, grantIds, userId);
|
||||
ensureGrantable(operator, grantIds, userId, grantableIds);
|
||||
LinkedHashSet<Long> finalGrantIds = new LinkedHashSet<>(grantIds);
|
||||
|
||||
if (!protectedIds.isEmpty()) {
|
||||
@@ -582,10 +601,23 @@ public class PermissionMenuService {
|
||||
}
|
||||
}
|
||||
|
||||
if (normalizedType == null) {
|
||||
// 「整树替换」的替换范围要跟着授权树的可见范围收窄:非超管的授权树已不再下发
|
||||
// 自己范围外的菜单(见 list(operator, menuType, true)),这些既有授权(通常由超管
|
||||
// 直接分配)不会出现在提交里,按全类型删除等于把它们静默清掉——与 09-13
|
||||
// 「权限自己没掉」、09-16「整单 403」是同一类事故。范围外项保持原样,
|
||||
// 既不会被本次保存删除,也不会被新增(新增越权仍由 ensureGrantable 拒绝)。
|
||||
Set<Long> deleteScope = normalizedType == null ? null : scopedMenuIds;
|
||||
if (grantableIds != null) {
|
||||
deleteScope = deleteScope == null
|
||||
? new LinkedHashSet<>(grantableIds)
|
||||
: deleteScope.stream()
|
||||
.filter(grantableIds::contains)
|
||||
.collect(Collectors.toCollection(LinkedHashSet::new));
|
||||
}
|
||||
if (deleteScope == null) {
|
||||
userColumnPermissionMapper.deleteByUserId(userId);
|
||||
} else if (!scopedMenuIds.isEmpty()) {
|
||||
userColumnPermissionMapper.deleteByUserIdAndColumnIds(userId, new ArrayList<>(scopedMenuIds));
|
||||
} else if (!deleteScope.isEmpty()) {
|
||||
userColumnPermissionMapper.deleteByUserIdAndColumnIds(userId, new ArrayList<>(deleteScope));
|
||||
}
|
||||
for (Long columnId : finalGrantIds) {
|
||||
UserColumnPermissionEntity entity = new UserColumnPermissionEntity();
|
||||
@@ -867,13 +899,16 @@ public class PermissionMenuService {
|
||||
* 编辑该用户时整树提交会把它原样带回,若一并判为越权,整笔事务会回滚——连建号、改密
|
||||
* 都做不成(生产 2026-09-16 现象:勾到一个越权项,创建用户与保存权限双双失败)。
|
||||
* 只放行「保留已有」不放行「新增」,因此不构成提权。
|
||||
*
|
||||
* @param grantableIds 调用方算好的操作者可授予全集,null 表示不限制
|
||||
*/
|
||||
private void ensureGrantable(AdminUserEntity operator, List<Long> requestedIds, Long targetUserId) {
|
||||
private void ensureGrantable(AdminUserEntity operator, List<Long> requestedIds, Long targetUserId,
|
||||
Set<Long> grantableIds) {
|
||||
if (operator == null || isSuperAdmin(operator)) {
|
||||
return;
|
||||
}
|
||||
// 操作者自身 id 缺失(异常数据)时按「无任何可授予项」从严处理,不放行
|
||||
Set<Long> effective = Objects.requireNonNullElse(resolveGrantableMenuIds(operator), Set.of());
|
||||
Set<Long> effective = Objects.requireNonNullElse(grantableIds, Set.of());
|
||||
Set<Long> keptIds = targetUserId == null
|
||||
? Set.of()
|
||||
: new LinkedHashSet<>(loadDirectColumnIds(targetUserId));
|
||||
|
||||
Reference in New Issue
Block a user