|
|
@@ -3,6 +3,7 @@ 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; |
|
|
@@ -11,11 +12,17 @@ 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.service.IUserAuthService; |
|
|
|
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; |
|
|
@@ -32,19 +39,18 @@ import java.util.stream.Collectors; |
|
|
|
@RequiredArgsConstructor |
|
|
|
public class UserAuthLoginManage { |
|
|
|
|
|
|
|
private final IUserAuthService iUserAuthService; |
|
|
|
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 |
|
|
|
* @param userId / |
|
|
|
* @return / |
|
|
|
*/ |
|
|
|
public UserFullInfoDTO getUserFullInfo(Long userId) { |
|
|
|
UserInfo userInfo = iUserInfoService.getById(userId); |
|
|
@@ -53,15 +59,15 @@ public class UserAuthLoginManage { |
|
|
|
} |
|
|
|
// 返回用户全量信息 |
|
|
|
UserFullInfoDTO userFullInfoDTO = new UserFullInfoDTO(); |
|
|
|
userFullInfoDTOAssembler(userInfo, userFullInfoDTO); |
|
|
|
userFullInfoAssembler(userInfo, userFullInfoDTO); |
|
|
|
return userFullInfoDTO; |
|
|
|
} |
|
|
|
|
|
|
|
/** |
|
|
|
* 根据用户名获取 |
|
|
|
* |
|
|
|
* @param username |
|
|
|
* @return |
|
|
|
* @param username / |
|
|
|
* @return / |
|
|
|
*/ |
|
|
|
public UserFullInfoDTO queryUserInfoInPasswordAuth(String username) { |
|
|
|
// TODO 目前账号密码登陆测试使用 强制用户登录 userId 1L |
|
|
@@ -71,15 +77,15 @@ public class UserAuthLoginManage { |
|
|
|
} |
|
|
|
// 返回用户全量信息 |
|
|
|
UserFullInfoDTO userFullInfoDTO = new UserFullInfoDTO(); |
|
|
|
userFullInfoDTOAssembler(userInfo, userFullInfoDTO); |
|
|
|
userFullInfoAssembler(userInfo, userFullInfoDTO); |
|
|
|
return userFullInfoDTO; |
|
|
|
} |
|
|
|
|
|
|
|
/** |
|
|
|
* 根据手机号获取 |
|
|
|
* |
|
|
|
* @param phoneNo |
|
|
|
* @return |
|
|
|
* @param phoneNo / |
|
|
|
* @return / |
|
|
|
*/ |
|
|
|
public UserFullInfoDTO queryUserInfoInPhoneNoAuth(String phoneNo) { |
|
|
|
UserInfo userInfo = iUserInfoService.getOne(Wrappers.lambdaQuery(UserInfo.class) |
|
|
@@ -89,15 +95,15 @@ public class UserAuthLoginManage { |
|
|
|
} |
|
|
|
// 返回用户全量信息 |
|
|
|
UserFullInfoDTO userFullInfoDTO = new UserFullInfoDTO(); |
|
|
|
userFullInfoDTOAssembler(userInfo, userFullInfoDTO); |
|
|
|
userFullInfoAssembler(userInfo, userFullInfoDTO); |
|
|
|
return userFullInfoDTO; |
|
|
|
} |
|
|
|
|
|
|
|
/** |
|
|
|
* 根据accountId(浙政钉扫码登陆) |
|
|
|
* |
|
|
|
* @param accountId |
|
|
|
* @return |
|
|
|
* @param accountId / |
|
|
|
* @return / |
|
|
|
*/ |
|
|
|
public UserFullInfoDTO queryUserInfoInAccountIdAuth(String accountId) { |
|
|
|
|
|
|
@@ -109,12 +115,12 @@ public class UserAuthLoginManage { |
|
|
|
} |
|
|
|
// 返回用户全量信息 |
|
|
|
UserFullInfoDTO userFullInfoDTO = new UserFullInfoDTO(); |
|
|
|
userFullInfoDTOAssembler(userInfo, userFullInfoDTO); |
|
|
|
userFullInfoAssembler(userInfo, userFullInfoDTO); |
|
|
|
return userFullInfoDTO; |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
private void userFullInfoDTOAssembler(UserInfo userInfo, UserFullInfoDTO userFullInfoDTO) { |
|
|
|
private void userFullInfoAssembler(UserInfo userInfo, UserFullInfoDTO userFullInfoDTO) { |
|
|
|
// 获取浙政钉雇员信息 组织信息 |
|
|
|
String employeeCode = userInfo.getEmployeeCode(); |
|
|
|
if (StringUtils.isNotBlank(employeeCode)) { |
|
|
@@ -128,9 +134,7 @@ public class UserAuthLoginManage { |
|
|
|
// 装配用户任职所在单位 |
|
|
|
String empPosUnitCode = dingEmployeeInfo.getEmpPosUnitCode(); |
|
|
|
if (StringUtils.isNotBlank(empPosUnitCode)) { |
|
|
|
DingOrganization dingOrganization = iDingOrganizationService |
|
|
|
.getOne(Wrappers.lambdaQuery(DingOrganization.class) |
|
|
|
.eq(DingOrganization::getOrganizationCode, empPosUnitCode)); |
|
|
|
DingOrganization dingOrganization = iDingOrganizationService.getByOrgCode(empPosUnitCode); |
|
|
|
if (Objects.nonNull(dingOrganization)) { |
|
|
|
userFullInfoDTO.setEmpPosUnitCode(empPosUnitCode); |
|
|
|
userFullInfoDTO.setEmpPosUnitName(dingOrganization.getOrganizationName()); |
|
|
@@ -139,9 +143,9 @@ public class UserAuthLoginManage { |
|
|
|
|
|
|
|
// 装配用户所在orgCode |
|
|
|
String organizationCode = dingEmployeeInfo.getOrganizationCode(); |
|
|
|
List<DingOrganization> dingOrganizationList = iDingOrganizationService |
|
|
|
.list(Wrappers.lambdaQuery(DingOrganization.class) |
|
|
|
.eq(DingOrganization::getOrganizationCode, organizationCode)); |
|
|
|
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()); |
|
|
@@ -174,7 +178,7 @@ public class UserAuthLoginManage { |
|
|
|
userFullInfoDTO.setEmployeeCode(employeeCode); |
|
|
|
userFullInfoDTO.setUsername(userInfo.getRealName()); |
|
|
|
userFullInfoDTO.setMobile(userInfo.getMobile()); |
|
|
|
|
|
|
|
userFullInfoDTO.setAccountId(userInfo.getAccountId()); |
|
|
|
} |
|
|
|
|
|
|
|
public UserFullInfoDTO getUserFullInfoByEmployeeCode(String employeeCode) { |
|
|
@@ -185,7 +189,50 @@ public class UserAuthLoginManage { |
|
|
|
} |
|
|
|
// 返回用户全量信息 |
|
|
|
UserFullInfoDTO userFullInfoDTO = new UserFullInfoDTO(); |
|
|
|
userFullInfoDTOAssembler(userInfo, 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); |
|
|
|
} |
|
|
|
|
|
|
|
} |