diff --git a/pigx-common/pigx-common-data/src/main/java/com/pig4cloud/pigx/common/data/datascope/PigxDefaultDataScopeHandle.java b/pigx-common/pigx-common-data/src/main/java/com/pig4cloud/pigx/common/data/datascope/PigxDefaultDataScopeHandle.java index 37bd4bc95..083278b8e 100644 --- a/pigx-common/pigx-common-data/src/main/java/com/pig4cloud/pigx/common/data/datascope/PigxDefaultDataScopeHandle.java +++ b/pigx-common/pigx-common-data/src/main/java/com/pig4cloud/pigx/common/data/datascope/PigxDefaultDataScopeHandle.java @@ -22,17 +22,18 @@ import cn.hutool.core.util.StrUtil; import com.pig4cloud.pigx.admin.api.entity.SysDept; import com.pig4cloud.pigx.admin.api.entity.SysRole; import com.pig4cloud.pigx.admin.api.feign.RemoteDataScopeService; +import com.pig4cloud.pigx.common.core.constant.SecurityConstants; import com.pig4cloud.pigx.common.core.constant.enums.UserTypeEnum; import com.pig4cloud.pigx.common.core.util.RetOps; import com.pig4cloud.pigx.common.security.service.PigxUser; import com.pig4cloud.pigx.common.security.util.SecurityUtils; import lombok.RequiredArgsConstructor; +import org.springframework.security.core.GrantedAuthority; import java.util.Arrays; import java.util.Collections; import java.util.List; import java.util.Objects; -import java.util.stream.Collectors; /** * @author lengleng @@ -50,108 +51,115 @@ import java.util.stream.Collectors; @RequiredArgsConstructor public class PigxDefaultDataScopeHandle implements DataScopeHandle { - private final RemoteDataScopeService dataScopeService; + private final RemoteDataScopeService dataScopeService; - @Override - public Boolean calcScope(DataScope dataScope) { - // 对于TOC客户端,不进行数据权限校验 - PigxUser user = SecurityUtils.getUser(); - if (UserTypeEnum.TOC.getStatus().equals(user.getUserType())) { - return true; - } + @Override + public Boolean calcScope(DataScope dataScope) { + // 对于TOC客户端,不进行数据权限校验 + PigxUser user = SecurityUtils.getUser(); + if (UserTypeEnum.TOC.getStatus().equals(user.getUserType())) { + return true; + } - // 业务代码里的规则,覆盖计算规则 - if (StrUtil.isNotBlank(dataScope.getUsername()) || CollUtil.isNotEmpty(dataScope.getDeptList())) { - return false; - } + // 业务代码里的规则,覆盖计算规则 + if (StrUtil.isNotBlank(dataScope.getUsername()) || CollUtil.isNotEmpty(dataScope.getDeptList())) { + return false; + } - // 获取用户角色ID列表 - List roleIdList = SecurityUtils.getRoleIds(); - if (CollUtil.isEmpty(roleIdList)) { - return false; - } + // 获取用户角色ID列表 + List roleIdList = user.getAuthorities() + .stream() + .map(GrantedAuthority::getAuthority) + .filter(authority -> authority.startsWith(SecurityConstants.ROLE)) + .map(authority -> authority.split(StrUtil.UNDERLINE)[1]) + .toList(); + if (CollUtil.isEmpty(roleIdList)) { + return false; + } - // 获取角色列表 - List roleList = RetOps.of(dataScopeService.getRoleList(roleIdList)) - .getData() - .orElseGet(Collections::emptyList); - if (CollUtil.isEmpty(roleList)) { - return false; - } + // 获取角色列表 + List roleList = RetOps.of(dataScopeService.getRoleList(roleIdList)) + .getData() + .orElseGet(Collections::emptyList); + if (CollUtil.isEmpty(roleList)) { + return false; + } - // 处理数据权限 - return processDataScope(user, dataScope, roleList); - } + // 处理数据权限 + return processDataScope(user, dataScope, roleList); + } + /** + * 处理数据权限 + */ + private boolean processDataScope(PigxUser user, DataScope dataScope, List roleList) { + List deptList = dataScope.getDeptList(); - /** - * 处理数据权限 - */ - private boolean processDataScope(PigxUser user, DataScope dataScope, List roleList) { - List deptList = dataScope.getDeptList(); + for (SysRole role : roleList) { + Integer dsType = role.getDsType(); - for (SysRole role : roleList) { - Integer dsType = role.getDsType(); + // 处理不同数据权限类型 + switch (Objects.requireNonNull(DataScopeTypeEnum.getByType(dsType))) { + case ALL: + return true; + case CUSTOM: + handleCustomScope(role, deptList); + break; + case OWN_CHILD_LEVEL: + handleOwnChildLevelScope(user, deptList); + break; + case OWN_LEVEL: + handleOwnLevelScope(user, deptList); + break; + case SELF_LEVEL: + handleSelfLevelScope(user, dataScope); + break; + default: + break; + } + } - // 处理不同数据权限类型 - switch (Objects.requireNonNull(DataScopeTypeEnum.getByType(dsType))) { - case ALL: - return true; - case CUSTOM: - handleCustomScope(role, deptList); - break; - case OWN_CHILD_LEVEL: - handleOwnChildLevelScope(user, deptList); - break; - case OWN_LEVEL: - handleOwnLevelScope(user, deptList); - break; - case SELF_LEVEL: - handleSelfLevelScope(user, dataScope); - break; - default: - break; - } - } + return false; + } - return false; - } + /** + * 处理自定义数据权限 + */ + private void handleCustomScope(SysRole role, List deptList) { + if (StrUtil.isNotBlank(role.getDsScope())) { + deptList.addAll(Arrays.stream(role.getDsScope().split(StrUtil.COMMA)).map(Long::parseLong).toList()); + } + } - /** - * 处理自定义数据权限 - */ - private void handleCustomScope(SysRole role, List deptList) { - if (StrUtil.isNotBlank(role.getDsScope())) { - deptList.addAll(Arrays.stream(role.getDsScope().split(StrUtil.COMMA)) - .map(Long::parseLong) - .collect(Collectors.toList())); - } - } + /** + * 处理本级及下级数据权限 + */ + private void handleOwnChildLevelScope(PigxUser user, List deptList) { - /** - * 处理本级及下级数据权限 - */ - private void handleOwnChildLevelScope(PigxUser user, List deptList) { - List descendantDeptIds = RetOps.of(dataScopeService.getDescendantList(user.getDeptId())) - .getData() - .orElseGet(Collections::emptyList) - .stream() - .map(SysDept::getDeptId) - .collect(Collectors.toList()); - deptList.addAll(descendantDeptIds); - } + for (Long deptId : user.getDeptIds()) { + List descendantDeptIds = RetOps.of(dataScopeService.getDescendantList(deptId)) + .getData() + .orElseGet(Collections::emptyList) + .stream() + .map(SysDept::getDeptId) + .toList(); + deptList.addAll(descendantDeptIds); + } - /** - * 处理本级数据权限 - */ - private void handleOwnLevelScope(PigxUser user, List deptList) { - deptList.add(user.getDeptId()); - } + } + + /** + * 处理本级数据权限 + */ + private void handleOwnLevelScope(PigxUser user, List deptList) { + deptList.addAll(user.getDeptIds()); + } + + /** + * 处理个人数据权限 + */ + private void handleSelfLevelScope(PigxUser user, DataScope dataScope) { + dataScope.setUsername(user.getUsername()); + } - /** - * 处理个人数据权限 - */ - private void handleSelfLevelScope(PigxUser user, DataScope dataScope) { - dataScope.setUsername(user.getUsername()); - } } diff --git a/pigx-common/pigx-common-security/src/main/java/com/pig4cloud/pigx/common/security/service/PigxTocDefaultUserDetailsServiceImpl.java b/pigx-common/pigx-common-security/src/main/java/com/pig4cloud/pigx/common/security/service/PigxTocDefaultUserDetailsServiceImpl.java index 39ffeea7a..a561ad446 100755 --- a/pigx-common/pigx-common-security/src/main/java/com/pig4cloud/pigx/common/security/service/PigxTocDefaultUserDetailsServiceImpl.java +++ b/pigx-common/pigx-common-security/src/main/java/com/pig4cloud/pigx/common/security/service/PigxTocDefaultUserDetailsServiceImpl.java @@ -110,7 +110,7 @@ public class PigxTocDefaultUserDetailsServiceImpl implements PigxUserDetailsServ AppUser user = info.getAppUser(); // 构造security用户 - return new PigxUser(user.getUserId(), user.getUsername(), null, user.getPhone(), user.getAvatar(), + return new PigxUser(user.getUserId(), user.getUsername(), null, null, null, user.getPhone(), user.getAvatar(), user.getNickname(), user.getName(), user.getEmail(), user.getTenantId(), SecurityConstants.BCRYPT + user.getPassword(), true, true, UserTypeEnum.TOC.getStatus(), true, null, !CommonConstants.STATUS_LOCK.equals(user.getLockFlag()), authorities); diff --git a/pigx-common/pigx-common-security/src/main/java/com/pig4cloud/pigx/common/security/service/PigxTocMobileUserDetailsServiceImpl.java b/pigx-common/pigx-common-security/src/main/java/com/pig4cloud/pigx/common/security/service/PigxTocMobileUserDetailsServiceImpl.java index b0b6f5b48..f510ba320 100644 --- a/pigx-common/pigx-common-security/src/main/java/com/pig4cloud/pigx/common/security/service/PigxTocMobileUserDetailsServiceImpl.java +++ b/pigx-common/pigx-common-security/src/main/java/com/pig4cloud/pigx/common/security/service/PigxTocMobileUserDetailsServiceImpl.java @@ -100,7 +100,7 @@ public class PigxTocMobileUserDetailsServiceImpl implements PigxUserDetailsServi AppUser user = info.getAppUser(); // 构造security用户 - return new PigxUser(user.getUserId(), user.getUsername(), null, user.getPhone(), user.getAvatar(), + return new PigxUser(user.getUserId(), user.getUsername(), null, null, null, user.getPhone(), user.getAvatar(), user.getNickname(), user.getName(), user.getEmail(), user.getTenantId(), SecurityConstants.BCRYPT + user.getPassword(), true, true, UserTypeEnum.TOC.getStatus(), true, null, !CommonConstants.STATUS_LOCK.equals(user.getLockFlag()), authorities); diff --git a/pigx-common/pigx-common-security/src/main/java/com/pig4cloud/pigx/common/security/service/PigxUser.java b/pigx-common/pigx-common-security/src/main/java/com/pig4cloud/pigx/common/security/service/PigxUser.java index 8d7a5b48d..bf6503f37 100644 --- a/pigx-common/pigx-common-security/src/main/java/com/pig4cloud/pigx/common/security/service/PigxUser.java +++ b/pigx-common/pigx-common-security/src/main/java/com/pig4cloud/pigx/common/security/service/PigxUser.java @@ -30,6 +30,7 @@ import java.io.Serial; import java.time.LocalDateTime; import java.util.Collection; import java.util.HashMap; +import java.util.List; import java.util.Map; /** @@ -52,11 +53,19 @@ public class PigxUser extends User implements OAuth2AuthenticatedPrincipal { @Getter private Long id; + /** 角色ID列表 */ + @Getter + private List roleIds; + /** * 部门ID */ @Getter - private Long deptId; + private List deptIds; + + /** 岗位ID列表 */ + @Getter + private List postIds; /** * 手机号 @@ -106,7 +115,7 @@ public class PigxUser extends User implements OAuth2AuthenticatedPrincipal { * Construct the User with the details required by * {@link DaoAuthenticationProvider}. * @param id 用户ID - * @param deptId 部门ID + * @param deptIds 部门ID * @param tenantId 租户ID * @param nickname 昵称 * @param name 姓名 @@ -126,7 +135,8 @@ public class PigxUser extends User implements OAuth2AuthenticatedPrincipal { */ @JsonCreator public PigxUser(@JsonProperty("id") Long id, @JsonProperty("username") String username, - @JsonProperty("deptId") Long deptId, @JsonProperty("phone") String phone, + @JsonProperty("roleIds") List roleIds, @JsonProperty("deptIds") List deptIds, + @JsonProperty("postIds") List postIds, @JsonProperty("phone") String phone, @JsonProperty("avatar") String avatar, @JsonProperty("nickname") String nickname, @JsonProperty("name") String name, @JsonProperty("email") String email, @JsonProperty("tenantId") Long tenantId, @JsonProperty("password") String password, @@ -138,7 +148,9 @@ public class PigxUser extends User implements OAuth2AuthenticatedPrincipal { @JsonProperty("authorities") Collection authorities) { super(username, password, enabled, accountNonExpired, credentialsNonExpired, accountNonLocked, authorities); this.id = id; - this.deptId = deptId; + this.roleIds = roleIds; + this.deptIds = deptIds; + this.postIds = postIds; this.phone = phone; this.avatar = avatar; this.tenantId = tenantId; diff --git a/pigx-common/pigx-common-security/src/main/java/com/pig4cloud/pigx/common/security/service/PigxUserDetailsService.java b/pigx-common/pigx-common-security/src/main/java/com/pig4cloud/pigx/common/security/service/PigxUserDetailsService.java index 34e71b0d4..e156865dc 100644 --- a/pigx-common/pigx-common-security/src/main/java/com/pig4cloud/pigx/common/security/service/PigxUserDetailsService.java +++ b/pigx-common/pigx-common-security/src/main/java/com/pig4cloud/pigx/common/security/service/PigxUserDetailsService.java @@ -16,8 +16,10 @@ */ package com.pig4cloud.pigx.common.security.service; -import cn.hutool.core.collection.CollUtil; import com.pig4cloud.pigx.admin.api.dto.UserInfo; +import com.pig4cloud.pigx.admin.api.entity.SysDept; +import com.pig4cloud.pigx.admin.api.entity.SysPost; +import com.pig4cloud.pigx.admin.api.entity.SysRole; import com.pig4cloud.pigx.common.core.constant.CommonConstants; import com.pig4cloud.pigx.common.core.constant.SecurityConstants; import com.pig4cloud.pigx.common.core.constant.enums.UserTypeEnum; @@ -92,12 +94,10 @@ public interface PigxUserDetailsService extends UserDetailsService, Ordered { Collection authorities = AuthorityUtils .createAuthorityList(dbAuthsSet.toArray(new String[0])); - // 构造security用户 - Long deptId = null; - if (CollUtil.isNotEmpty(info.getDeptList())) { - deptId = CollUtil.getFirst(info.getDeptList()).getDeptId(); - } - return new PigxUser(info.getUserId(), info.getUsername(), deptId, info.getPhone(), info.getAvatar(), + return new PigxUser(info.getUserId(), info.getUsername(), + info.getRoleList().stream().map(SysRole::getRoleId).toList(), + info.getDeptList().stream().map(SysDept::getDeptId).toList(), + info.getPostList().stream().map(SysPost::getPostId).toList(), info.getPhone(), info.getAvatar(), info.getNickname(), info.getName(), info.getEmail(), info.getTenantId(), SecurityConstants.BCRYPT + info.getPassword(), true, true, UserTypeEnum.TOB.getStatus(), !CommonConstants.STATUS_LOCK.equals(info.getPasswordExpireFlag()) // 密码过期判断 diff --git a/pigx-upms/pigx-upms-api/src/main/java/com/pig4cloud/pigx/admin/api/constant/OrgTypeEnum.java b/pigx-upms/pigx-upms-api/src/main/java/com/pig4cloud/pigx/admin/api/constant/OrgTypeEnum.java index 09b109765..03877f7bf 100644 --- a/pigx-upms/pigx-upms-api/src/main/java/com/pig4cloud/pigx/admin/api/constant/OrgTypeEnum.java +++ b/pigx-upms/pigx-upms-api/src/main/java/com/pig4cloud/pigx/admin/api/constant/OrgTypeEnum.java @@ -8,7 +8,7 @@ package com.pig4cloud.pigx.admin.api.constant; */ public enum OrgTypeEnum { - USER("user", "用户"), ROLE("role", "角色"), DEPT("dept", "部门"); + USER("user", "用户"), ROLE("role", "角色"), DEPT("dept", "部门"), POST("post", "岗位"); private String type; diff --git a/pigx-upms/pigx-upms-api/src/main/java/com/pig4cloud/pigx/admin/api/vo/UserVO.java b/pigx-upms/pigx-upms-api/src/main/java/com/pig4cloud/pigx/admin/api/vo/UserVO.java index ea553740e..53f74b811 100644 --- a/pigx-upms/pigx-upms-api/src/main/java/com/pig4cloud/pigx/admin/api/vo/UserVO.java +++ b/pigx-upms/pigx-upms-api/src/main/java/com/pig4cloud/pigx/admin/api/vo/UserVO.java @@ -31,6 +31,7 @@ import lombok.Data; import java.io.Serial; import java.io.Serializable; import java.time.LocalDateTime; +import java.util.ArrayList; import java.util.List; /** @@ -41,149 +42,149 @@ import java.util.List; @Schema(description = "前端用户展示对象") public class UserVO implements Serializable { - @Serial - private static final long serialVersionUID = 1L; + @Serial + private static final long serialVersionUID = 1L; - /** - * 主键ID - */ - @Schema(description = "主键") - private Long userId; + /** + * 主键ID + */ + @Schema(description = "主键") + private Long userId; - /** - * 用户名 - */ - @Schema(description = "用户名") - private String username; + /** + * 用户名 + */ + @Schema(description = "用户名") + private String username; - /** - * 密码 - */ - @JsonIgnore - private String password; + /** + * 密码 + */ + @JsonIgnore + private String password; - /** - * 随机盐 - */ - @JsonIgnore - private String salt; + /** + * 随机盐 + */ + @JsonIgnore + private String salt; - /** - * 微信openid - */ - @Schema(description = "微信open id") - private String wxOpenid; + /** + * 微信openid + */ + @Schema(description = "微信open id") + private String wxOpenid; - /** - * QQ openid - */ - @Schema(description = "qq open id") - private String qqOpenid; + /** + * QQ openid + */ + @Schema(description = "qq open id") + private String qqOpenid; - /** - * gitee openid - */ - @Schema(description = "gitee open id") - private String giteeOpenId; + /** + * gitee openid + */ + @Schema(description = "gitee open id") + private String giteeOpenId; - /** - * 开源中国 openid - */ - @Schema(description = "开源中国 open id") - private String oscOpenId; + /** + * 开源中国 openid + */ + @Schema(description = "开源中国 open id") + private String oscOpenId; - /** - * 企微微信 userid - */ - @Schema(description = "企微微信 userid") - private String wxCpUserid; + /** + * 企微微信 userid + */ + @Schema(description = "企微微信 userid") + private String wxCpUserid; - /** - * 钉钉 userid - */ - @Schema(description = "钉钉 userid") - private String wxDingUserid; + /** + * 钉钉 userid + */ + @Schema(description = "钉钉 userid") + private String wxDingUserid; - /** - * 创建时间 - */ - @Schema(description = "创建时间") - private LocalDateTime createTime; + /** + * 创建时间 + */ + @Schema(description = "创建时间") + private LocalDateTime createTime; - /** - * 修改时间 - */ - @Schema(description = "修改时间") - private LocalDateTime updateTime; + /** + * 修改时间 + */ + @Schema(description = "修改时间") + private LocalDateTime updateTime; - /** - * 0-正常,1-删除 - */ - @Schema(description = "删除标记,1:已删除,0:正常") - private String delFlag; + /** + * 0-正常,1-删除 + */ + @Schema(description = "删除标记,1:已删除,0:正常") + private String delFlag; - /** - * 锁定标记 - */ - @Schema(description = "锁定标记,0:正常,9:已锁定") - private String lockFlag; + /** + * 锁定标记 + */ + @Schema(description = "锁定标记,0:正常,9:已锁定") + private String lockFlag; - /** - * 密码过期标记 - */ - @Schema(description = "密码过期标记") - private String passwordExpireFlag; + /** + * 密码过期标记 + */ + @Schema(description = "密码过期标记") + private String passwordExpireFlag; - /** - * 密码修改时间 - */ - @Schema(description = "密码修改时间") - private LocalDateTime passwordModifyTime; + /** + * 密码修改时间 + */ + @Schema(description = "密码修改时间") + private LocalDateTime passwordModifyTime; - /** - * 手机号 - */ - @Sensitive(type = SensitiveTypeEnum.MOBILE_PHONE) - @Schema(description = "手机号") - private String phone; + /** + * 手机号 + */ + @Sensitive(type = SensitiveTypeEnum.MOBILE_PHONE) + @Schema(description = "手机号") + private String phone; - /** - * 头像 - */ - @Schema(description = "头像") - private String avatar; + /** + * 头像 + */ + @Schema(description = "头像") + private String avatar; - /** - * 租户ID - */ - @Schema(description = "所属租户") - private Long tenantId; + /** + * 租户ID + */ + @Schema(description = "所属租户") + private Long tenantId; - @Schema(description = "所属部门名称") - private List deptList; + @Schema(description = "所属部门名称") + private List deptList = new ArrayList<>(); - /** - * 角色列表 - */ - @Schema(description = "拥有的角色列表") - private List roleList; + /** + * 角色列表 + */ + @Schema(description = "拥有的角色列表") + private List roleList = new ArrayList<>(); - /** - * 岗位列表 - */ - @Schema(description = "拥有的岗位列表") - private List postList; + /** + * 岗位列表 + */ + @Schema(description = "拥有的岗位列表") + private List postList = new ArrayList<>(); - /** - * 昵称 - */ - @Schema(description = "昵称") - private String nickname; + /** + * 昵称 + */ + @Schema(description = "昵称") + private String nickname; - /** - * 姓名 - */ - @Schema(description = "姓名") + /** + * 姓名 + */ + @Schema(description = "姓名") private String name; /** diff --git a/pigx-upms/pigx-upms-biz/src/main/java/com/pig4cloud/pigx/admin/service/impl/SysDeptServiceImpl.java b/pigx-upms/pigx-upms-biz/src/main/java/com/pig4cloud/pigx/admin/service/impl/SysDeptServiceImpl.java index 7874b697c..104b10713 100644 --- a/pigx-upms/pigx-upms-biz/src/main/java/com/pig4cloud/pigx/admin/service/impl/SysDeptServiceImpl.java +++ b/pigx-upms/pigx-upms-biz/src/main/java/com/pig4cloud/pigx/admin/service/impl/SysDeptServiceImpl.java @@ -80,7 +80,7 @@ public class SysDeptServiceImpl extends ServiceImpl impl @Transactional(rollbackFor = Exception.class) public Boolean removeDeptById(Long id) { // 级联删除部门 - List idList = this.listDescendant(id).stream().map(SysDept::getDeptId).toList(); + List idList = this.listDescendant(id).stream().map(SysDept::getDeptId).toList(); Optional.ofNullable(idList).filter(CollUtil::isNotEmpty).ifPresent(this::removeByIds); @@ -97,34 +97,34 @@ public class SysDeptServiceImpl extends ServiceImpl impl public List> selectTree(String deptName, Long parentId) { // 查询全部部门 List deptAllList = deptMapper - .selectList(Wrappers.lambdaQuery().like(StrUtil.isNotBlank(deptName), SysDept::getName, deptName)); + .selectList(Wrappers.lambdaQuery().like(StrUtil.isNotBlank(deptName), SysDept::getName, deptName)); // 查询数据权限内部门 List deptOwnIdList = deptMapper - .selectListByScope( - Wrappers.lambdaQuery().like(StrUtil.isNotBlank(deptName), SysDept::getName, deptName), - DataScope.of()) - .stream() - .map(SysDept::getDeptId) - .toList(); + .selectListByScope( + Wrappers.lambdaQuery().like(StrUtil.isNotBlank(deptName), SysDept::getName, deptName), + DataScope.of()) + .stream() + .map(SysDept::getDeptId) + .toList(); // 权限内部门 List> collect = deptAllList.stream() - .filter(dept -> dept.getDeptId().intValue() != dept.getParentId()) - .sorted(Comparator.comparingInt(SysDept::getSortOrder)) - .map(dept -> { - TreeNode treeNode = new TreeNode<>(); - treeNode.setId(dept.getDeptId()); - treeNode.setParentId(dept.getParentId()); - treeNode.setName(dept.getName()); - treeNode.setWeight(dept.getSortOrder()); - // 有权限不返回标识 - Map extra = new HashMap<>(8); - extra.put("isLock", !deptOwnIdList.contains(dept.getDeptId())); - extra.put("createTime", dept.getCreateTime()); - treeNode.setExtra(extra); - return treeNode; - }) - .toList(); + .filter(dept -> dept.getDeptId().intValue() != dept.getParentId()) + .sorted(Comparator.comparingInt(SysDept::getSortOrder)) + .map(dept -> { + TreeNode treeNode = new TreeNode<>(); + treeNode.setId(dept.getDeptId()); + treeNode.setParentId(dept.getParentId()); + treeNode.setName(dept.getName()); + treeNode.setWeight(dept.getSortOrder()); + // 有权限不返回标识 + Map extra = new HashMap<>(8); + extra.put("isLock", !deptOwnIdList.contains(dept.getDeptId())); + extra.put("createTime", dept.getCreateTime()); + treeNode.setExtra(extra); + return treeNode; + }) + .toList(); // 模糊查询 不组装树结构 直接返回 表格方便编辑 if (StrUtil.isNotBlank(deptName)) { @@ -133,7 +133,7 @@ public class SysDeptServiceImpl extends ServiceImpl impl tree.putAll(node.getExtra()); BeanUtils.copyProperties(node, tree); return tree; - }).toList(); + }).toList(); } return TreeUtil.build(collect, parentId == null ? 0 : parentId); @@ -150,14 +150,14 @@ public class SysDeptServiceImpl extends ServiceImpl impl DeptExcelVO deptExcelVo = new DeptExcelVO(); deptExcelVo.setName(item.getName()); Optional first = this.list() - .stream() - .filter(it -> item.getParentId().equals(it.getDeptId())) - .map(SysDept::getName) - .findFirst(); + .stream() + .filter(it -> item.getParentId().equals(it.getDeptId())) + .map(SysDept::getName) + .findFirst(); deptExcelVo.setParentName(first.orElse("根部门")); deptExcelVo.setSortOrder(item.getSortOrder()); return deptExcelVo; - }).toList(); + }).toList(); return deptExcelVos; } @@ -186,7 +186,8 @@ public class SysDeptServiceImpl extends ServiceImpl impl sysDept.setParentId(one.getDeptId()); sysDept.setSortOrder(item.getSortOrder()); baseMapper.insert(sysDept); - } else { + } + else { // 数据不合法情况 errorMessageList.add(new ErrorMessage(item.getLineNum(), errorMsg)); } @@ -226,30 +227,30 @@ public class SysDeptServiceImpl extends ServiceImpl impl @Override public List listDeptLeader(Long deptId) { - List userDeptList = userDeptMapper - .selectList(Wrappers.lambdaQuery().eq(SysUserDept::getDeptId, deptId)); + List userDeptList = userDeptMapper + .selectList(Wrappers.lambdaQuery().eq(SysUserDept::getDeptId, deptId)); if (CollUtil.isEmpty(userDeptList)) { return null; } SysPost deptLeader = postMapper - .selectOne(Wrappers.lambdaQuery().eq(SysPost::getPostCode, PostCodeEnum.TEAM_LEADER.getCode())); + .selectOne(Wrappers.lambdaQuery().eq(SysPost::getPostCode, PostCodeEnum.TEAM_LEADER.getCode())); if (deptLeader == null) { return null; } List userIdList = userDeptList.stream().map(SysUserDept::getUserId).toList(); return userPostMapper.selectList(Wrappers.lambdaQuery().in(SysUserPost::getUserId, userIdList)) - .stream() - .filter(post -> Objects.equals(post.getPostId(), deptLeader.getPostId())) - .map(SysUserPost::getUserId) - .toList(); + .stream() + .filter(post -> Objects.equals(post.getPostId(), deptLeader.getPostId())) + .map(SysUserPost::getUserId) + .toList(); } /** * 根据父部门ID和类型获取组织树 * @param parentDeptId 父部门ID - * @param type 类型 + * @param type 类型 * @return 组织树信息 */ public Map listOrgTree(Long parentDeptId, String type) { @@ -270,15 +271,37 @@ public class SysDeptServiceImpl extends ServiceImpl impl }); return Dict.create() - .set("roleList", orgs) - .set("childDepartments", orgs) - .set("employees", new ArrayList<>()); + .set("roleList", orgs) + .set("postList", new ArrayList<>()) + .set("childDepartments", orgs) + .set("employees", new ArrayList<>()); + } + + if (StrUtil.equals(type, OrgTypeEnum.POST.getType())) { + // 角色 + List postList = postMapper.selectList(Wrappers.emptyWrapper()); + + postList.forEach(post -> { + OrgTreeVO orgTreeVo = new OrgTreeVO(); + orgTreeVo.setId(post.getPostId()); + orgTreeVo.setName(post.getPostName()); + orgTreeVo.setType(OrgTypeEnum.POST.getType()); + orgTreeVo.setSelected(false); + orgs.add(orgTreeVo); + }); + + return Dict.create() + .set("roleList", new ArrayList<>()) + .set("postList", orgs) + .set("childDepartments", orgs) + .set("employees", new ArrayList<>()); } Dict dict = Dict.create() - .set("titleDepartments", new ArrayList<>()) - .set("roleList", new ArrayList<>()) - .set("employees", new ArrayList<>()); + .set("titleDepartments", new ArrayList<>()) + .set("roleList", new ArrayList<>()) + .set("postList", new ArrayList<>()) + .set("employees", new ArrayList<>()); List deptList = this.list( Wrappers.lambdaQuery().eq(Objects.nonNull(parentDeptId), SysDept::getParentId, parentDeptId)); @@ -301,8 +324,8 @@ public class SysDeptServiceImpl extends ServiceImpl impl MPJLambdaWrapper userQuery = new MPJLambdaWrapper<>(); userQuery.selectAll(SysUser.class) - .leftJoin(SysUserDept.class, SysUserDept::getUserId, SysUser::getUserId) - .eq(SysUserDept::getDeptId, parentDeptId); + .leftJoin(SysUserDept.class, SysUserDept::getUserId, SysUser::getUserId) + .eq(SysUserDept::getDeptId, parentDeptId); List userList = userMapper.selectJoinList(userQuery); @@ -334,23 +357,23 @@ public class SysDeptServiceImpl extends ServiceImpl impl */ public List getOrgTreeUser(String username) { return userMapper.selectList(Wrappers.lambdaQuery().like(SysUser::getUsername, username)) - .stream() - .map(user -> { - OrgTreeVO orgTreeVo = new OrgTreeVO(); - orgTreeVo.setId(user.getUserId()); - orgTreeVo.setName(user.getUsername()); - orgTreeVo.setType(OrgTypeEnum.USER.getType()); - orgTreeVo.setSelected(false); - orgTreeVo.setAvatar(user.getAvatar()); - return orgTreeVo; - }) - .toList(); + .stream() + .map(user -> { + OrgTreeVO orgTreeVo = new OrgTreeVO(); + orgTreeVo.setId(user.getUserId()); + orgTreeVo.setName(user.getUsername()); + orgTreeVo.setType(OrgTypeEnum.USER.getType()); + orgTreeVo.setSelected(false); + orgTreeVo.setAvatar(user.getAvatar()); + return orgTreeVo; + }) + .toList(); } /** * 递归查询所有子节点。 * @param allDeptList 所有部门列表 - * @param parentId 父部门ID + * @param parentId 父部门ID * @param resDeptList 结果集合 */ private void recursiveDept(List allDeptList, Long parentId, List resDeptList) {