应用授权修订
ci-userbase-service / build-and-push (push) Successful in 1m13s

This commit is contained in:
chenxing
2026-08-11 11:52:10 +08:00
parent a0aa1cfb35
commit ed07f380da
3 changed files with 49 additions and 42 deletions
@@ -97,6 +97,29 @@ public class GroupAuthzService implements IGroupAuthzService {
@Autowired
private SysUserAppRoleRelRepository sysUserAppRoleRelRepository;
// ===================== 新增:沿祖先链收集「本组+所有祖先组开通的应用ID并集」(应用池联动GetSchoolAppId) =====================
/**
* 按用户需求:父组(学校组)若开通了应用(GetSchoolAppId返回非空),则其所有子孙组的模块权限分配面板也能看到这些应用(无需子组重复开通)。<br>
* 取应用范围=sys_group_app_rel里group_id属于「本组+全部祖先组」的app_id并集;开通不继承,模块权限池继承。<br>
* 对 hasModulePermission / UserPermissionTree 不做修改,避免越权(最终用户拿到权限仍需要自己的归属组开通应用+继承到模块权限,双保险)。
*/
private Set<Integer> collectAppIdsForAuthzScope(Integer groupId) {
Map<Integer, SysGroup> allGroupMap = buildAllGroupMap();
List<Integer> chain = ancestorChain(groupId, allGroupMap);
if (chain == null || chain.isEmpty()) {
return Collections.emptySet();
}
List<SysGroupAppRel> rels = sysGroupAppRelRepository.findByGroupIdIn(chain);
Set<Integer> appIds = new LinkedHashSet<>();
for (SysGroupAppRel rel : rels) {
if (rel.getAppId() != null) {
appIds.add(rel.getAppId());
}
}
return appIds;
}
// ===================== 功能1:应用开通 =====================
@Override
@@ -105,7 +128,6 @@ public class GroupAuthzService implements IGroupAuthzService {
if (!sysGroupRepository.existsById(groupId)) {
throw new DException("系统组不存在");
}
// 系统全量应用 + 该组已开通集合,标记 checked 回显
List<ApplicationInfo> apps = applicationInfoRepository.findAll();
apps.sort(appOrder());
Set<Integer> openedAppIds = new HashSet<>();
@@ -177,29 +199,22 @@ public class GroupAuthzService implements IGroupAuthzService {
if (!sysGroupRepository.existsById(groupId)) {
throw new DException("系统组不存在");
}
// 1. 该组已开通的应用;无记录则右侧不显示任何应用
List<SysGroupAppRel> rels = sysGroupAppRelRepository.findByGroupId(groupId);
if (rels.isEmpty()) {
Set<Integer> appIdSet = collectAppIdsForAuthzScope(groupId);
if (appIdSet.isEmpty()) {
return Collections.emptyList();
}
List<Integer> appIds = new ArrayList<>();
for (SysGroupAppRel rel : rels) {
appIds.add(rel.getAppId());
}
List<Integer> appIds = new ArrayList<>(appIdSet);
List<ApplicationInfo> apps = applicationInfoRepository.findAllById(appIds);
apps.sort(appOrder());
// 2. 已开通应用下的全部模块,按 appId 分桶
List<AppModule> allModules = appModuleRepository.findByAppIdIn(appIds);
Map<Integer, List<AppModule>> appModuleMap = new HashMap<>();
for (AppModule m : allModules) {
appModuleMap.computeIfAbsent(m.getAppId(), k -> new ArrayList<>()).add(m);
}
// 3. 权限回显:该组已勾选的模块权限记录
Map<Integer, SysGroupModulePerm> permMap = new HashMap<>();
for (SysGroupModulePerm perm : sysGroupModulePermRepository.findByGroupId(groupId)) {
permMap.put(perm.getModuleId(), perm);
}
// 4. 逐应用组装模块树
List<GroupAuthzAppVo> result = new ArrayList<>();
for (ApplicationInfo app : apps) {
GroupAuthzAppVo appVo = new GroupAuthzAppVo();
@@ -290,15 +305,10 @@ public class GroupAuthzService implements IGroupAuthzService {
}
List<GroupModulePermRequest.ModulePermItem> perms =
request.getPerms() == null ? Collections.emptyList() : request.getPerms();
// 勾选的模块必须属于该组已开通应用(边界情况:未开通应用的模块权限无效,不允许写入)
Set<Integer> appIdSet = collectAppIdsForAuthzScope(groupId);
Set<Integer> validModuleIds = new HashSet<>();
List<SysGroupAppRel> rels = sysGroupAppRelRepository.findByGroupId(groupId);
if (!rels.isEmpty()) {
List<Integer> appIds = new ArrayList<>();
for (SysGroupAppRel rel : rels) {
appIds.add(rel.getAppId());
}
for (AppModule m : appModuleRepository.findByAppIdIn(appIds)) {
if (!appIdSet.isEmpty()) {
for (AppModule m : appModuleRepository.findByAppIdIn(new ArrayList<>(appIdSet))) {
validModuleIds.add(m.getId());
}
}
@@ -418,31 +428,22 @@ public class GroupAuthzService implements IGroupAuthzService {
Integer moduleId = item.getModuleId();
int mode = item.getMode();
boolean isAll = item.getIsAll() != null && item.getIsAll() == 1;
// 基础校验
if (!sysGroupRepository.existsById(groupId)) {
throw new DException("系统组不存在: " + groupId);
}
if (!applicationInfoRepository.existsById(appId)) {
throw new DException("应用不存在: " + appId);
}
// 操作模式:仅 0=追加 / 2=删除 合法(同 SetSchoolApp,经 IEnum.getByKey 解析)
SysModeEnum modeEnum = IEnum.getByKey(SysModeEnum.class, mode);
if (modeEnum == null || (modeEnum != SysModeEnum.APPEND && modeEnum != SysModeEnum.DELETE)) {
throw new DException("不支持的操作模式: " + mode + ",模块授权仅支持 0(追加)/2(删除)");
}
// 解析本次要操作的模块集合:moduleId=0 → 该应用下全部模块(展开,不写 module_id=0 伪记录)
List<Integer> targetModuleIds = resolveModuleIds(appId, moduleId);
// isAll 语义(重点):映射 sys_group_module_perm.inherit_flag。
// isAll=1 → inheritFlag=true:子孙组在 hasModulePermission 判定时沿祖先链自动继承(逻辑继承,不物理写子组)。
// isAll=0 → inheritFlag=false:仅本组生效。
// —— 与「应用不继承 / 模块可继承」对称,复用现有权限判定,不破坏其他功能。
// —— 若需「物理写子组」:将下方 upsert/delete 作用范围由 [groupId] 改为 [groupId + 其全部子孙组] 即可
// (子孙组集合可由 sys_group 全量构建 parentId→children 映射后 DFS 得到,参考本类 ancestorChain 的上行写法)。
Set<Integer> scopeAppIds = collectAppIdsForAuthzScope(groupId);
for (Integer mid : targetModuleIds) {
if (modeEnum == SysModeEnum.APPEND) {
// 边界一致:该组须已开通该应用,未开通应用的模块权限无意义
if (!sysGroupAppRelRepository.existsByGroupIdAndAppId(groupId, appId)) {
throw new DException("组 " + groupId + " 未开通应用 " + appId + ",无法授权其模块 " + mid);
if (!scopeAppIds.contains(appId)) {
throw new DException("组及其祖先均未开通应用 " + appId + ",无法授权其模块 " + mid);
}
SysGroupModulePerm existing = sysGroupModulePermRepository.findByGroupIdAndModuleId(groupId, mid);
if (existing != null) {
@@ -457,7 +458,7 @@ public class GroupAuthzService implements IGroupAuthzService {
perm.setInheritFlag(isAll);
sysGroupModulePermRepository.save(perm);
}
} else { // DELETE 取消模块授权
} else {
sysGroupModulePermRepository.deleteByGroupIdAndModuleIdBulk(groupId, mid);
}
}