【更新】优化登录逻辑,减少查询次数

pull/87/head
徐玉祥 2023-03-05 15:11:48 +08:00
parent 755e70e2c5
commit 567127f2b2
10 changed files with 126 additions and 163 deletions

View File

@ -83,36 +83,36 @@ public interface SaBaseLoginUserApi {
List<JSONObject> listUserByUserIdList(List<String> userIdList); List<JSONObject> listUserByUserIdList(List<String> userIdList);
/** /**
* id * id
* *
* @author xuyuxiang * @author xuyuxiang
* @date 2022/4/27 22:53 * @date 2022/4/27 22:53
*/ */
List<String> getRoleCodeListByUserId(String userId); List<JSONObject> getRoleListByUserId(String userId);
/** /**
* id * idid
* *
* @author xuyuxiang * @author xuyuxiang
* @date 2022/4/27 22:54 * @date 2022/4/27 22:54
*/ */
List<String> getButtonCodeListListByUserId(String userId); List<String> getButtonCodeListListByUserAndRoleIdList(List<String> userAndRoleIdList);
/** /**
* id * idid
* *
* @author xuyuxiang * @author xuyuxiang
* @date 2022/4/27 22:54 * @date 2022/4/27 22:54
*/ */
List<String> getMobileButtonCodeListListByUserId(String userId); List<String> getMobileButtonCodeListListByUserIdAndRoleIdList(List<String> userAndRoleIdList);
/** /**
* id * idid
* *
* @author xuyuxiang * @author xuyuxiang
* @date 2022/4/27 22:54 * @date 2022/4/27 22:54
*/ */
List<JSONObject> getPermissionListByUserId(String userId, String orgId); List<JSONObject> getPermissionListByUserIdAndRoleIdList(List<String> userAndRoleIdList, String orgId);
/** /**
* ip * ip

View File

@ -23,9 +23,8 @@ import java.util.List;
public interface MobileButtonApi { public interface MobileButtonApi {
/** /**
* * id
* *
* @param idList
* @author * @author
* @date 2023/2/5 13:26 * @date 2023/2/5 13:26
**/ **/

View File

@ -49,9 +49,9 @@ public class AuthListener implements SaTokenListener {
if(SaClientTypeEnum.B.getValue().equals(loginType)) { if(SaClientTypeEnum.B.getValue().equals(loginType)) {
loginUserApi.updateUserLoginInfo(Convert.toStr(loginId), loginModel.getDevice()); loginUserApi.updateUserLoginInfo(Convert.toStr(loginId), loginModel.getDevice());
// 记录B端登录日志 // 记录B端登录日志
SaBaseLoginUser saBaseLoginUser = loginUserApi.getUserById(Convert.toStr(loginId)); Object name = loginModel.getExtra("name");
if(ObjectUtil.isNotEmpty(saBaseLoginUser)) { if(ObjectUtil.isNotEmpty(name)) {
devLogApi.executeLoginLog(saBaseLoginUser.getName()); devLogApi.executeLoginLog(Convert.toStr(name));
} else { } else {
devLogApi.executeLoginLog(null); devLogApi.executeLoginLog(null);
} }

View File

@ -16,10 +16,12 @@ import cn.dev33.satoken.stp.SaLoginModel;
import cn.dev33.satoken.stp.StpUtil; import cn.dev33.satoken.stp.StpUtil;
import cn.hutool.captcha.CaptchaUtil; import cn.hutool.captcha.CaptchaUtil;
import cn.hutool.captcha.CircleCaptcha; import cn.hutool.captcha.CircleCaptcha;
import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.convert.Convert; import cn.hutool.core.convert.Convert;
import cn.hutool.core.util.ObjectUtil; import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.PhoneUtil; import cn.hutool.core.util.PhoneUtil;
import cn.hutool.core.util.RandomUtil; import cn.hutool.core.util.RandomUtil;
import cn.hutool.json.JSONObject;
import com.baomidou.mybatisplus.core.toolkit.IdWorker; import com.baomidou.mybatisplus.core.toolkit.IdWorker;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
import vip.xiaonuo.auth.api.SaBaseLoginUserApi; import vip.xiaonuo.auth.api.SaBaseLoginUserApi;
@ -43,6 +45,7 @@ import vip.xiaonuo.dev.api.DevConfigApi;
import vip.xiaonuo.dev.api.DevSmsApi; import vip.xiaonuo.dev.api.DevSmsApi;
import javax.annotation.Resource; import javax.annotation.Resource;
import java.util.List;
import java.util.stream.Collectors; import java.util.stream.Collectors;
/** /**
@ -281,20 +284,27 @@ public class AuthServiceImpl implements AuthService {
throw new CommonException(AuthExceptionEnum.ACCOUNT_DISABLED.getValue()); throw new CommonException(AuthExceptionEnum.ACCOUNT_DISABLED.getValue());
} }
// 执行登录 // 执行登录
StpUtil.login(saBaseLoginUser.getId(), new SaLoginModel().setDevice(device)); StpUtil.login(saBaseLoginUser.getId(), new SaLoginModel().setDevice(device).setExtra("name", saBaseLoginUser.getName()));
// TODO 登录部份需要优化 // 角色集合
List<JSONObject> roleList = loginUserApi.getRoleListByUserId(saBaseLoginUser.getId());
// 角色id集合
List<String> roleIdList = roleList.stream().map(jsonObject -> jsonObject.getStr("id")).collect(Collectors.toList());
// 角色码集合
List<String> roleCodeList = roleList.stream().map(jsonObject -> jsonObject.getStr("code")).collect(Collectors.toList());
// 角色id和用户id集合
List<String> userAndRoleIdList = CollectionUtil.unionAll(roleIdList, CollectionUtil.newArrayList(saBaseLoginUser.getId()));
// 获取按钮码 // 获取按钮码
saBaseLoginUser.setButtonCodeList(loginUserApi.getButtonCodeListListByUserId(saBaseLoginUser.getId())); saBaseLoginUser.setButtonCodeList(loginUserApi.getButtonCodeListListByUserAndRoleIdList(userAndRoleIdList));
// 获取移动端按钮码 // 获取移动端按钮码
saBaseLoginUser.setMobileButtonCodeList(loginUserApi.getMobileButtonCodeListListByUserId(saBaseLoginUser.getId())); saBaseLoginUser.setMobileButtonCodeList(loginUserApi.getMobileButtonCodeListListByUserIdAndRoleIdList(userAndRoleIdList));
// 获取数据范围 // 获取数据范围
saBaseLoginUser.setDataScopeList(Convert.toList(SaBaseLoginUser.DataScope.class, saBaseLoginUser.setDataScopeList(Convert.toList(SaBaseLoginUser.DataScope.class,
loginUserApi.getPermissionListByUserId(saBaseLoginUser.getId(), saBaseLoginUser.getOrgId()))); loginUserApi.getPermissionListByUserIdAndRoleIdList(userAndRoleIdList, saBaseLoginUser.getOrgId())));
// 获取权限码 // 获取权限码
saBaseLoginUser.setPermissionCodeList(saBaseLoginUser.getDataScopeList().stream() saBaseLoginUser.setPermissionCodeList(saBaseLoginUser.getDataScopeList().stream()
.map(SaBaseLoginUser.DataScope::getApiUrl).collect(Collectors.toList())); .map(SaBaseLoginUser.DataScope::getApiUrl).collect(Collectors.toList()));
// 获取角色码 // 获取角色码
saBaseLoginUser.setRoleCodeList(loginUserApi.getRoleCodeListByUserId(saBaseLoginUser.getId())); saBaseLoginUser.setRoleCodeList(roleCodeList);
// 缓存用户信息此处使用TokenSession为了指定时间内无操作则自动下线 // 缓存用户信息此处使用TokenSession为了指定时间内无操作则自动下线
StpUtil.getTokenSession().set("loginUser", saBaseLoginUser); StpUtil.getTokenSession().set("loginUser", saBaseLoginUser);
// 返回token // 返回token
@ -313,19 +323,27 @@ public class AuthServiceImpl implements AuthService {
throw new CommonException(AuthExceptionEnum.ACCOUNT_DISABLED.getValue()); throw new CommonException(AuthExceptionEnum.ACCOUNT_DISABLED.getValue());
} }
// 执行登录 // 执行登录
StpClientUtil.login(saBaseClientLoginUser.getId(), new SaLoginModel().setDevice(device)); StpClientUtil.login(saBaseClientLoginUser.getId(), new SaLoginModel().setDevice(device).setExtra("name", saBaseClientLoginUser.getName()));
// 角色集合
List<JSONObject> roleList = loginUserApi.getRoleListByUserId(saBaseClientLoginUser.getId());
// 角色id集合
List<String> roleIdList = roleList.stream().map(jsonObject -> jsonObject.getStr("id")).collect(Collectors.toList());
// 角色码集合
List<String> roleCodeList = roleList.stream().map(jsonObject -> jsonObject.getStr("code")).collect(Collectors.toList());
// 角色id和用户id集合
List<String> userAndRoleIdList = CollectionUtil.unionAll(roleIdList, CollectionUtil.newArrayList(saBaseClientLoginUser.getId()));
// 获取按钮码 // 获取按钮码
saBaseClientLoginUser.setButtonCodeList(clientLoginUserApi.getButtonCodeListListByUserId(saBaseClientLoginUser.getId())); saBaseClientLoginUser.setButtonCodeList(clientLoginUserApi.getButtonCodeListListByUserAndRoleIdList(userAndRoleIdList));
// 获取移动端按钮码 // 获取移动端按钮码
saBaseClientLoginUser.setMobileButtonCodeList(clientLoginUserApi.getMobileButtonCodeListListByUserId(saBaseClientLoginUser.getId())); saBaseClientLoginUser.setMobileButtonCodeList(clientLoginUserApi.getMobileButtonCodeListListByUserIdAndRoleIdList(userAndRoleIdList));
// 获取数据范围 // 获取数据范围
saBaseClientLoginUser.setDataScopeList(Convert.toList(SaBaseClientLoginUser.DataScope.class, saBaseClientLoginUser.setDataScopeList(Convert.toList(SaBaseClientLoginUser.DataScope.class,
clientLoginUserApi.getPermissionListByUserId(saBaseClientLoginUser.getId(), null))); clientLoginUserApi.getPermissionListByUserIdAndRoleIdList(userAndRoleIdList, null)));
// 获取权限码 // 获取权限码
saBaseClientLoginUser.setPermissionCodeList(saBaseClientLoginUser.getDataScopeList().stream() saBaseClientLoginUser.setPermissionCodeList(saBaseClientLoginUser.getDataScopeList().stream()
.map(SaBaseClientLoginUser.DataScope::getApiUrl).collect(Collectors.toList())); .map(SaBaseClientLoginUser.DataScope::getApiUrl).collect(Collectors.toList()));
// 获取角色码 // 获取角色码
saBaseClientLoginUser.setRoleCodeList(clientLoginUserApi.getRoleCodeListByUserId(saBaseClientLoginUser.getId())); saBaseClientLoginUser.setRoleCodeList(roleCodeList);
// 缓存用户信息此处使用TokenSession为了指定时间内无操作则自动下线 // 缓存用户信息此处使用TokenSession为了指定时间内无操作则自动下线
StpClientUtil.getTokenSession().set("loginUser", saBaseClientLoginUser); StpClientUtil.getTokenSession().set("loginUser", saBaseClientLoginUser);
// 返回token // 返回token

View File

@ -116,49 +116,49 @@ public class ClientLoginUserApiProvider implements SaBaseLoginUserApi {
} }
/** /**
* id * id
* *
* @author xuyuxiang * @author xuyuxiang
* @date 2022/4/27 22:53 * @date 2022/4/27 22:53
*/ */
@Override @Override
public List<String> getRoleCodeListByUserId(String userId) { public List<JSONObject> getRoleListByUserId(String userId) {
// TODO C端用户暂无角色 // TODO C端用户暂无角色
return CollectionUtil.newArrayList(); return CollectionUtil.newArrayList();
} }
/** /**
* id * idid
* *
* @author xuyuxiang * @author xuyuxiang
* @date 2022/4/27 22:54 * @date 2022/4/27 22:54
*/ */
@Override @Override
public List<String> getButtonCodeListListByUserId(String userId) { public List<String> getButtonCodeListListByUserAndRoleIdList(List<String> userAndRoleIdList) {
// TODO C端用户暂无按钮码 // TODO C端用户暂无按钮码
return CollectionUtil.newArrayList(); return CollectionUtil.newArrayList();
} }
/** /**
* id * idid
* *
* @author xuyuxiang * @author xuyuxiang
* @date 2022/4/27 22:54 * @date 2022/4/27 22:54
*/ */
@Override @Override
public List<String> getMobileButtonCodeListListByUserId(String userId) { public List<String> getMobileButtonCodeListListByUserIdAndRoleIdList(List<String> userAndRoleIdList) {
// TODO C端用户暂无移动端按钮码 // TODO C端用户暂无移动端按钮码
return CollectionUtil.newArrayList(); return CollectionUtil.newArrayList();
} }
/** /**
* id * idid
* *
* @author xuyuxiang * @author xuyuxiang
* @date 2022/4/27 22:54 * @date 2022/4/27 22:54
*/ */
@Override @Override
public List<JSONObject> getPermissionListByUserId(String userId, String orgId) { public List<JSONObject> getPermissionListByUserIdAndRoleIdList(List<String> userAndRoleIdList, String orgId) {
// TODO C端用户暂无权限码 // TODO C端用户暂无权限码
return CollectionUtil.newArrayList(); return CollectionUtil.newArrayList();
} }

View File

@ -12,7 +12,6 @@
*/ */
package vip.xiaonuo.mobile.modular.resource.provider; package vip.xiaonuo.mobile.modular.resource.provider;
import cn.hutool.core.util.ObjectUtil;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
import vip.xiaonuo.mobile.api.MobileButtonApi; import vip.xiaonuo.mobile.api.MobileButtonApi;
import vip.xiaonuo.mobile.modular.resource.entity.MobileButton; import vip.xiaonuo.mobile.modular.resource.entity.MobileButton;
@ -36,9 +35,6 @@ public class MobileButtonApiProvider implements MobileButtonApi {
@Override @Override
public List<String> listByIds(List<String> buttonIdList) { public List<String> listByIds(List<String> buttonIdList) {
if (ObjectUtil.isEmpty(buttonIdList)) {
return null;
}
return mobileButtonService.listByIds(buttonIdList).stream().map(MobileButton::getCode).collect(Collectors.toList()); return mobileButtonService.listByIds(buttonIdList).stream().map(MobileButton::getCode).collect(Collectors.toList());
} }
} }

View File

@ -37,7 +37,7 @@ import java.util.Date;
**/ **/
@Getter @Getter
@Setter @Setter
@TableName(value = "SYS_USER") @TableName(value = "SYS_USER", autoResultMap = true)
public class SysUser extends CommonEntity implements TransPojo { public class SysUser extends CommonEntity implements TransPojo {
/** id */ /** id */

View File

@ -115,47 +115,47 @@ public class SysLoginUserApiProvider implements SaBaseLoginUserApi {
} }
/** /**
* id * id
*
* @author xuyuxiang
* @date 2022/4/27 22:54
*/
@Override
public List<String> getButtonCodeListListByUserId(String userId) {
return sysUserService.getButtonCodeList(userId);
}
/**
* id
*
* @author xuyuxiang
* @date 2022/4/27 22:54
*/
@Override
public List<String> getMobileButtonCodeListListByUserId(String userId) {
return sysUserService.getMobileButtonCodeListListByUserId(userId);
}
/**
* id
*
* @author xuyuxiang
* @date 2022/4/27 22:54
*/
@Override
public List<JSONObject> getPermissionListByUserId(String userId, String orgId) {
return sysUserService.getPermissionList(userId, orgId);
}
/**
* id
* *
* @author xuyuxiang * @author xuyuxiang
* @date 2022/4/27 22:53 * @date 2022/4/27 22:53
*/ */
@Override @Override
public List<String> getRoleCodeListByUserId(String userId) { public List<JSONObject> getRoleListByUserId(String userId) {
return sysUserService.getRoleCodeList(userId); return sysUserService.getRoleList(userId);
}
/**
* idid
*
* @author xuyuxiang
* @date 2022/4/27 22:54
*/
@Override
public List<String> getButtonCodeListListByUserAndRoleIdList(List<String> userAndRoleIdList) {
return sysUserService.getButtonCodeList(userAndRoleIdList);
}
/**
* idid
*
* @author xuyuxiang
* @date 2022/4/27 22:54
*/
@Override
public List<String> getMobileButtonCodeListListByUserIdAndRoleIdList(List<String> userAndRoleIdList) {
return sysUserService.getMobileButtonCodeList(userAndRoleIdList);
}
/**
* idid
*
* @author xuyuxiang
* @date 2022/4/27 22:54
*/
@Override
public List<JSONObject> getPermissionListByUserIdAndRoleIdList(List<String> userAndRoleIdList, String orgId) {
return sysUserService.getPermissionList(userAndRoleIdList, orgId);
} }
/** /**

View File

@ -308,13 +308,21 @@ public interface SysUserService extends IService<SysUser> {
*/ */
String loginWorkbench(SysUserIdParam sysUserIdParam); String loginWorkbench(SysUserIdParam sysUserIdParam);
/**
*
*
* @author xuyuxiang
* @date 2022/4/29 11:13
**/
List<JSONObject> getRoleList(String userId);
/** /**
* *
* *
* @author xuyuxiang * @author xuyuxiang
* @date 2022/4/29 11:13 * @date 2022/4/29 11:13
**/ **/
List<String> getButtonCodeList(String userId); List<String> getButtonCodeList(List<String> userAndRoleIdList);
/** /**
* *
@ -322,7 +330,7 @@ public interface SysUserService extends IService<SysUser> {
* @author xuyuxiang * @author xuyuxiang
* @date 2022/4/29 11:13 * @date 2022/4/29 11:13
**/ **/
List<String> getMobileButtonCodeListListByUserId(String userId); List<String> getMobileButtonCodeList(List<String> userAndRoleIdList);
/** /**
* *
@ -330,15 +338,7 @@ public interface SysUserService extends IService<SysUser> {
* @author xuyuxiang * @author xuyuxiang
* @date 2022/4/29 11:13 * @date 2022/4/29 11:13
**/ **/
List<JSONObject> getPermissionList(String userId, String orgId); List<JSONObject> getPermissionList(List<String> userAndRoleIdList, String orgId);
/**
*
*
* @author xuyuxiang
* @date 2022/4/29 11:13
**/
List<String> getRoleCodeList(String userId);
/** /**
* *

View File

@ -27,7 +27,6 @@ import cn.hutool.core.lang.tree.TreeNode;
import cn.hutool.core.lang.tree.TreeNodeConfig; import cn.hutool.core.lang.tree.TreeNodeConfig;
import cn.hutool.core.lang.tree.TreeUtil; import cn.hutool.core.lang.tree.TreeUtil;
import cn.hutool.core.lang.tree.parser.DefaultNodeParser; import cn.hutool.core.lang.tree.parser.DefaultNodeParser;
import cn.hutool.core.map.MapUtil;
import cn.hutool.core.util.ObjectUtil; import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.PhoneUtil; import cn.hutool.core.util.PhoneUtil;
import cn.hutool.core.util.RandomUtil; import cn.hutool.core.util.RandomUtil;
@ -873,16 +872,21 @@ public class SysUserServiceImpl extends ServiceImpl<SysUserMapper, SysUser> impl
} }
@Override @Override
public List<String> getButtonCodeList(String userId) { public List<JSONObject> getRoleList(String userId) {
List<String> buttonCodeListGrantUser = this.getButtonCodeListGrantUser(userId); List<String> roleIdList = sysRelationService.getRelationTargetIdListByObjectIdAndCategory(userId,
List<String> buttonCodeListGrantRole = this.getButtonCodeListGrantRole(userId); SysRelationCategoryEnum.SYS_USER_HAS_ROLE.getValue());
return CollectionUtil.newArrayList(CollectionUtil.unionDistinct(buttonCodeListGrantUser, buttonCodeListGrantRole)); if (ObjectUtil.isNotEmpty(roleIdList)) {
return sysRoleService.listByIds(roleIdList).stream().map(JSONUtil::parseObj).collect(Collectors.toList());
}
return CollectionUtil.newArrayList();
} }
public List<String> getButtonCodeListGrantUser(String userId) { @Override
public List<String> getButtonCodeList(List<String> userAndRoleIdList) {
List<String> buttonIdList = CollectionUtil.newArrayList(); List<String> buttonIdList = CollectionUtil.newArrayList();
sysRelationService.getRelationListByObjectIdAndCategory(userId, sysRelationService.list(new LambdaQueryWrapper<SysRelation>().in(SysRelation::getObjectId, userAndRoleIdList)
SysRelationCategoryEnum.SYS_USER_HAS_RESOURCE.getValue()).forEach(sysRelation -> { .in(SysRelation::getCategory, SysRelationCategoryEnum.SYS_USER_HAS_RESOURCE.getValue(),
SysRelationCategoryEnum.SYS_ROLE_HAS_RESOURCE.getValue())).forEach(sysRelation -> {
if (ObjectUtil.isNotEmpty(sysRelation.getExtJson())) { if (ObjectUtil.isNotEmpty(sysRelation.getExtJson())) {
buttonIdList.addAll(JSONUtil.parseObj(sysRelation.getExtJson()).getBeanList("buttonInfo", String.class)); buttonIdList.addAll(JSONUtil.parseObj(sysRelation.getExtJson()).getBeanList("buttonInfo", String.class));
} }
@ -893,72 +897,29 @@ public class SysUserServiceImpl extends ServiceImpl<SysUserMapper, SysUser> impl
return CollectionUtil.newArrayList(); return CollectionUtil.newArrayList();
} }
public List<String> getButtonCodeListGrantRole(String userId) {
List<String> roleIdList = sysRelationService.getRelationTargetIdListByObjectIdAndCategory(userId,
SysRelationCategoryEnum.SYS_USER_HAS_ROLE.getValue());
if (ObjectUtil.isNotEmpty(roleIdList)) {
List<String> buttonIdList = CollectionUtil.newArrayList();
sysRelationService.getRelationListByObjectIdListAndCategory(roleIdList,
SysRelationCategoryEnum.SYS_ROLE_HAS_RESOURCE.getValue()).forEach(sysRelation -> {
if (ObjectUtil.isNotEmpty(sysRelation.getExtJson())) {
buttonIdList.addAll(JSONUtil.parseObj(sysRelation.getExtJson()).getBeanList("buttonInfo", String.class));
}
});
if (ObjectUtil.isNotEmpty(buttonIdList)) {
return sysButtonService.listByIds(buttonIdList).stream().map(SysButton::getCode).collect(Collectors.toList());
}
}
return CollectionUtil.newArrayList();
}
@Override @Override
public List<String> getMobileButtonCodeListListByUserId(String userId) { public List<String> getMobileButtonCodeList(List<String> userAndRoleIdList) {
List<String> roleIdList = sysRelationService.getRelationTargetIdListByObjectIdAndCategory(userId, List<String> buttonIdList = CollectionUtil.newArrayList();
SysRelationCategoryEnum.SYS_USER_HAS_ROLE.getValue()); sysRelationService.getRelationListByObjectIdListAndCategory(userAndRoleIdList,
if (ObjectUtil.isNotEmpty(roleIdList)) { SysRelationCategoryEnum.SYS_ROLE_HAS_MOBILE_MENU.getValue()).forEach(sysRelation -> {
List<String> buttonIdList = CollectionUtil.newArrayList(); if (ObjectUtil.isNotEmpty(sysRelation.getExtJson())) {
sysRelationService.getRelationListByObjectIdListAndCategory(roleIdList, buttonIdList.addAll(JSONUtil.parseObj(sysRelation.getExtJson()).getBeanList("buttonInfo", String.class));
SysRelationCategoryEnum.SYS_ROLE_HAS_MOBILE_MENU.getValue()).forEach(sysRelation -> { }
if (ObjectUtil.isNotEmpty(sysRelation.getExtJson())) { });
buttonIdList.addAll(JSONUtil.parseObj(sysRelation.getExtJson()).getBeanList("buttonInfo", String.class)); if (ObjectUtil.isNotEmpty(buttonIdList)) {
}
});
return mobileButtonApi.listByIds(buttonIdList); return mobileButtonApi.listByIds(buttonIdList);
} }
return CollectionUtil.newArrayList(); return CollectionUtil.newArrayList();
} }
@Override @Override
public List<JSONObject> getPermissionList(String userId, String orgId) { public List<JSONObject> getPermissionList(List<String> userAndRoleIdList, String orgId) {
Map<String, List<SysRelation>> permissionListGrantUser = this.getPermissionListGrantUser(userId, orgId); Map<String, List<SysRelation>> map = sysRelationService.list(new LambdaQueryWrapper<SysRelation>()
Map<String, List<SysRelation>> permissionListGrantRole = this.getPermissionListGrantRole(userId, orgId); .in(SysRelation::getObjectId, userAndRoleIdList).in(SysRelation::getCategory,
permissionListGrantUser.forEach((key, value) -> permissionListGrantRole.merge(key, value, (prev, next) -> { SysRelationCategoryEnum.SYS_USER_HAS_PERMISSION.getValue(),
prev.addAll(next); SysRelationCategoryEnum.SYS_ROLE_HAS_PERMISSION.getValue())).stream()
return prev; .collect(Collectors.groupingBy(SysRelation::getTargetId));
})); return getScopeListByMap(map, orgId);
return getScopeListByMap(permissionListGrantRole, orgId);
}
public Map<String, List<SysRelation>> getPermissionListGrantUser(String userId, String orgId) {
if (ObjectUtil.isNotEmpty(orgId)) {
return sysRelationService.getRelationListByObjectIdAndCategory(userId,
SysRelationCategoryEnum.SYS_USER_HAS_PERMISSION.getValue()).stream()
.collect(Collectors.groupingBy(SysRelation::getTargetId));
}
return MapUtil.newHashMap();
}
public Map<String, List<SysRelation>> getPermissionListGrantRole(String userId, String orgId) {
if (ObjectUtil.isNotEmpty(orgId)) {
List<String> roleIdList = sysRelationService.getRelationTargetIdListByObjectIdAndCategory(userId,
SysRelationCategoryEnum.SYS_USER_HAS_ROLE.getValue());
if (ObjectUtil.isNotEmpty(roleIdList)) {
return sysRelationService.getRelationListByObjectIdListAndCategory(roleIdList,
SysRelationCategoryEnum.SYS_ROLE_HAS_PERMISSION.getValue()).stream()
.collect(Collectors.groupingBy(SysRelation::getTargetId));
}
}
return MapUtil.newHashMap();
} }
public List<JSONObject> getScopeListByMap(Map<String, List<SysRelation>> groupMap, String orgId) { public List<JSONObject> getScopeListByMap(Map<String, List<SysRelation>> groupMap, String orgId) {
@ -991,17 +952,6 @@ public class SysUserServiceImpl extends ServiceImpl<SysUserMapper, SysUser> impl
return resultList; return resultList;
} }
@Override
public List<String> getRoleCodeList(String userId) {
List<String> roleIdList = sysRelationService.getRelationTargetIdListByObjectIdAndCategory(userId,
SysRelationCategoryEnum.SYS_USER_HAS_ROLE.getValue());
if (ObjectUtil.isNotEmpty(roleIdList)) {
return sysRoleService.listByIds(roleIdList)
.stream().map(SysRole::getCode).collect(Collectors.toList());
}
return CollectionUtil.newArrayList();
}
@Override @Override
public void importUser(MultipartFile file) { public void importUser(MultipartFile file) {
// TODO 待完善 // TODO 待完善