@@ -1,7 +1,7 @@ | |||||
package com.ningdatech.pmapi.common.helper; | package com.ningdatech.pmapi.common.helper; | ||||
import com.ningdatech.pmapi.user.entity.UserInfo; | |||||
import com.ningdatech.pmapi.user.security.auth.model.UserFullInfoDTO; | import com.ningdatech.pmapi.user.security.auth.model.UserFullInfoDTO; | ||||
import org.springframework.stereotype.Component; | |||||
/** | /** | ||||
* @author liuxinxin | * @author liuxinxin | ||||
@@ -9,7 +9,6 @@ import org.springframework.stereotype.Component; | |||||
* @Description: 用户信息管理工具类 helper | * @Description: 用户信息管理工具类 helper | ||||
*/ | */ | ||||
@Component | |||||
public interface UserInfoHelper { | public interface UserInfoHelper { | ||||
/** | /** | ||||
@@ -22,6 +21,8 @@ public interface UserInfoHelper { | |||||
UserFullInfoDTO getUserFullInfo(Long userId); | UserFullInfoDTO getUserFullInfo(Long userId); | ||||
UserFullInfoDTO getUserFullInfo(UserInfo userInfo); | |||||
String getUserName(Long userId); | String getUserName(Long userId); | ||||
/** | /** | ||||
@@ -1,14 +1,30 @@ | |||||
package com.ningdatech.pmapi.common.helper.impl; | package com.ningdatech.pmapi.common.helper.impl; | ||||
import cn.hutool.core.collection.CollUtil; | import cn.hutool.core.collection.CollUtil; | ||||
import cn.hutool.core.collection.CollectionUtil; | |||||
import com.baomidou.mybatisplus.core.toolkit.StringUtils; | |||||
import com.baomidou.mybatisplus.core.toolkit.Wrappers; | |||||
import com.ningdatech.pmapi.common.helper.UserInfoHelper; | import com.ningdatech.pmapi.common.helper.UserInfoHelper; | ||||
import com.ningdatech.pmapi.organization.model.entity.DingEmployeeInfo; | |||||
import com.ningdatech.pmapi.organization.model.entity.DingOrganization; | |||||
import com.ningdatech.pmapi.organization.service.IDingEmployeeInfoService; | |||||
import com.ningdatech.pmapi.organization.service.IDingOrganizationService; | |||||
import com.ningdatech.pmapi.sys.mapper.RoleMapper; | |||||
import com.ningdatech.pmapi.sys.model.entity.Role; | import com.ningdatech.pmapi.sys.model.entity.Role; | ||||
import com.ningdatech.pmapi.sys.model.entity.UserRole; | |||||
import com.ningdatech.pmapi.sys.service.IUserRoleService; | |||||
import com.ningdatech.pmapi.user.entity.UserInfo; | |||||
import com.ningdatech.pmapi.user.entity.enumeration.RoleEnum; | import com.ningdatech.pmapi.user.entity.enumeration.RoleEnum; | ||||
import com.ningdatech.pmapi.user.manage.UserAuthLoginManage; | |||||
import com.ningdatech.pmapi.user.security.auth.model.UserFullInfoDTO; | import com.ningdatech.pmapi.user.security.auth.model.UserFullInfoDTO; | ||||
import com.ningdatech.pmapi.user.service.IUserInfoService; | |||||
import lombok.RequiredArgsConstructor; | import lombok.RequiredArgsConstructor; | ||||
import org.springframework.stereotype.Component; | import org.springframework.stereotype.Component; | ||||
import java.util.ArrayList; | |||||
import java.util.List; | |||||
import java.util.Objects; | |||||
import java.util.stream.Collectors; | |||||
/** | /** | ||||
* @author liuxinxin | * @author liuxinxin | ||||
* @date 2023/2/10 下午4:40 | * @date 2023/2/10 下午4:40 | ||||
@@ -18,36 +34,106 @@ import org.springframework.stereotype.Component; | |||||
public class UserInfoHelperImpl implements UserInfoHelper { | public class UserInfoHelperImpl implements UserInfoHelper { | ||||
private final UserAuthLoginManage userAuthLoginManage; | |||||
private final IUserInfoService userInfoService; | |||||
private final IDingEmployeeInfoService iDingEmployeeInfoService; | |||||
private final IDingOrganizationService iDingOrganizationService; | |||||
private final IUserRoleService iUserRoleService; | |||||
private final RoleMapper roleMapper; | |||||
@Override | @Override | ||||
public String getOrganizationCode(Long userId) { | public String getOrganizationCode(Long userId) { | ||||
UserFullInfoDTO userFullInfo = userAuthLoginManage.getUserFullInfo(userId); | |||||
UserFullInfoDTO userFullInfo = getUserFullInfo(userId); | |||||
return userFullInfo.getOrganizationCode(); | return userFullInfo.getOrganizationCode(); | ||||
} | } | ||||
@Override | @Override | ||||
public UserFullInfoDTO getUserFullInfo(Long userId) { | public UserFullInfoDTO getUserFullInfo(Long userId) { | ||||
UserFullInfoDTO userFullInfo = userAuthLoginManage.getUserFullInfo(userId); | |||||
UserInfo userInfo = userInfoService.getById(userId); | |||||
if (Objects.isNull(userInfo)) { | |||||
return null; | |||||
} | |||||
// 返回用户全量信息 | |||||
return getUserFullInfo(userInfo); | |||||
} | |||||
@Override | |||||
public UserFullInfoDTO getUserFullInfo(UserInfo userInfo) { | |||||
UserFullInfoDTO userFullInfo = new UserFullInfoDTO(); | |||||
// 获取浙政钉雇员信息 组织信息 | |||||
String employeeCode = userInfo.getEmployeeCode(); | |||||
if (StringUtils.isNotBlank(employeeCode)) { | |||||
List<DingEmployeeInfo> dingEmployeeInfoList = iDingEmployeeInfoService | |||||
.list(Wrappers.lambdaQuery(DingEmployeeInfo.class) | |||||
.eq(DingEmployeeInfo::getEmployeeCode, employeeCode) | |||||
.eq(DingEmployeeInfo::getMainJob, "true")); | |||||
DingEmployeeInfo dingEmployeeInfo = dingEmployeeInfoList.get(0); | |||||
// 装配用户任职所在单位 | |||||
String empPosUnitCode = dingEmployeeInfo.getEmpPosUnitCode(); | |||||
if (StringUtils.isNotBlank(empPosUnitCode)) { | |||||
DingOrganization dingOrganization = iDingOrganizationService.getByOrgCode(empPosUnitCode); | |||||
if (Objects.nonNull(dingOrganization)) { | |||||
userFullInfo.setEmpPosUnitCode(empPosUnitCode); | |||||
userFullInfo.setEmpPosUnitName(dingOrganization.getOrganizationName()); | |||||
} | |||||
} | |||||
// 装配用户所在orgCode | |||||
String organizationCode = dingEmployeeInfo.getOrganizationCode(); | |||||
List<DingOrganization> dingOrganizationList = iDingOrganizationService.list(Wrappers | |||||
.lambdaQuery(DingOrganization.class) | |||||
.eq(DingOrganization::getOrganizationCode, organizationCode)); | |||||
if (CollectionUtil.isNotEmpty(dingOrganizationList)) { | |||||
DingOrganization dingOrganization = dingOrganizationList.get(0); | |||||
userFullInfo.setOrganizationCode(dingOrganization.getOrganizationCode()); | |||||
userFullInfo.setOrganizationName(dingOrganization.getOrganizationName()); | |||||
userFullInfo.setRegionCode(dingOrganization.getDivisionCode()); | |||||
// 测试使用 | |||||
userFullInfo.setRegionLevel(3); | |||||
} | |||||
} | |||||
List<Role> roleList = new ArrayList<>(); | |||||
// 获取用户角色列表信息 | |||||
List<UserRole> userRoleList = iUserRoleService | |||||
.list(Wrappers.lambdaQuery(UserRole.class) | |||||
.eq(UserRole::getUserId, userInfo.getId())); | |||||
if (CollectionUtil.isNotEmpty(userRoleList)) { | |||||
List<Long> roleIdList = userRoleList.stream() | |||||
.map(UserRole::getRoleId).distinct() | |||||
.collect(Collectors.toList()); | |||||
roleList = roleMapper.selectBatchIds(roleIdList); | |||||
} | |||||
userFullInfo.setUserRoleList(roleList); | |||||
// 装配用户任职所在单位 | |||||
// 装配返回 | |||||
userFullInfo.setUserId(userInfo.getId()); | |||||
userFullInfo.setIdentifier(userInfo.getRealName()); | |||||
userFullInfo.setRealName(userInfo.getRealName()); | |||||
userFullInfo.setEmployeeCode(employeeCode); | |||||
userFullInfo.setUsername(userInfo.getRealName()); | |||||
userFullInfo.setMobile(userInfo.getMobile()); | |||||
userFullInfo.setAccountId(userInfo.getAccountId()); | |||||
return userFullInfo; | return userFullInfo; | ||||
} | } | ||||
@Override | @Override | ||||
public String getUserName(Long userId) { | public String getUserName(Long userId) { | ||||
UserFullInfoDTO userFullInfo = userAuthLoginManage.getUserFullInfo(userId); | |||||
String realName = userFullInfo.getRealName(); | |||||
return realName; | |||||
UserFullInfoDTO userFullInfo = getUserFullInfo(userId); | |||||
return userFullInfo.getRealName(); | |||||
} | } | ||||
@Override | @Override | ||||
public boolean isSuperOrRegionAdmin(Long userId) { | public boolean isSuperOrRegionAdmin(Long userId) { | ||||
UserFullInfoDTO userFullInfo = userAuthLoginManage.getUserFullInfo(userId); | |||||
if(CollUtil.isNotEmpty(userFullInfo.getUserRoleList())){ | |||||
for(Role role : userFullInfo.getUserRoleList()){ | |||||
if(RoleEnum.SUPER_ADMIN.name().equals(role.getCode()) || | |||||
RoleEnum.REGION_MANAGER.name().equals(role.getCode()) ){ | |||||
UserFullInfoDTO userFullInfo = getUserFullInfo(userId); | |||||
if (CollUtil.isNotEmpty(userFullInfo.getUserRoleList())) { | |||||
for (Role role : userFullInfo.getUserRoleList()) { | |||||
if (RoleEnum.SUPER_ADMIN.name().equals(role.getCode()) || | |||||
RoleEnum.REGION_MANAGER.name().equals(role.getCode())) { | |||||
return Boolean.TRUE; | return Boolean.TRUE; | ||||
} | } | ||||
} | } | ||||
@@ -57,13 +143,12 @@ public class UserInfoHelperImpl implements UserInfoHelper { | |||||
@Override | @Override | ||||
public UserFullInfoDTO getUserFullInfoByEmployeeCode(String employeeCode) { | public UserFullInfoDTO getUserFullInfoByEmployeeCode(String employeeCode) { | ||||
UserFullInfoDTO userFullInfo = userAuthLoginManage.getUserFullInfoByEmployeeCode(employeeCode); | |||||
return userFullInfo; | |||||
return getUserFullInfoByEmployeeCode(employeeCode); | |||||
} | } | ||||
@Override | @Override | ||||
public String getUserEmpPosUnitCode(Long userId) { | public String getUserEmpPosUnitCode(Long userId) { | ||||
UserFullInfoDTO userFullInfo = userAuthLoginManage.getUserFullInfo(userId); | |||||
UserFullInfoDTO userFullInfo = getUserFullInfo(userId); | |||||
return userFullInfo.getEmpPosUnitCode(); | return userFullInfo.getEmpPosUnitCode(); | ||||
} | } | ||||
} | } |
@@ -19,7 +19,7 @@ import com.ningdatech.pmapi.sys.model.dto.RoleUpdateDTO; | |||||
import com.ningdatech.pmapi.sys.model.entity.*; | import com.ningdatech.pmapi.sys.model.entity.*; | ||||
import com.ningdatech.pmapi.sys.service.*; | import com.ningdatech.pmapi.sys.service.*; | ||||
import com.ningdatech.pmapi.sys.utils.AuthCacheKeyUtils; | import com.ningdatech.pmapi.sys.utils.AuthCacheKeyUtils; | ||||
import com.ningdatech.pmapi.user.manage.UserAuthLoginManage; | |||||
import com.ningdatech.pmapi.user.manage.UserAuthManage; | |||||
import lombok.RequiredArgsConstructor; | import lombok.RequiredArgsConstructor; | ||||
import lombok.extern.slf4j.Slf4j; | import lombok.extern.slf4j.Slf4j; | ||||
import org.springframework.stereotype.Service; | import org.springframework.stereotype.Service; | ||||
@@ -48,7 +48,7 @@ public class RoleServiceImpl extends ServiceImpl<RoleMapper, Role> implements IR | |||||
private final CachePlusOps cachePlusOps; | private final CachePlusOps cachePlusOps; | ||||
private final IRoleMenuService roleMenuService; | private final IRoleMenuService roleMenuService; | ||||
private final IMenuService menuService; | private final IMenuService menuService; | ||||
private final UserAuthLoginManage userAuthLoginManage; | |||||
private final UserAuthManage userAuthManage; | |||||
/** | /** | ||||
* 删除角色时,需要级联删除跟角色相关的一切资源: | * 删除角色时,需要级联删除跟角色相关的一切资源: | ||||
@@ -161,7 +161,7 @@ public class RoleServiceImpl extends ServiceImpl<RoleMapper, Role> implements IR | |||||
List<Long> userIdList = userRoleService.listUserIdByRoleId(role.getId()); | List<Long> userIdList = userRoleService.listUserIdByRoleId(role.getId()); | ||||
userIdList.forEach(w -> { | userIdList.forEach(w -> { | ||||
cachePlusOps.del(AuthCacheKeyUtils.userResourceCacheKey(w)); | cachePlusOps.del(AuthCacheKeyUtils.userResourceCacheKey(w)); | ||||
userAuthLoginManage.refreshSession(w); | |||||
userAuthManage.refreshSession(w); | |||||
}); | }); | ||||
} | } | ||||
@@ -1,238 +0,0 @@ | |||||
package com.ningdatech.pmapi.user.manage; | |||||
import cn.hutool.core.collection.CollectionUtil; | |||||
import com.baomidou.mybatisplus.core.toolkit.StringUtils; | |||||
import com.baomidou.mybatisplus.core.toolkit.Wrappers; | |||||
import com.ningdatech.pmapi.common.util.StrUtils; | |||||
import com.ningdatech.pmapi.organization.model.entity.DingEmployeeInfo; | |||||
import com.ningdatech.pmapi.organization.model.entity.DingOrganization; | |||||
import com.ningdatech.pmapi.organization.service.IDingEmployeeInfoService; | |||||
import com.ningdatech.pmapi.organization.service.IDingOrganizationService; | |||||
import com.ningdatech.pmapi.sys.model.entity.Role; | |||||
import com.ningdatech.pmapi.sys.model.entity.UserRole; | |||||
import com.ningdatech.pmapi.sys.service.IRoleService; | |||||
import com.ningdatech.pmapi.sys.service.IUserRoleService; | |||||
import com.ningdatech.pmapi.user.convert.UserInfoConvertor; | |||||
import com.ningdatech.pmapi.user.entity.UserInfo; | |||||
import com.ningdatech.pmapi.user.security.auth.credential.CredentialAuthToken; | |||||
import com.ningdatech.pmapi.user.security.auth.model.UserFullInfoDTO; | |||||
import com.ningdatech.pmapi.user.security.auth.model.UserInfoDetails; | |||||
import com.ningdatech.pmapi.user.service.IUserInfoService; | |||||
import lombok.RequiredArgsConstructor; | |||||
import org.springframework.security.core.context.SecurityContextImpl; | |||||
import org.springframework.session.Session; | |||||
import org.springframework.session.SessionRepository; | |||||
import org.springframework.session.data.redis.RedisIndexedSessionRepository; | |||||
import org.springframework.stereotype.Component; | |||||
import java.util.ArrayList; | |||||
import java.util.List; | |||||
import java.util.Objects; | |||||
import java.util.stream.Collectors; | |||||
/** | |||||
* @author liuxinxin | |||||
* @date 2023/1/3 上午10:57 | |||||
*/ | |||||
@Component | |||||
@RequiredArgsConstructor | |||||
public class UserAuthLoginManage { | |||||
private final IUserInfoService iUserInfoService; | |||||
private final IRoleService iRoleService; | |||||
private final IDingOrganizationService iDingOrganizationService; | |||||
private final IDingEmployeeInfoService iDingEmployeeInfoService; | |||||
private final IUserRoleService iUserRoleService; | |||||
private final RedisIndexedSessionRepository redisIndexedSessionRepository; | |||||
/** | |||||
* 根据用户id 获取用户全量信息 | |||||
* | |||||
* @param userId / | |||||
* @return / | |||||
*/ | |||||
public UserFullInfoDTO getUserFullInfo(Long userId) { | |||||
UserInfo userInfo = iUserInfoService.getById(userId); | |||||
if (Objects.isNull(userInfo)) { | |||||
return null; | |||||
} | |||||
// 返回用户全量信息 | |||||
UserFullInfoDTO userFullInfoDTO = new UserFullInfoDTO(); | |||||
userFullInfoAssembler(userInfo, userFullInfoDTO); | |||||
return userFullInfoDTO; | |||||
} | |||||
/** | |||||
* 根据用户名获取 | |||||
* | |||||
* @param username / | |||||
* @return / | |||||
*/ | |||||
public UserFullInfoDTO queryUserInfoInPasswordAuth(String username) { | |||||
// TODO 目前账号密码登陆测试使用 强制用户登录 userId 1L | |||||
UserInfo userInfo = iUserInfoService.getById(username); | |||||
if (Objects.isNull(userInfo)) { | |||||
return null; | |||||
} | |||||
// 返回用户全量信息 | |||||
UserFullInfoDTO userFullInfoDTO = new UserFullInfoDTO(); | |||||
userFullInfoAssembler(userInfo, userFullInfoDTO); | |||||
return userFullInfoDTO; | |||||
} | |||||
/** | |||||
* 根据手机号获取 | |||||
* | |||||
* @param phoneNo / | |||||
* @return / | |||||
*/ | |||||
public UserFullInfoDTO queryUserInfoInPhoneNoAuth(String phoneNo) { | |||||
UserInfo userInfo = iUserInfoService.getOne(Wrappers.lambdaQuery(UserInfo.class) | |||||
.eq(UserInfo::getMobile, phoneNo)); | |||||
if (Objects.isNull(userInfo)) { | |||||
return null; | |||||
} | |||||
// 返回用户全量信息 | |||||
UserFullInfoDTO userFullInfoDTO = new UserFullInfoDTO(); | |||||
userFullInfoAssembler(userInfo, userFullInfoDTO); | |||||
return userFullInfoDTO; | |||||
} | |||||
/** | |||||
* 根据accountId(浙政钉扫码登陆) | |||||
* | |||||
* @param accountId / | |||||
* @return / | |||||
*/ | |||||
public UserFullInfoDTO queryUserInfoInAccountIdAuth(String accountId) { | |||||
// 获取用户信息 | |||||
UserInfo userInfo = iUserInfoService.getOne(Wrappers.lambdaQuery(UserInfo.class) | |||||
.eq(UserInfo::getAccountId, accountId)); | |||||
if (Objects.isNull(userInfo)) { | |||||
return null; | |||||
} | |||||
// 返回用户全量信息 | |||||
UserFullInfoDTO userFullInfoDTO = new UserFullInfoDTO(); | |||||
userFullInfoAssembler(userInfo, userFullInfoDTO); | |||||
return userFullInfoDTO; | |||||
} | |||||
private void userFullInfoAssembler(UserInfo userInfo, UserFullInfoDTO userFullInfoDTO) { | |||||
// 获取浙政钉雇员信息 组织信息 | |||||
String employeeCode = userInfo.getEmployeeCode(); | |||||
if (StringUtils.isNotBlank(employeeCode)) { | |||||
List<DingEmployeeInfo> dingEmployeeInfoList = iDingEmployeeInfoService | |||||
.list(Wrappers.lambdaQuery(DingEmployeeInfo.class) | |||||
.eq(DingEmployeeInfo::getEmployeeCode, employeeCode) | |||||
.eq(DingEmployeeInfo::getMainJob, "true")); | |||||
DingEmployeeInfo dingEmployeeInfo = dingEmployeeInfoList.get(0); | |||||
// 装配用户任职所在单位 | |||||
String empPosUnitCode = dingEmployeeInfo.getEmpPosUnitCode(); | |||||
if (StringUtils.isNotBlank(empPosUnitCode)) { | |||||
DingOrganization dingOrganization = iDingOrganizationService.getByOrgCode(empPosUnitCode); | |||||
if (Objects.nonNull(dingOrganization)) { | |||||
userFullInfoDTO.setEmpPosUnitCode(empPosUnitCode); | |||||
userFullInfoDTO.setEmpPosUnitName(dingOrganization.getOrganizationName()); | |||||
} | |||||
} | |||||
// 装配用户所在orgCode | |||||
String organizationCode = dingEmployeeInfo.getOrganizationCode(); | |||||
List<DingOrganization> dingOrganizationList = iDingOrganizationService.list(Wrappers | |||||
.lambdaQuery(DingOrganization.class) | |||||
.eq(DingOrganization::getOrganizationCode, organizationCode)); | |||||
if (CollectionUtil.isNotEmpty(dingOrganizationList)) { | |||||
DingOrganization dingOrganization = dingOrganizationList.get(0); | |||||
userFullInfoDTO.setOrganizationCode(dingOrganization.getOrganizationCode()); | |||||
userFullInfoDTO.setOrganizationName(dingOrganization.getOrganizationName()); | |||||
userFullInfoDTO.setRegionCode(dingOrganization.getDivisionCode()); | |||||
// 测试使用 | |||||
userFullInfoDTO.setRegionLevel(3); | |||||
} | |||||
} | |||||
List<Role> roleList = new ArrayList<>(); | |||||
// 获取用户角色列表信息 | |||||
List<UserRole> userRoleList = iUserRoleService | |||||
.list(Wrappers.lambdaQuery(UserRole.class) | |||||
.eq(UserRole::getUserId, userInfo.getId())); | |||||
if (CollectionUtil.isNotEmpty(userRoleList)) { | |||||
List<Long> roleIdList = userRoleList.stream() | |||||
.map(UserRole::getRoleId).distinct() | |||||
.collect(Collectors.toList()); | |||||
roleList = iRoleService.list(Wrappers.lambdaQuery(Role.class).in(Role::getId, roleIdList)); | |||||
} | |||||
userFullInfoDTO.setUserRoleList(roleList); | |||||
// 装配用户任职所在单位 | |||||
// 装配返回 | |||||
userFullInfoDTO.setUserId(userInfo.getId()); | |||||
userFullInfoDTO.setIdentifier(userInfo.getRealName()); | |||||
userFullInfoDTO.setRealName(userInfo.getRealName()); | |||||
userFullInfoDTO.setEmployeeCode(employeeCode); | |||||
userFullInfoDTO.setUsername(userInfo.getRealName()); | |||||
userFullInfoDTO.setMobile(userInfo.getMobile()); | |||||
userFullInfoDTO.setAccountId(userInfo.getAccountId()); | |||||
} | |||||
public UserFullInfoDTO getUserFullInfoByEmployeeCode(String employeeCode) { | |||||
UserInfo userInfo = iUserInfoService.getOne(Wrappers.lambdaQuery(UserInfo.class) | |||||
.eq(UserInfo::getEmployeeCode, employeeCode).last("limit 1")); | |||||
if (Objects.isNull(userInfo)) { | |||||
return null; | |||||
} | |||||
// 返回用户全量信息 | |||||
UserFullInfoDTO userFullInfoDTO = new UserFullInfoDTO(); | |||||
userFullInfoAssembler(userInfo, userFullInfoDTO); | |||||
return userFullInfoDTO; | |||||
} | |||||
@SuppressWarnings({"rawtypes", "unchecked"}) | |||||
public void refreshSession(Long userId) { | |||||
UserFullInfoDTO userFullInfo = getUserFullInfo(userId); | |||||
List<String> sessionIds = getSessionIds(userFullInfo); | |||||
if (sessionIds.isEmpty()) { | |||||
return; | |||||
} | |||||
SessionRepository redisSessionRepository = redisIndexedSessionRepository; | |||||
UserInfoDetails details = UserInfoConvertor.toUserInfoDetails(userFullInfo); | |||||
CredentialAuthToken cat = new CredentialAuthToken(details, details.getPassword(), details.getAuthorities()); | |||||
sessionIds.forEach(sessionId -> { | |||||
Session session = redisIndexedSessionRepository.findById(sessionId); | |||||
SecurityContextImpl context = session.getAttribute("SPRING_SECURITY_CONTEXT"); | |||||
context.setAuthentication(cat); | |||||
session.setAttribute("SPRING_SECURITY_CONTEXT", context); | |||||
redisSessionRepository.save(session); | |||||
}); | |||||
} | |||||
private List<String> getSessionIds(UserFullInfoDTO userFullInfo) { | |||||
List<String> sessionIds = new ArrayList<>(); | |||||
String identifier = userFullInfo.getIdentifier(); | |||||
if (StrUtils.isNotBlank(identifier)) { | |||||
sessionIds.addAll(redisIndexedSessionRepository.findByPrincipalName(identifier).keySet()); | |||||
} | |||||
String mobile = userFullInfo.getMobile(); | |||||
if (StrUtils.isNotBlank(mobile)) { | |||||
sessionIds.addAll(redisIndexedSessionRepository.findByPrincipalName(mobile).keySet()); | |||||
} | |||||
Long accountId = userFullInfo.getAccountId(); | |||||
if (accountId != null) { | |||||
sessionIds.addAll(redisIndexedSessionRepository.findByPrincipalName(accountId.toString()).keySet()); | |||||
} | |||||
return sessionIds; | |||||
} | |||||
public void kickOff(Long userId) { | |||||
UserFullInfoDTO userFullInfo = getUserFullInfo(userId); | |||||
List<String> sessionIds = getSessionIds(userFullInfo); | |||||
sessionIds.forEach(redisIndexedSessionRepository::deleteById); | |||||
} | |||||
} |
@@ -0,0 +1,76 @@ | |||||
package com.ningdatech.pmapi.user.manage; | |||||
import com.ningdatech.pmapi.common.helper.UserInfoHelper; | |||||
import com.ningdatech.pmapi.common.util.StrUtils; | |||||
import com.ningdatech.pmapi.user.convert.UserInfoConvertor; | |||||
import com.ningdatech.pmapi.user.security.auth.credential.CredentialAuthToken; | |||||
import com.ningdatech.pmapi.user.security.auth.model.UserFullInfoDTO; | |||||
import com.ningdatech.pmapi.user.security.auth.model.UserInfoDetails; | |||||
import lombok.AllArgsConstructor; | |||||
import org.springframework.security.core.context.SecurityContextImpl; | |||||
import org.springframework.session.Session; | |||||
import org.springframework.session.SessionRepository; | |||||
import org.springframework.session.data.redis.RedisIndexedSessionRepository; | |||||
import org.springframework.stereotype.Component; | |||||
import java.util.ArrayList; | |||||
import java.util.List; | |||||
/** | |||||
* <p> | |||||
* UserAuthHelper | |||||
* </p> | |||||
* | |||||
* @author WendyYang | |||||
* @since 2023/5/5 | |||||
**/ | |||||
@Component | |||||
@AllArgsConstructor | |||||
public class UserAuthManage { | |||||
private final UserInfoHelper userInfoHelper; | |||||
private final RedisIndexedSessionRepository redisSessionRepository; | |||||
@SuppressWarnings({"rawtypes", "unchecked"}) | |||||
public void refreshSession(Long userId) { | |||||
UserFullInfoDTO userFullInfo = userInfoHelper.getUserFullInfo(userId); | |||||
List<String> sessionIds = getSessionIds(userFullInfo); | |||||
if (sessionIds.isEmpty()) { | |||||
return; | |||||
} | |||||
SessionRepository sessionRepository = redisSessionRepository; | |||||
UserInfoDetails details = UserInfoConvertor.toUserInfoDetails(userFullInfo); | |||||
CredentialAuthToken cat = new CredentialAuthToken(details, details.getPassword(), details.getAuthorities()); | |||||
sessionIds.forEach(sessionId -> { | |||||
Session session = redisSessionRepository.findById(sessionId); | |||||
SecurityContextImpl context = session.getAttribute("SPRING_SECURITY_CONTEXT"); | |||||
context.setAuthentication(cat); | |||||
session.setAttribute("SPRING_SECURITY_CONTEXT", context); | |||||
sessionRepository.save(session); | |||||
}); | |||||
} | |||||
private List<String> getSessionIds(UserFullInfoDTO userFullInfo) { | |||||
List<String> sessionIds = new ArrayList<>(); | |||||
String identifier = userFullInfo.getIdentifier(); | |||||
if (StrUtils.isNotBlank(identifier)) { | |||||
sessionIds.addAll(redisSessionRepository.findByPrincipalName(identifier).keySet()); | |||||
} | |||||
String mobile = userFullInfo.getMobile(); | |||||
if (StrUtils.isNotBlank(mobile)) { | |||||
sessionIds.addAll(redisSessionRepository.findByPrincipalName(mobile).keySet()); | |||||
} | |||||
Long accountId = userFullInfo.getAccountId(); | |||||
if (accountId != null) { | |||||
sessionIds.addAll(redisSessionRepository.findByPrincipalName(accountId.toString()).keySet()); | |||||
} | |||||
return sessionIds; | |||||
} | |||||
public void kickOff(Long userId) { | |||||
UserFullInfoDTO userFullInfo = userInfoHelper.getUserFullInfo(userId); | |||||
List<String> sessionIds = getSessionIds(userFullInfo); | |||||
sessionIds.forEach(redisSessionRepository::deleteById); | |||||
} | |||||
} |
@@ -1,6 +1,7 @@ | |||||
package com.ningdatech.pmapi.user.manage; | package com.ningdatech.pmapi.user.manage; | ||||
import cn.hutool.core.collection.CollUtil; | import cn.hutool.core.collection.CollUtil; | ||||
import cn.hutool.core.collection.CollectionUtil; | |||||
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper; | import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper; | ||||
import com.baomidou.mybatisplus.core.toolkit.StringUtils; | import com.baomidou.mybatisplus.core.toolkit.StringUtils; | ||||
import com.baomidou.mybatisplus.core.toolkit.Wrappers; | import com.baomidou.mybatisplus.core.toolkit.Wrappers; | ||||
@@ -48,14 +49,14 @@ import java.util.stream.Collectors; | |||||
@RequiredArgsConstructor | @RequiredArgsConstructor | ||||
public class UserInfoManage { | public class UserInfoManage { | ||||
private final UserAuthManage userAuthManage; | |||||
private final IUserInfoService iUserInfoService; | |||||
private final IRoleService iRoleService; | |||||
private final IDingOrganizationService iDingOrganizationService; | private final IDingOrganizationService iDingOrganizationService; | ||||
private final IDingEmployeeInfoService iDingEmployeeInfoService; | private final IDingEmployeeInfoService iDingEmployeeInfoService; | ||||
private final IUserInfoService iUserInfoService; | |||||
private final IUserRoleService iUserRoleService; | private final IUserRoleService iUserRoleService; | ||||
private final IRoleService iRoleService; | |||||
private final UserInfoHelper userInfoHelper; | private final UserInfoHelper userInfoHelper; | ||||
private final RegionCacheHelper regionCacheHelper; | private final RegionCacheHelper regionCacheHelper; | ||||
private final UserAuthLoginManage userAuthLoginManage; | |||||
public PageVo<ResUserInfoListVO> list(ReqUserInfoListPO req) { | public PageVo<ResUserInfoListVO> list(ReqUserInfoListPO req) { | ||||
PageVo<ResUserInfoListVO> pageVo = new PageVo<>(); | PageVo<ResUserInfoListVO> pageVo = new PageVo<>(); | ||||
@@ -261,7 +262,7 @@ public class UserInfoManage { | |||||
userInfo.setAvailable(reqUserDisableOrEnablePO.getOperation()); | userInfo.setAvailable(reqUserDisableOrEnablePO.getOperation()); | ||||
iUserInfoService.updateById(userInfo); | iUserInfoService.updateById(userInfo); | ||||
if (userInfo.getAvailable().equals("DISABLE")) { | if (userInfo.getAvailable().equals("DISABLE")) { | ||||
userAuthLoginManage.kickOff(userId); | |||||
userAuthManage.kickOff(userId); | |||||
} | } | ||||
} | } | ||||
@@ -337,7 +338,7 @@ public class UserInfoManage { | |||||
iUserRoleService.saveBatch(userRoleList); | iUserRoleService.saveBatch(userRoleList); | ||||
} | } | ||||
// 刷新用户权限 | // 刷新用户权限 | ||||
userAuthLoginManage.refreshSession(userId); | |||||
userAuthManage.refreshSession(userId); | |||||
} | } | ||||
/** | /** | ||||
@@ -536,4 +537,64 @@ public class UserInfoManage { | |||||
return controlledRoleVOList; | return controlledRoleVOList; | ||||
} | } | ||||
/** | |||||
* 根据用户名获取 | |||||
* | |||||
* @param username / | |||||
* @return / | |||||
*/ | |||||
public UserFullInfoDTO queryUserInfoInPasswordAuth(String username) { | |||||
// TODO 目前账号密码登陆测试使用 强制用户登录 userId 1L | |||||
UserInfo userInfo = iUserInfoService.getById(username); | |||||
if (Objects.isNull(userInfo)) { | |||||
return null; | |||||
} | |||||
// 返回用户全量信息 | |||||
return userInfoHelper.getUserFullInfo(userInfo); | |||||
} | |||||
/** | |||||
* 根据手机号获取 | |||||
* | |||||
* @param phoneNo / | |||||
* @return / | |||||
*/ | |||||
public UserFullInfoDTO queryUserInfoInPhoneNoAuth(String phoneNo) { | |||||
UserInfo userInfo = iUserInfoService.getOne(Wrappers.lambdaQuery(UserInfo.class) | |||||
.eq(UserInfo::getMobile, phoneNo)); | |||||
if (Objects.isNull(userInfo)) { | |||||
return null; | |||||
} | |||||
// 返回用户全量信息 | |||||
return userInfoHelper.getUserFullInfo(userInfo); | |||||
} | |||||
/** | |||||
* 根据accountId(浙政钉扫码登陆) | |||||
* | |||||
* @param accountId / | |||||
* @return / | |||||
*/ | |||||
public UserFullInfoDTO queryUserInfoInAccountIdAuth(String accountId) { | |||||
// 获取用户信息 | |||||
UserInfo userInfo = iUserInfoService.getOne(Wrappers.lambdaQuery(UserInfo.class) | |||||
.eq(UserInfo::getAccountId, accountId)); | |||||
if (Objects.isNull(userInfo)) { | |||||
return null; | |||||
} | |||||
// 返回用户全量信息 | |||||
return userInfoHelper.getUserFullInfo(userInfo); | |||||
} | |||||
public UserFullInfoDTO getUserFullInfoByEmployeeCode(String employeeCode) { | |||||
UserInfo userInfo = iUserInfoService.getOne(Wrappers.lambdaQuery(UserInfo.class) | |||||
.eq(UserInfo::getEmployeeCode, employeeCode).last("limit 1")); | |||||
if (Objects.isNull(userInfo)) { | |||||
return null; | |||||
} | |||||
// 返回用户全量信息 | |||||
return userInfoHelper.getUserFullInfo(userInfo); | |||||
} | |||||
} | } |
@@ -1,7 +1,7 @@ | |||||
package com.ningdatech.pmapi.user.security.auth.agent; | package com.ningdatech.pmapi.user.security.auth.agent; | ||||
import com.ningdatech.pmapi.user.manage.UserAuthLoginManage; | |||||
import com.ningdatech.pmapi.common.helper.UserInfoHelper; | |||||
import com.ningdatech.pmapi.user.security.auth.model.UserFullInfoDTO; | import com.ningdatech.pmapi.user.security.auth.model.UserFullInfoDTO; | ||||
import com.ningdatech.pmapi.user.security.auth.model.UserInfoDetails; | import com.ningdatech.pmapi.user.security.auth.model.UserInfoDetails; | ||||
import lombok.RequiredArgsConstructor; | import lombok.RequiredArgsConstructor; | ||||
@@ -20,14 +20,14 @@ import java.util.Objects; | |||||
@RequiredArgsConstructor | @RequiredArgsConstructor | ||||
public class AgentLoginUserDetailService implements UserDetailsService { | public class AgentLoginUserDetailService implements UserDetailsService { | ||||
private final UserAuthLoginManage userAuthLoginManage; | |||||
private final UserInfoHelper userInfoHelper; | |||||
@Override | @Override | ||||
public UserInfoDetails loadUserByUsername(String username) throws UsernameNotFoundException { | public UserInfoDetails loadUserByUsername(String username) throws UsernameNotFoundException { | ||||
final Long userId = Long.parseLong(username); | final Long userId = Long.parseLong(username); | ||||
UserFullInfoDTO userFullInfoDTO = userAuthLoginManage.getUserFullInfo(userId); | |||||
UserFullInfoDTO userFullInfoDTO = userInfoHelper.getUserFullInfo(userId); | |||||
if (Objects.isNull(userFullInfoDTO)) { | if (Objects.isNull(userFullInfoDTO)) { | ||||
throw new UsernameNotFoundException(String.format("%s user not exist", username)); | throw new UsernameNotFoundException(String.format("%s user not exist", username)); | ||||
@@ -1,7 +1,7 @@ | |||||
package com.ningdatech.pmapi.user.security.auth.common; | package com.ningdatech.pmapi.user.security.auth.common; | ||||
import com.ningdatech.pmapi.user.manage.UserAuthLoginManage; | |||||
import com.ningdatech.pmapi.user.manage.UserInfoManage; | |||||
import com.ningdatech.pmapi.user.security.auth.model.UserFullInfoDTO; | import com.ningdatech.pmapi.user.security.auth.model.UserFullInfoDTO; | ||||
import com.ningdatech.pmapi.user.security.auth.model.UserInfoDetails; | import com.ningdatech.pmapi.user.security.auth.model.UserInfoDetails; | ||||
import lombok.RequiredArgsConstructor; | import lombok.RequiredArgsConstructor; | ||||
@@ -20,12 +20,12 @@ import java.util.Objects; | |||||
@RequiredArgsConstructor | @RequiredArgsConstructor | ||||
public class CommonLoginUserDetailService implements UserDetailsService { | public class CommonLoginUserDetailService implements UserDetailsService { | ||||
private final UserAuthLoginManage userAuthLoginManage; | |||||
private final UserInfoManage userInfoManage; | |||||
@Override | @Override | ||||
public UserInfoDetails loadUserByUsername(String username) throws UsernameNotFoundException { | public UserInfoDetails loadUserByUsername(String username) throws UsernameNotFoundException { | ||||
final String employeeCode = username; | final String employeeCode = username; | ||||
UserFullInfoDTO userFullInfoDTO = userAuthLoginManage.getUserFullInfoByEmployeeCode(employeeCode); | |||||
UserFullInfoDTO userFullInfoDTO = userInfoManage.getUserFullInfoByEmployeeCode(employeeCode); | |||||
if (Objects.isNull(userFullInfoDTO)) { | if (Objects.isNull(userFullInfoDTO)) { | ||||
throw new UsernameNotFoundException(String.format("%s user not exist", username)); | throw new UsernameNotFoundException(String.format("%s user not exist", username)); | ||||
@@ -3,7 +3,7 @@ package com.ningdatech.pmapi.user.security.auth.credential; | |||||
import com.ningdatech.pmapi.user.constant.LoginTypeEnum; | import com.ningdatech.pmapi.user.constant.LoginTypeEnum; | ||||
import com.ningdatech.pmapi.user.convert.UserInfoConvertor; | import com.ningdatech.pmapi.user.convert.UserInfoConvertor; | ||||
import com.ningdatech.pmapi.user.manage.UserAuthLoginManage; | |||||
import com.ningdatech.pmapi.user.manage.UserInfoManage; | |||||
import com.ningdatech.pmapi.user.security.auth.constants.UserDeatilsServiceConstant; | import com.ningdatech.pmapi.user.security.auth.constants.UserDeatilsServiceConstant; | ||||
import com.ningdatech.pmapi.user.security.auth.model.UserFullInfoDTO; | import com.ningdatech.pmapi.user.security.auth.model.UserFullInfoDTO; | ||||
import com.ningdatech.pmapi.user.security.auth.model.UserInfoDetails; | import com.ningdatech.pmapi.user.security.auth.model.UserInfoDetails; | ||||
@@ -24,7 +24,7 @@ import java.util.Objects; | |||||
@RequiredArgsConstructor | @RequiredArgsConstructor | ||||
public class CredentialLoginUserDetailService implements UserDetailsService { | public class CredentialLoginUserDetailService implements UserDetailsService { | ||||
private final UserAuthLoginManage userAuthLoginManage; | |||||
private final UserInfoManage userInfoManage; | |||||
@Override | @Override | ||||
public UserInfoDetails loadUserByUsername(String username) throws UsernameNotFoundException { | public UserInfoDetails loadUserByUsername(String username) throws UsernameNotFoundException { | ||||
@@ -36,21 +36,21 @@ public class CredentialLoginUserDetailService implements UserDetailsService { | |||||
UserFullInfoDTO userFullInfo; | UserFullInfoDTO userFullInfo; | ||||
switch (loginTypeEnum) { | switch (loginTypeEnum) { | ||||
case PHONE_VERIFICATION_CODE_LOGIN: { | case PHONE_VERIFICATION_CODE_LOGIN: { | ||||
userFullInfo = userAuthLoginManage.queryUserInfoInPhoneNoAuth(username); | |||||
userFullInfo = userInfoManage.queryUserInfoInPhoneNoAuth(username); | |||||
if (Objects.isNull(userFullInfo)) { | if (Objects.isNull(userFullInfo)) { | ||||
throw new CommonLoginException("改手机号未绑定用户"); | throw new CommonLoginException("改手机号未绑定用户"); | ||||
} | } | ||||
} | } | ||||
break; | break; | ||||
case USERNAME_PASSWORD_LOGIN: { | case USERNAME_PASSWORD_LOGIN: { | ||||
userFullInfo = userAuthLoginManage.queryUserInfoInPasswordAuth(username); | |||||
userFullInfo = userInfoManage.queryUserInfoInPasswordAuth(username); | |||||
if (Objects.isNull(userFullInfo)) { | if (Objects.isNull(userFullInfo)) { | ||||
throw new UsernameNotFoundException(String.format("%s user not exist", username)); | throw new UsernameNotFoundException(String.format("%s user not exist", username)); | ||||
} | } | ||||
} | } | ||||
break; | break; | ||||
case DING_QR_LOGIN: { | case DING_QR_LOGIN: { | ||||
userFullInfo = userAuthLoginManage.queryUserInfoInAccountIdAuth(username); | |||||
userFullInfo = userInfoManage.queryUserInfoInAccountIdAuth(username); | |||||
if (Objects.isNull(userFullInfo)) { | if (Objects.isNull(userFullInfo)) { | ||||
throw new CommonLoginException("浙政钉账号无法登陆"); | throw new CommonLoginException("浙政钉账号无法登陆"); | ||||
} | } | ||||