fix: 优化权限代码
This commit is contained in:
parent
125ff31f7f
commit
d4250a304a
@ -52,4 +52,9 @@ public interface SysUserDataScopeMapper extends BaseMapper<SysUserDataScope> {
|
||||
* orgType=RVCD时关联SD_RVCD_DIC获取rvnm(流域名称)
|
||||
*/
|
||||
List<SysUserDataScope> selectValidPermissionsWithName(@Param("userId") String userId);
|
||||
|
||||
/**
|
||||
* 批量根据用户ID查询权限列表(含关联资源名称)
|
||||
*/
|
||||
List<SysUserDataScope> selectValidPermissionsWithNameByUserIds(@Param("userIds") List<String> userIds);
|
||||
}
|
||||
|
||||
@ -163,4 +163,10 @@ public class SysUser implements Serializable {
|
||||
|
||||
@TableField(exist = false)
|
||||
List<SysRole> roles;
|
||||
|
||||
@TableField(exist = false)
|
||||
private String basinNames;
|
||||
|
||||
@TableField(exist = false)
|
||||
private String stationNames;
|
||||
}
|
||||
|
||||
@ -65,9 +65,9 @@ public interface SysRoleMapper extends BaseMapper<SysRole> {
|
||||
|
||||
|
||||
/**
|
||||
* 批量获取用户角色
|
||||
* 批量获取用户角色(含userId映射)
|
||||
*/
|
||||
List<SysRole> getRolesByUserIds(@Param("userIds") List<String> userIds);
|
||||
List<Map<String, Object>> getUserRolesByUserIds(@Param("userIds") List<String> userIds);
|
||||
|
||||
/**********************************
|
||||
* 用途说明: 根据角色ID删除菜单与角色关联信息
|
||||
|
||||
@ -57,21 +57,13 @@ public class SysOrganizationServiceImpl extends ServiceImpl<SysOrganizationMappe
|
||||
@Override
|
||||
public List<Map<String, Object>> getOrgTree(String parentid,
|
||||
String params) {
|
||||
List<SysOrganization> orgList = new ArrayList<>();
|
||||
QueryWrapper<SysOrganization> queryWrapper = new QueryWrapper<>();
|
||||
//根据父级id查询
|
||||
queryWrapper.eq("parentid", parentid);
|
||||
if (StrUtil.isNotEmpty(params)) {
|
||||
queryWrapper.like("orgname", params); // 根据 部门名称
|
||||
}
|
||||
SysUser userInfo = userService.getUserInfo();
|
||||
|
||||
// 构建权限过滤条件
|
||||
Set<String> allowedOrgIds = new HashSet<>();
|
||||
if (userInfo.getUsertype() != 0) {
|
||||
List<SysRole> roleByUserId =
|
||||
sysRoleMapper.getRoleByUserId(userInfo.getId());
|
||||
List<String> ids = new ArrayList<>();
|
||||
// 循环当前角色
|
||||
List<SysRole> roleByUserId = sysRoleMapper.getRoleByUserId(userInfo.getId());
|
||||
for (SysRole sysRole : roleByUserId) {
|
||||
// 获取角色的组织Id
|
||||
String orgscope = sysRole.getOrgscope();
|
||||
if (StrUtil.isBlank(orgscope)) {
|
||||
continue;
|
||||
@ -79,21 +71,33 @@ public class SysOrganizationServiceImpl extends ServiceImpl<SysOrganizationMappe
|
||||
// 拆分组织Id
|
||||
String[] split = orgscope.split(",");
|
||||
List<String> stringList = Arrays.asList(split);
|
||||
Set<String> set = new HashSet<>();
|
||||
if (!stringList.isEmpty()) {
|
||||
List<SysOrganization> list =
|
||||
sysOrganizationMapper.selectList(new LambdaQueryWrapper<SysOrganization>().in(SysOrganization::getId, stringList));
|
||||
list.forEach(l -> set.add(l.getParentid()));
|
||||
allowedOrgIds.addAll(stringList);
|
||||
// 查询这些组织的父级ID
|
||||
List<SysOrganization> list = sysOrganizationMapper.selectList(
|
||||
new LambdaQueryWrapper<SysOrganization>().in(SysOrganization::getId, stringList));
|
||||
for (SysOrganization org : list) {
|
||||
if (org.getParentid() != null) {
|
||||
allowedOrgIds.add(org.getParentid());
|
||||
}
|
||||
ids.addAll(stringList);
|
||||
ids.addAll(set);
|
||||
}
|
||||
queryWrapper.in("id", ids);
|
||||
}
|
||||
orgList = this.list(queryWrapper.orderByAsc("orgcode"));
|
||||
// 将实体对象转换为 Map,确保字段名与实体类一致
|
||||
// 将实体对象转换为 Map,确保字段名与实体类一致
|
||||
List<Map<String,Object>> listMap = orgList.stream().map(org -> {
|
||||
}
|
||||
}
|
||||
|
||||
// 查询所有组织数据
|
||||
QueryWrapper<SysOrganization> queryWrapper = new QueryWrapper<>();
|
||||
if (!allowedOrgIds.isEmpty()) {
|
||||
queryWrapper.in("id", allowedOrgIds);
|
||||
}
|
||||
if (StrUtil.isNotEmpty(params)) {
|
||||
queryWrapper.like("orgname", params);
|
||||
}
|
||||
|
||||
List<SysOrganization> allOrgList = this.list(queryWrapper.orderByAsc("orgcode"));
|
||||
|
||||
// 将所有组织数据转换为Map结构
|
||||
List<Map<String, Object>> allOrgMaps = allOrgList.stream().map(org -> {
|
||||
Map<String, Object> map = new HashMap<>();
|
||||
map.put("id", org.getId());
|
||||
map.put("orgtype", org.getOrgtype());
|
||||
@ -110,12 +114,39 @@ public class SysOrganizationServiceImpl extends ServiceImpl<SysOrganizationMappe
|
||||
map.put("custom3", org.getCustom3());
|
||||
return map;
|
||||
}).collect(Collectors.toList());
|
||||
for (Map<String, Object> map : listMap) {
|
||||
List<Map<String, Object>> childList = child(map.get(
|
||||
"id").toString());//查询下一子集
|
||||
map.put("childList", childList); //添加新列 子集
|
||||
|
||||
// 构建父子关系映射
|
||||
Map<String, List<Map<String, Object>>> parentToChildrenMap = new HashMap<>();
|
||||
for (Map<String, Object> orgMap : allOrgMaps) {
|
||||
String parentId = (String) orgMap.get("parentid");
|
||||
parentToChildrenMap.computeIfAbsent(parentId, k -> new ArrayList<>()).add(orgMap);
|
||||
}
|
||||
|
||||
// 过滤出指定父级ID的组织作为根节点
|
||||
List<Map<String, Object>> rootOrgs = parentToChildrenMap.getOrDefault(parentid, new ArrayList<>());
|
||||
|
||||
// 构建完整的树形结构
|
||||
buildTreeStructure(rootOrgs, parentToChildrenMap);
|
||||
|
||||
return rootOrgs;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 构建树形结构
|
||||
* @param orgList 当前层级的组织列表
|
||||
* @param parentToChildrenMap 父子关系映射
|
||||
*/
|
||||
private void buildTreeStructure(List<Map<String, Object>> orgList,
|
||||
Map<String, List<Map<String, Object>>> parentToChildrenMap) {
|
||||
for (Map<String, Object> orgMap : orgList) {
|
||||
String orgId = (String) orgMap.get("id");
|
||||
List<Map<String, Object>> children = parentToChildrenMap.getOrDefault(orgId, new ArrayList<>());
|
||||
if (!children.isEmpty()) {
|
||||
buildTreeStructure(children, parentToChildrenMap); // 递归构建子树
|
||||
}
|
||||
orgMap.put("childList", children);
|
||||
}
|
||||
return listMap;
|
||||
}
|
||||
|
||||
/***********************************
|
||||
@ -203,35 +234,42 @@ public class SysOrganizationServiceImpl extends ServiceImpl<SysOrganizationMappe
|
||||
@Override
|
||||
public List<SysOrganization> getOrganizationById(String id,
|
||||
String orgName) {
|
||||
|
||||
LambdaQueryWrapper<SysOrganization> queryWrapper =
|
||||
new LambdaQueryWrapper<>();
|
||||
SysUser userInfo = userService.getUserInfo();
|
||||
|
||||
// 收集所有允许的组织ID
|
||||
Set<String> allowedOrgIds = new HashSet<>();
|
||||
if (userInfo.getUsertype() != 0) {
|
||||
List<SysRole> roleByUserId =
|
||||
sysRoleMapper.getRoleByUserId(userInfo.getId());
|
||||
List<String> ids = new ArrayList<>();
|
||||
// 循环当前角色
|
||||
List<SysRole> roleByUserId = sysRoleMapper.getRoleByUserId(userInfo.getId());
|
||||
for (SysRole sysRole : roleByUserId) {
|
||||
// 获取角色的组织Id
|
||||
String orgscope = sysRole.getOrgscope();
|
||||
if (StrUtil.isBlank(orgscope)) {
|
||||
continue;
|
||||
}
|
||||
// 拆分组织Id
|
||||
// 拆分组织Id并添加到集合中(自动去重)
|
||||
String[] split = orgscope.split(",");
|
||||
List<String> stringList = Arrays.asList(split);
|
||||
ids.addAll(stringList);
|
||||
for (String orgId : split) {
|
||||
if (StrUtil.isNotBlank(orgId)) {
|
||||
allowedOrgIds.add(orgId.trim());
|
||||
}
|
||||
}
|
||||
}
|
||||
if (ObjectUtil.isNotEmpty(ids)) {
|
||||
queryWrapper.in(SysOrganization::getId, ids);
|
||||
}
|
||||
|
||||
}
|
||||
// 构建查询条件
|
||||
LambdaQueryWrapper<SysOrganization> queryWrapper = new LambdaQueryWrapper<>();
|
||||
queryWrapper.eq(SysOrganization::getParentid, id);
|
||||
|
||||
if (StrUtil.isNotBlank(orgName)) {
|
||||
queryWrapper.like(SysOrganization::getOrgname, orgName);
|
||||
}
|
||||
queryWrapper.eq(SysOrganization::getParentid, id).orderByDesc(SysOrganization::getOrgcode);
|
||||
|
||||
// 如果有权限限制,添加IN条件
|
||||
if (!allowedOrgIds.isEmpty()) {
|
||||
queryWrapper.in(SysOrganization::getId, allowedOrgIds);
|
||||
}
|
||||
|
||||
queryWrapper.orderByDesc(SysOrganization::getOrgcode);
|
||||
|
||||
return this.list(queryWrapper);
|
||||
}
|
||||
|
||||
|
||||
@ -10,7 +10,12 @@ import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
|
||||
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
|
||||
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
|
||||
import com.yfd.platform.config.ResponseResult;
|
||||
import com.yfd.platform.data.domain.SysUserDataScope;
|
||||
import com.yfd.platform.data.mapper.SysUserDataScopeMapper;
|
||||
import com.yfd.platform.env.domain.SdEngInfoBH;
|
||||
import com.yfd.platform.env.domain.SdHbrvDic;
|
||||
import com.yfd.platform.env.mapper.SdEngInfoBHMapper;
|
||||
import com.yfd.platform.env.mapper.SdHbrvDicMapper;
|
||||
import com.yfd.platform.system.domain.LoginUser;
|
||||
import com.yfd.platform.system.domain.SysRole;
|
||||
import com.yfd.platform.system.domain.SysUser;
|
||||
@ -56,6 +61,12 @@ public class UserServiceImpl extends ServiceImpl<SysUserMapper, SysUser> impleme
|
||||
|
||||
@Resource
|
||||
private PasswordEncoder passwordEncoder;
|
||||
|
||||
@Resource
|
||||
private SdHbrvDicMapper sdHbrvDicMapper;
|
||||
|
||||
@Resource
|
||||
private SdEngInfoBHMapper sdEngInfoBHMapper;
|
||||
/**
|
||||
* 文件空间配置
|
||||
*/
|
||||
@ -647,11 +658,113 @@ public class UserServiceImpl extends ServiceImpl<SysUserMapper, SysUser> impleme
|
||||
);
|
||||
queryWrapper.orderByDesc(SysUser::getRegTime);
|
||||
Page<SysUser> mapPage = this.page(page, queryWrapper);
|
||||
mapPage.getRecords().forEach(record -> {
|
||||
String id = record.getId();
|
||||
List<SysRole> sysRoles = sysRoleMapper.getRoleByUserId(id);
|
||||
record.setRoles(sysRoles);
|
||||
});
|
||||
|
||||
List<SysUser> records = mapPage.getRecords();
|
||||
if (records == null || records.isEmpty()) {
|
||||
return mapPage;
|
||||
}
|
||||
|
||||
List<String> userIds = records.stream()
|
||||
.map(SysUser::getId)
|
||||
.collect(Collectors.toList());
|
||||
|
||||
Map<String, List<SysRole>> userRoleMap = new HashMap<>();
|
||||
List<Map<String, Object>> userRoles = sysRoleMapper.getUserRolesByUserIds(userIds);
|
||||
if (userRoles != null) {
|
||||
for (Map<String, Object> row : userRoles) {
|
||||
String userId = (String) row.get("userId");
|
||||
SysRole role = new SysRole();
|
||||
role.setId((String) row.get("id"));
|
||||
role.setRolecode((String) row.get("rolecode"));
|
||||
role.setRolename((String) row.get("rolename"));
|
||||
role.setLevel((String) row.get("level"));
|
||||
role.setDescription((String) row.get("description"));
|
||||
role.setIsvaild((String) row.get("isvaild"));
|
||||
role.setOrgscope((String) row.get("orgscope"));
|
||||
role.setOptscope((String) row.get("optscope"));
|
||||
role.setBusscope((String) row.get("busscope"));
|
||||
userRoleMap.computeIfAbsent(userId, k -> new ArrayList<>()).add(role);
|
||||
}
|
||||
}
|
||||
|
||||
Map<String, List<SysUserDataScope>> userScopeMap = new HashMap<>();
|
||||
List<SysUserDataScope> allScopes = sysUserDataScopeMapper.selectValidPermissionsWithNameByUserIds(userIds);
|
||||
if (allScopes != null) {
|
||||
for (SysUserDataScope scope : allScopes) {
|
||||
userScopeMap.computeIfAbsent(scope.getUserId(), k -> new ArrayList<>()).add(scope);
|
||||
}
|
||||
}
|
||||
List<SdHbrvDic> allHbrvDicts = sdHbrvDicMapper.selectList(null);
|
||||
Map<String, String> basinCodeToNameMap = new HashMap<>();
|
||||
if (allHbrvDicts != null) {
|
||||
for (SdHbrvDic hbrv : allHbrvDicts) {
|
||||
if (hbrv.getHbrvcd() != null && hbrv.getHbrvnm() != null) {
|
||||
basinCodeToNameMap.put(hbrv.getHbrvcd(), hbrv.getHbrvnm());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
List<SdEngInfoBH> allStations = sdEngInfoBHMapper.selectList(null);
|
||||
Map<String, List<SdEngInfoBH>> basinToStationsMap = new HashMap<>();
|
||||
Map<String, String> stationCodeToNameMap = new HashMap<>();
|
||||
|
||||
if (allStations != null) {
|
||||
for (SdEngInfoBH station : allStations) {
|
||||
if (station.getStcd() != null && station.getEnnm() != null) {
|
||||
stationCodeToNameMap.put(station.getStcd(), station.getEnnm());
|
||||
}
|
||||
if (station.getHbrvcd() != null) {
|
||||
basinToStationsMap.computeIfAbsent(station.getHbrvcd(), k -> new ArrayList<>()).add(station);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
for (SysUser record : records) {
|
||||
String userId = record.getId();
|
||||
|
||||
List<SysRole> roles = userRoleMap.getOrDefault(userId, Collections.emptyList());
|
||||
record.setRoles(roles);
|
||||
|
||||
List<SysUserDataScope> scopes = userScopeMap.getOrDefault(userId, Collections.emptyList());
|
||||
Set<String> basinNameSet = new LinkedHashSet<>();
|
||||
Set<String> stationNameSet = new LinkedHashSet<>();
|
||||
|
||||
for (SysUserDataScope scope : scopes) {
|
||||
String orgType = scope.getOrgType();
|
||||
String orgId = scope.getOrgId();
|
||||
String orgName = scope.getOrgName();
|
||||
|
||||
if ("HBRVCD".equals(orgType)) {
|
||||
if (orgName != null) {
|
||||
basinNameSet.add(orgName);
|
||||
}
|
||||
if (orgId != null) {
|
||||
List<SdEngInfoBH> stations = basinToStationsMap.get(orgId);
|
||||
if (stations != null) {
|
||||
for (SdEngInfoBH station : stations) {
|
||||
if (station.getEnnm() != null) {
|
||||
stationNameSet.add(station.getEnnm());
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
} else if ("STATION".equals(orgType)) {
|
||||
if (orgName != null) {
|
||||
stationNameSet.add(orgName);
|
||||
}
|
||||
if (orgId != null) {
|
||||
String stationName = stationCodeToNameMap.get(orgId);
|
||||
if (stationName != null) {
|
||||
stationNameSet.add(stationName);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
record.setBasinNames(basinNameSet.isEmpty() ? null : String.join(",", basinNameSet));
|
||||
record.setStationNames(stationNameSet.isEmpty() ? null : String.join(",", stationNameSet));
|
||||
}
|
||||
|
||||
return mapPage;
|
||||
}
|
||||
|
||||
|
||||
@ -103,4 +103,36 @@
|
||||
ORDER BY s.CREATED_AT DESC
|
||||
</select>
|
||||
|
||||
<select id="selectValidPermissionsWithNameByUserIds" resultMap="BaseResultMap">
|
||||
SELECT
|
||||
s.ID,
|
||||
s.USER_ID,
|
||||
s.ORG_TYPE,
|
||||
s.ORG_ID,
|
||||
s.PARENT_ID,
|
||||
s.ORG_LEVEL,
|
||||
s.PATH,
|
||||
s.PERMISSION_TYPE,
|
||||
s.ROLE_CODE,
|
||||
s.DATA_SCOPE,
|
||||
s.STATUS,
|
||||
s.START_TIME,
|
||||
s.END_TIME,
|
||||
s.CREATED_AT,
|
||||
s.CREATED_BY,
|
||||
s.UPDATED_AT,
|
||||
s.UPDATED_BY,
|
||||
s.REMARK,
|
||||
COALESCE(e.ENNM, h.HBRVNM) AS orgName
|
||||
FROM SYS_USER_DATA_SCOPE s
|
||||
LEFT JOIN SD_ENGINFO_B_H e ON s.ORG_TYPE = 'STATION' AND s.ORG_ID = e.stcd
|
||||
LEFT JOIN SD_HBRV_DIC h ON s.ORG_TYPE = 'HBRVCD' AND s.ORG_ID = h.hbrvcd
|
||||
WHERE s.USER_ID IN
|
||||
<foreach collection="userIds" item="id" open="(" separator="," close=")">
|
||||
#{id}
|
||||
</foreach>
|
||||
AND s.STATUS = 1
|
||||
ORDER BY s.CREATED_AT DESC
|
||||
</select>
|
||||
|
||||
</mapper>
|
||||
|
||||
@ -143,6 +143,27 @@
|
||||
#{id}
|
||||
</foreach>
|
||||
</select>
|
||||
|
||||
<select id="getUserRolesByUserIds" resultType="java.util.HashMap">
|
||||
SELECT
|
||||
ur.USERID AS "userId",
|
||||
r.ID AS "id",
|
||||
r.ROLECODE AS "rolecode",
|
||||
r.ROLENAME AS "rolename",
|
||||
r."LEVEL" AS "level",
|
||||
r.DESCRIPTION AS "description",
|
||||
r.ISVAILD AS "isvaild",
|
||||
r.ORGSCOPE AS "orgscope",
|
||||
r.OPTSCOPE AS "optscope",
|
||||
r.BUSSCOPE AS "busscope"
|
||||
FROM
|
||||
SYS_ROLE r
|
||||
INNER JOIN SYS_ROLE_USERS ur ON r.ID = ur.ROLEID
|
||||
WHERE ur.USERID IN
|
||||
<foreach collection="userIds" item="id" open="(" separator="," close=")">
|
||||
#{id}
|
||||
</foreach>
|
||||
</select>
|
||||
<!--根据 角色id和用户id 删除系统角色用户对照 (admin除外)-->
|
||||
<delete id="deleteRoleUsers">
|
||||
delete from sys_role_users where userid !=(select u.id from sys_user u where u.account="admin") and roleid=#{roleid} and userid=#{urserid}
|
||||
|
||||
Loading…
Reference in New Issue
Block a user