|
@@ -9,20 +9,20 @@ import com.citu.framework.common.util.monitor.TracerUtils;
|
|
|
import com.citu.framework.common.util.servlet.ServletUtils;
|
|
|
import com.citu.framework.common.util.validation.ValidationUtils;
|
|
|
import com.citu.framework.security.core.LoginUser;
|
|
|
+import com.citu.module.menduner.common.CommonConstants;
|
|
|
import com.citu.module.menduner.common.util.LoginUserContext;
|
|
|
import com.citu.module.menduner.system.controller.app.common.auth.enterprise.AppEnterpriseAuthSwitchLoginReqVO;
|
|
|
import com.citu.module.menduner.system.controller.app.common.auth.enterprise.AppEnterpriseResetPasswordReqVO;
|
|
|
import com.citu.module.menduner.system.controller.app.common.auth.vo.AppMdeAuthLoginRespVO;
|
|
|
+import com.citu.module.menduner.system.controller.base.logger.LoginLogCreateReqDTO;
|
|
|
import com.citu.module.menduner.system.convert.MdeAuthConvert;
|
|
|
+import com.citu.module.menduner.system.dal.dataobject.enterprise.EnterpriseDO;
|
|
|
import com.citu.module.menduner.system.dal.dataobject.enterprise.EnterpriseUserBindDO;
|
|
|
-import com.citu.module.menduner.system.dal.dataobject.user.MdeUserDO;
|
|
|
-import com.citu.module.menduner.common.CommonConstants;
|
|
|
import com.citu.module.menduner.system.enums.MendunerStatusEnum;
|
|
|
+import com.citu.module.menduner.system.service.enterprise.EnterpriseService;
|
|
|
import com.citu.module.menduner.system.service.enterprise.bind.EnterpriseUserBindService;
|
|
|
import com.citu.module.menduner.system.service.enterprise.register.EnterpriseRegisterService;
|
|
|
-import com.citu.module.menduner.system.service.user.MdeUserService;
|
|
|
-import com.citu.module.system.api.logger.LoginLogApi;
|
|
|
-import com.citu.module.system.api.logger.dto.LoginLogCreateReqDTO;
|
|
|
+import com.citu.module.menduner.system.service.logger.LoginLogService;
|
|
|
import com.citu.module.system.api.mail.MailSendApi;
|
|
|
import com.citu.module.system.api.mail.dto.MailSendSingleToUserReqDTO;
|
|
|
import com.citu.module.system.api.oauth2.OAuth2TokenApi;
|
|
@@ -48,6 +48,7 @@ import java.util.concurrent.TimeUnit;
|
|
|
|
|
|
import static com.citu.framework.common.exception.util.ServiceExceptionUtil.exception;
|
|
|
import static com.citu.framework.common.util.servlet.ServletUtils.getClientIP;
|
|
|
+import static com.citu.module.menduner.system.controller.base.logger.LoginLogCreateReqDTO.TYPE_ENTERPRISE_USER;
|
|
|
import static com.citu.module.menduner.system.dal.redis.RedisKeyConstants.MDE_AUTH_ENTERPRISE_USER_PWD_LOCK;
|
|
|
import static com.citu.module.menduner.system.enums.ErrorCodeConstants.*;
|
|
|
|
|
@@ -60,11 +61,8 @@ import static com.citu.module.menduner.system.enums.ErrorCodeConstants.*;
|
|
|
@Validated
|
|
|
public class MdeEnterpriseAuthServiceImpl implements MdeEnterpriseAuthService {
|
|
|
|
|
|
-
|
|
|
- @Resource
|
|
|
- protected MdeUserService userService;
|
|
|
@Resource
|
|
|
- protected LoginLogApi loginLogApi;
|
|
|
+ protected LoginLogService loginLogService;
|
|
|
@Resource
|
|
|
protected OAuth2TokenApi oauth2TokenApi;
|
|
|
/**
|
|
@@ -79,6 +77,8 @@ public class MdeEnterpriseAuthServiceImpl implements MdeEnterpriseAuthService {
|
|
|
@Resource
|
|
|
private EnterpriseUserBindService userBindService;
|
|
|
@Resource
|
|
|
+ private EnterpriseService enterpriseService;
|
|
|
+ @Resource
|
|
|
private EnterpriseRegisterService registerService;
|
|
|
@Resource
|
|
|
private MailSendApi mailSendApi;
|
|
@@ -94,7 +94,6 @@ public class MdeEnterpriseAuthServiceImpl implements MdeEnterpriseAuthService {
|
|
|
// 创建 Token 令牌,记录登录日志
|
|
|
return createTokenAfterLoginSuccess(
|
|
|
bindUser,
|
|
|
- bindUser.getUserId(),
|
|
|
email,
|
|
|
LoginLogTypeEnum.LOGIN_EMAIL,
|
|
|
null,
|
|
@@ -105,23 +104,19 @@ public class MdeEnterpriseAuthServiceImpl implements MdeEnterpriseAuthService {
|
|
|
@Override
|
|
|
@DSTransactional
|
|
|
public AppMdeAuthLoginRespVO switchLogin(AppEnterpriseAuthSwitchLoginReqVO reqVO) {
|
|
|
- LoginUser loginUser = LoginUserContext.get();
|
|
|
+ Long userId = LoginUserContext.getUserId();
|
|
|
// 查询用户
|
|
|
- MdeUserDO user = userService.getMdeUser(loginUser.getId());
|
|
|
- EnterpriseUserBindDO bindUser = check2(reqVO.getEnterpriseId(), user.getId(), user.getPhone(),
|
|
|
+ EnterpriseUserBindDO bindUser = check2(reqVO.getEnterpriseId(), userId,
|
|
|
LoginLogTypeEnum.LOGIN_SWITCH);
|
|
|
|
|
|
// 创建 Token 令牌,记录登录日志
|
|
|
- AppMdeAuthLoginRespVO resp = createTokenAfterLoginSuccess(
|
|
|
+ return createTokenAfterLoginSuccess(
|
|
|
bindUser,
|
|
|
- user.getId(),
|
|
|
- user.getPhone(),
|
|
|
+ bindUser.getPhone(),
|
|
|
LoginLogTypeEnum.LOGIN_SWITCH,
|
|
|
null,
|
|
|
reqVO.getEnterpriseId()
|
|
|
);
|
|
|
-
|
|
|
- return resp;
|
|
|
}
|
|
|
|
|
|
protected EnterpriseUserBindDO check(String email, String password) {
|
|
@@ -138,7 +133,7 @@ public class MdeEnterpriseAuthServiceImpl implements MdeEnterpriseAuthService {
|
|
|
}
|
|
|
// 校验是否禁用
|
|
|
if (ObjectUtil.notEqual(user.getStatus(), MendunerStatusEnum.ENABLE.getStatus())) {
|
|
|
- createLoginLog(user.getId(), email, logTypeEnum, LoginResultEnum.USER_DISABLED);
|
|
|
+ createLoginLog(user, email, logTypeEnum, LoginResultEnum.USER_DISABLED);
|
|
|
throw exception(MDE_ENTERPRISE_USER_BIND_IS_DISABLE);
|
|
|
}
|
|
|
if (!userBindService.isPasswordMatch(password, user.getPassword())) {
|
|
@@ -164,10 +159,10 @@ public class MdeEnterpriseAuthServiceImpl implements MdeEnterpriseAuthService {
|
|
|
if (numInt >= 5) {
|
|
|
// 8个小时内输错5次 锁定
|
|
|
userBindService.disable(user.getId());
|
|
|
- createLoginLog(user.getId(), email, logTypeEnum, LoginResultEnum.USER_DISABLED);
|
|
|
+ createLoginLog(user, email, logTypeEnum, LoginResultEnum.USER_DISABLED);
|
|
|
throw exception(MDE_ENTERPRISE_USER_BIND_IS_DISABLE);
|
|
|
} else {
|
|
|
- createLoginLog(user.getId(), email, logTypeEnum, LoginResultEnum.BAD_CREDENTIALS);
|
|
|
+ createLoginLog(user, email, logTypeEnum, LoginResultEnum.BAD_CREDENTIALS);
|
|
|
throw exception(MDE_AUTH_LOGIN_BAD_CREDENTIALS);
|
|
|
}
|
|
|
}
|
|
@@ -180,7 +175,6 @@ public class MdeEnterpriseAuthServiceImpl implements MdeEnterpriseAuthService {
|
|
|
|
|
|
protected EnterpriseUserBindDO check2(Long enterpriseId,
|
|
|
Long userId,
|
|
|
- String phone,
|
|
|
LoginLogTypeEnum logTypeEnum) {
|
|
|
// 查询绑定用户
|
|
|
EnterpriseUserBindDO bind = userBindService.selectByEnterpriseIdAndUserId(
|
|
@@ -188,12 +182,12 @@ public class MdeEnterpriseAuthServiceImpl implements MdeEnterpriseAuthService {
|
|
|
userId
|
|
|
);
|
|
|
if (null == bind) {
|
|
|
- createLoginLog(bind.getId(), phone, logTypeEnum, LoginResultEnum.BAD_CREDENTIALS);
|
|
|
+ createLoginLog(null, null, logTypeEnum, LoginResultEnum.BAD_CREDENTIALS);
|
|
|
throw exception(MDE_USER_ENTERPRISE_NOT_USER_NULL);
|
|
|
}
|
|
|
// 校验是否禁用
|
|
|
if (ObjectUtil.notEqual(bind.getStatus(), MendunerStatusEnum.ENABLE.getStatus())) {
|
|
|
- createLoginLog(bind.getId(), phone, logTypeEnum, LoginResultEnum.USER_DISABLED);
|
|
|
+ createLoginLog(bind, bind.getPhone(), logTypeEnum, LoginResultEnum.USER_DISABLED);
|
|
|
throw exception(MDE_AUTH_LOGIN_USER_DISABLED);
|
|
|
}
|
|
|
return bind;
|
|
@@ -206,15 +200,14 @@ public class MdeEnterpriseAuthServiceImpl implements MdeEnterpriseAuthService {
|
|
|
|
|
|
|
|
|
protected AppMdeAuthLoginRespVO createTokenAfterLoginSuccess(EnterpriseUserBindDO bindUser,
|
|
|
- Long userId,
|
|
|
String account,
|
|
|
LoginLogTypeEnum logType,
|
|
|
String openid, Long enterpriseId) {
|
|
|
// 插入登陆日志
|
|
|
- createLoginLog(bindUser.getId(), account, logType, LoginResultEnum.SUCCESS);
|
|
|
+ createLoginLog(bindUser, account, logType, LoginResultEnum.SUCCESS);
|
|
|
// 创建 Token 令牌
|
|
|
OAuth2AccessTokenRespDTO accessTokenRespDTO = oauth2TokenApi.createAccessToken(new OAuth2AccessTokenCreateReqDTO()
|
|
|
- .setUserId(userId).setUserType(getUserType().getValue())
|
|
|
+ .setUserId(bindUser.getUserId()).setUserType(getUserType().getValue())
|
|
|
.setClientId(OAuth2ClientConstants.CLIENT_ID_MENDUNER_ADMIN)
|
|
|
.setUserInfo(MapUtil.builder(LoginUser.INFO_KEY_DATA_ID, enterpriseId.toString()).build())
|
|
|
).getCheckedData();
|
|
@@ -222,49 +215,73 @@ public class MdeEnterpriseAuthServiceImpl implements MdeEnterpriseAuthService {
|
|
|
return MdeAuthConvert.INSTANCE.convert(accessTokenRespDTO, openid);
|
|
|
}
|
|
|
|
|
|
- protected void createLoginLog(Long bindUserId, String account, LoginLogTypeEnum logType, LoginResultEnum loginResult) {
|
|
|
+ protected void createLoginLog(EnterpriseUserBindDO bindUser,
|
|
|
+ String account,
|
|
|
+ LoginLogTypeEnum logType,
|
|
|
+ LoginResultEnum loginResult) {
|
|
|
// 插入登录日志
|
|
|
LoginLogCreateReqDTO reqDTO = new LoginLogCreateReqDTO();
|
|
|
reqDTO.setLogType(logType.getType());
|
|
|
reqDTO.setTraceId(TracerUtils.getTraceId());
|
|
|
- reqDTO.setUserId(bindUserId);
|
|
|
- reqDTO.setUserType(getUserType().getValue());
|
|
|
+ reqDTO.setUserId(null != bindUser ? bindUser.getUserId() : null);
|
|
|
+ reqDTO.setEnterpriseId(null != bindUser ? bindUser.getEnterpriseId() : null);
|
|
|
+ reqDTO.setType(TYPE_ENTERPRISE_USER);
|
|
|
reqDTO.setUsername(account);
|
|
|
+ // 该企业账户是否首次登录(一个企业可以有多个账号的情况)
|
|
|
+ reqDTO.setFirst(null != bindUser && null == bindUser.getLoginDate());
|
|
|
+ if(null != bindUser) {
|
|
|
+ EnterpriseDO enterprise = enterpriseService.getEnterprise(bindUser.getEnterpriseId());
|
|
|
+ if(null != enterprise && (null == enterprise.getFirst() || !enterprise.getFirst())) {
|
|
|
+ // 企业首次登录
|
|
|
+ reqDTO.setEnterpriseFirst(true);
|
|
|
+ }else {
|
|
|
+ // 企业非首次登录
|
|
|
+ reqDTO.setEnterpriseFirst(false);
|
|
|
+ }
|
|
|
+ }else {
|
|
|
+ // 企业非首次登录
|
|
|
+ reqDTO.setEnterpriseFirst(false);
|
|
|
+ }
|
|
|
reqDTO.setUserAgent(ServletUtils.getUserAgent());
|
|
|
reqDTO.setUserIp(getClientIP());
|
|
|
reqDTO.setResult(loginResult.getResult());
|
|
|
- loginLogApi.createLoginLog(reqDTO);
|
|
|
+ loginLogService.createLoginLog(reqDTO);
|
|
|
// 更新最后登录时间
|
|
|
- if (null != bindUserId && Objects.equals(LoginResultEnum.SUCCESS.getResult(), loginResult.getResult())) {
|
|
|
- userBindService.updateUserLogin(bindUserId, getClientIP());
|
|
|
+ if (null != bindUser && Objects.equals(LoginResultEnum.SUCCESS.getResult(), loginResult.getResult())) {
|
|
|
+ userBindService.updateUserLogin(bindUser.getId(), getClientIP());
|
|
|
}
|
|
|
}
|
|
|
|
|
|
|
|
|
@Override
|
|
|
public void logout(String token) {
|
|
|
+ Long enterpriseId = LoginUserContext.getEnterpriseId();
|
|
|
// 删除访问令牌
|
|
|
OAuth2AccessTokenRespDTO accessTokenRespDTO = oauth2TokenApi.removeAccessToken(token).getCheckedData();
|
|
|
if (accessTokenRespDTO == null) {
|
|
|
return;
|
|
|
}
|
|
|
// 删除成功,则记录登出日志
|
|
|
- createLogoutLog(accessTokenRespDTO.getUserId());
|
|
|
+ createLogoutLog(enterpriseId, accessTokenRespDTO.getUserId());
|
|
|
}
|
|
|
|
|
|
- private void createLogoutLog(Long userId) {
|
|
|
- LoginUser loginUser = LoginUserContext.get();
|
|
|
- MdeUserDO user = userService.getMdeUser(loginUser.getId());
|
|
|
+ private void createLogoutLog(Long enterpriseId, Long userId) {
|
|
|
+ EnterpriseUserBindDO bind = userBindService.selectByEnterpriseIdAndUserId(
|
|
|
+ enterpriseId,
|
|
|
+ userId
|
|
|
+ );
|
|
|
LoginLogCreateReqDTO reqDTO = new LoginLogCreateReqDTO();
|
|
|
reqDTO.setLogType(LoginLogTypeEnum.LOGOUT_SELF.getType());
|
|
|
reqDTO.setTraceId(TracerUtils.getTraceId());
|
|
|
reqDTO.setUserId(userId);
|
|
|
- reqDTO.setUserType(getUserType().getValue());
|
|
|
- reqDTO.setUsername(user.getPhone());
|
|
|
+ reqDTO.setEnterpriseId(enterpriseId);
|
|
|
+ reqDTO.setType(TYPE_ENTERPRISE_USER);
|
|
|
+ reqDTO.setUsername(null != bind ? bind.getPhone() : null);
|
|
|
+ reqDTO.setFirst(false);
|
|
|
reqDTO.setUserAgent(ServletUtils.getUserAgent());
|
|
|
reqDTO.setUserIp(getClientIP());
|
|
|
reqDTO.setResult(LoginResultEnum.SUCCESS.getResult());
|
|
|
- loginLogApi.createLoginLog(reqDTO);
|
|
|
+ loginLogService.createLoginLog(reqDTO);
|
|
|
}
|
|
|
|
|
|
@Override
|