✨ 用户编辑支持数据管理权限多选组织并与角色权限并集

Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
2026-11-04 03:14:44 +08:00
parent fd3ca7496d
commit a0e30d5834
6 changed files with 193 additions and 9 deletions
@@ -38,9 +38,12 @@ import org.springblade.system.service.IUserService;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import io.swagger.v3.oas.annotations.Hidden;
import java.util.List;
/**
* 用户服务Feign实现类
*
@@ -60,6 +63,12 @@ public class UserClient implements IUserClient {
return R.data(service.getById(userId));
}
@Override
@GetMapping(MANAGE_DEPT_IDS)
public R<List<Long>> manageDeptIds(@RequestParam("userId") Long userId) {
return R.data(service.resolveManageDeptIds(userId));
}
@Override
@GetMapping(USER_BY_ACCOUNT)
public R<User> userByAccount(String tenantId, String account) {
@@ -257,6 +257,14 @@ public interface IUserService extends BaseService<User> {
*/
boolean removeUser(String userIds);
/**
* 解析用户「数据管理权限」组织(含下级展开)
*
* @param userId 用户主键
* @return 可管理组织 ID 集合;无配置时返回空列表
*/
List<Long> resolveManageDeptIds(Long userId);
/**
* 导入用户数据(仅超管 + Excel 显式指定时允许跨租户,否则归属当前会话租户)
*
@@ -86,6 +86,7 @@ import java.util.ArrayList;
import java.util.Collections;
import java.util.Date;
import java.util.LinkedHashMap;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Map;
import java.util.Objects;
@@ -663,6 +664,7 @@ public class UserServiceImpl extends BaseServiceImpl<UserMapper, User> implement
boolean tempUser = this.deleteLogic(Func.toLongList(userIds));
boolean tempUserDept = userDeptService.remove(Wrappers.<UserDept>lambdaQuery().in(UserDept::getUserId, Func.toLongList(userIds)));
if (tempUser && tempUserDept) {
userDataScopeMapper.delete(Wrappers.<UserDataScope>lambdaQuery().in(UserDataScope::getUserId, idList));
UserOauth userOauth = new UserOauth();
userOauth.delete(Wrappers.<UserOauth>lambdaQuery().in(UserOauth::getUserId, Func.toLongList(userIds)));
UserWeb userWeb = new UserWeb();
@@ -672,6 +674,7 @@ public class UserServiceImpl extends BaseServiceImpl<UserMapper, User> implement
UserOther userOther = new UserOther();
userOther.delete(Wrappers.<UserOther>lambdaQuery().in(UserOther::getUserId, Func.toLongList(userIds)));
CacheUtil.clear(USER_CACHE);
CacheUtil.clear(USER_CACHE, Boolean.FALSE);
return true;
} else {
throw new ServiceException("删除用户失败!");
@@ -1119,8 +1122,10 @@ public class UserServiceImpl extends BaseServiceImpl<UserMapper, User> implement
if (user.getPersonCategory() == null) {
user.setPersonCategory(1);
}
// 组织/客商数据权限已迁移至角色菜单配置,用户侧字段仅保留兼容默认值,不再参与过滤。
if (user.getDataScopeRange() == null) {
// 有「数据管理权限」组织时标记为自定义;否则仅补兼容默认值(业务过滤以角色权限 ∪ 用户管理组织为准)
if (Func.isNotEmpty(user.getDataScopeDeptIds())) {
user.setDataScopeRange(3);
} else if (user.getDataScopeRange() == null) {
user.setDataScopeRange(2);
}
if (user.getDataLevelRange() == null) {
@@ -1141,8 +1146,33 @@ public class UserServiceImpl extends BaseServiceImpl<UserMapper, User> implement
return null;
}
/**
* 保存用户「数据管理权限」组织;dataScopeDeptIds 为 null 时保留原配置(兼容 IAM/同步等未传该字段的调用)。
*/
private boolean submitUserScopes(User user) {
// 用户级组织/客商数据权限已停用,改由角色菜单数据权限配置。
if (user == null || user.getId() == null) {
return true;
}
if (user.getDataScopeDeptIds() == null) {
return true;
}
userDataScopeMapper.delete(Wrappers.<UserDataScope>lambdaQuery().eq(UserDataScope::getUserId, user.getId()));
List<Long> deptIdList = user.getDataScopeDeptIds().stream()
.filter(Objects::nonNull)
.filter(deptId -> deptId > 0)
.distinct()
.toList();
for (Long deptId : deptIdList) {
UserDataScope scope = new UserDataScope();
scope.setUserId(user.getId());
scope.setDeptId(deptId);
userDataScopeMapper.insert(scope);
}
this.update(Wrappers.<User>lambdaUpdate()
.set(User::getDataScopeRange, deptIdList.isEmpty() ? 2 : 3)
.eq(User::getId, user.getId()));
CacheUtil.clear(USER_CACHE);
CacheUtil.clear(USER_CACHE, Boolean.FALSE);
return true;
}
@@ -1150,11 +1180,46 @@ public class UserServiceImpl extends BaseServiceImpl<UserMapper, User> implement
if (user == null || user.getId() == null) {
return user;
}
user.setDataScopeDeptIds(Collections.emptyList());
List<Long> deptIdList = userDataScopeMapper.selectList(Wrappers.<UserDataScope>lambdaQuery()
.eq(UserDataScope::getUserId, user.getId()))
.stream()
.map(UserDataScope::getDeptId)
.filter(Objects::nonNull)
.distinct()
.toList();
user.setDataScopeDeptIds(deptIdList);
user.setCustomerIds(Collections.emptyList());
return user;
}
@Override
public List<Long> resolveManageDeptIds(Long userId) {
if (Func.isEmpty(userId) || userId <= 0) {
return Collections.emptyList();
}
List<Long> baseDeptIds = userDataScopeMapper.selectList(Wrappers.<UserDataScope>lambdaQuery()
.eq(UserDataScope::getUserId, userId))
.stream()
.map(UserDataScope::getDeptId)
.filter(Objects::nonNull)
.filter(deptId -> deptId > 0)
.distinct()
.toList();
if (Func.isEmpty(baseDeptIds)) {
return Collections.emptyList();
}
LinkedHashSet<Long> allowedDeptIds = new LinkedHashSet<>();
for (Long deptId : baseDeptIds) {
List<Long> childIds = SysCache.getDeptChildIds(deptId);
if (Func.isNotEmpty(childIds)) {
allowedDeptIds.addAll(childIds);
} else {
allowedDeptIds.add(deptId);
}
}
return List.copyOf(allowedDeptIds);
}
/**
* 统一的租户范围过滤:超管放行,其他用户强制限定为当前会话租户。
*/