You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
197 lines
8.6 KiB
197 lines
8.6 KiB
3 years ago
|
/*
|
||
|
* Copyright (C), 2018-2021
|
||
|
* Project: starter
|
||
|
* FileName: UserServiceKit
|
||
|
* Author: Louis
|
||
|
* Date: 2021/5/14 8:28
|
||
|
*/
|
||
|
package com.fr.plugin.hdmu.kit;
|
||
|
|
||
|
import com.fanruan.api.log.LogKit;
|
||
|
import com.fanruan.api.util.StringKit;
|
||
|
import com.fr.decision.authority.AuthorityContext;
|
||
|
import com.fr.decision.authority.base.constant.type.operation.ManualOperationType;
|
||
|
import com.fr.decision.authority.data.User;
|
||
|
import com.fr.decision.authority.data.extra.user.type.PasswordChangeTimeKey;
|
||
|
import com.fr.decision.base.util.UUIDUtil;
|
||
|
import com.fr.decision.privilege.TransmissionTool;
|
||
|
import com.fr.decision.privilege.encrpt.PasswordValidator;
|
||
|
import com.fr.decision.webservice.bean.user.DepartmentPostBean;
|
||
|
import com.fr.decision.webservice.bean.user.UserBean;
|
||
|
import com.fr.decision.webservice.exception.login.UserPwdErrorException;
|
||
|
import com.fr.decision.webservice.utils.UserSourceFactory;
|
||
|
import com.fr.decision.webservice.v10.login.kickout.KickOutConfig;
|
||
|
import com.fr.decision.webservice.v10.login.kickout.KickOutUserEvent;
|
||
|
import com.fr.decision.webservice.v10.login.lock.LoginLockService;
|
||
|
import com.fr.decision.webservice.v10.password.strategy.PasswordStrategyService;
|
||
|
import com.fr.decision.webservice.v10.user.PositionService;
|
||
|
import com.fr.decision.webservice.v10.user.UserService;
|
||
|
import com.fr.event.EventDispatcher;
|
||
|
import com.fr.general.ComparatorUtils;
|
||
|
import com.fr.json.JSONObject;
|
||
|
import com.fr.stable.StringUtils;
|
||
|
|
||
|
import java.util.ArrayList;
|
||
|
import java.util.Date;
|
||
|
import java.util.List;
|
||
|
|
||
|
import static com.fr.plugin.hdmu.user.SsoUserManager.JOB_CODE;
|
||
|
import static com.fr.plugin.hdmu.user.SsoUserManager.ORG_CODE;
|
||
|
|
||
|
/**
|
||
|
* <Function Description><br>
|
||
|
* <UserServiceKit>
|
||
|
*
|
||
|
* @author fr.open
|
||
|
* @since 1.0.0
|
||
|
*/
|
||
|
public class UserServiceKit extends UserService {
|
||
|
public static final String USER_NAME = "prWorkNo";
|
||
|
public static final String REAL_NAME = "userName";
|
||
|
public static final String ENABLE = "status";
|
||
|
public static final String EMAIL = "email";
|
||
|
public static final String PASSWORD = "appPwd";
|
||
|
public static final String REQUEST_FLAG = "requestFlag";
|
||
|
private static volatile UserServiceKit userServiceKit = null;
|
||
|
|
||
|
public UserServiceKit() {
|
||
|
}
|
||
|
|
||
|
public static UserServiceKit getInstance() {
|
||
|
if (userServiceKit == null) {
|
||
|
userServiceKit = new UserServiceKit();
|
||
|
}
|
||
|
return userServiceKit;
|
||
|
}
|
||
|
|
||
|
@Override
|
||
|
public void resetPassword(UserBean var1) throws Exception {
|
||
|
User var2 = (User) AuthorityContext.getInstance().getUserController().getById(var1.getId());
|
||
|
String var3 = var2.getUserName();
|
||
|
String var4 = var2.getId();
|
||
|
String var5 = TransmissionTool.decrypt(var1.getPassword());
|
||
|
String var6 = var2.getPassword();
|
||
|
String var7 = "";
|
||
|
PasswordValidator var8 = UserSourceFactory.getInstance().getUserSource(var2).getPasswordValidator();
|
||
|
if (StringUtils.isNotEmpty(var5)) {
|
||
|
// AuthenticController var9 = ControllerFactory.getInstance().getAuthenticController(var2.getId());
|
||
|
// if (!var9.passwordChangeable(var2)) {
|
||
|
// throw new UserPasswordCanNotChangeException();
|
||
|
// }
|
||
|
var7 = var8.encode(var2.getUserName(), var5, var2.getSalt());
|
||
|
var2.setPassword(var7);
|
||
|
PasswordStrategyService.getInstance().checkPasswordHistoryList(var2, var7);
|
||
|
}
|
||
|
AuthorityContext.getInstance().getUserController().update(var2.lastOperationType(ManualOperationType.KEY));
|
||
|
if (StringUtils.isNotEmpty(var7) && !ComparatorUtils.equals(var6, var7)) {
|
||
|
var2.salt(UUIDUtil.generate());
|
||
|
var2.password(var8.encode(var2.getUserName(), var5, var2.getSalt()));
|
||
|
AuthorityContext.getInstance().getUserController().update(var2);
|
||
|
PasswordStrategyService.getInstance().updateUserPasswordHistoryList(var2, var6);
|
||
|
AuthorityContext.getInstance().getUserController().addPasswordChangeTime(var4, PasswordChangeTimeKey.KEY, (Date) null);
|
||
|
LoginLockService.getInstance().unlockByUserId(var4);
|
||
|
PasswordStrategyService.getInstance().unblockPasswordChangeByUser(var2);
|
||
|
EventDispatcher.fire(KickOutUserEvent.KickOutUser, new KickOutConfig(new UserPwdErrorException(), new String[]{var3}));
|
||
|
}
|
||
|
}
|
||
|
|
||
|
public UserBean createUserBean(JSONObject account) throws Exception {
|
||
|
UserBean userBean = new UserBean();
|
||
|
userBean.setUsername(account.getString(USER_NAME));
|
||
|
userBean.setRealName(account.getString(REAL_NAME));
|
||
|
userBean.setEnable(StringKit.equals(account.getJSONArray("userJobAttr").getJSONObject(0).getString(ENABLE), "1"));
|
||
|
userBean.setEmail(account.getString(EMAIL));
|
||
|
userBean.setPassword(TransmissionTool.defaultEncrypt(account.getString(PASSWORD)));
|
||
|
userBean.setPasswordChangeable(true);
|
||
|
String departmentId;
|
||
|
String position;
|
||
|
try {
|
||
|
departmentId = account.getString(ORG_CODE);
|
||
|
position = account.getString(JOB_CODE);
|
||
|
} catch (Exception e) {
|
||
|
departmentId = StringKit.EMPTY;
|
||
|
position = StringKit.EMPTY;
|
||
|
}
|
||
|
if (StringKit.isNotBlank(departmentId)) {
|
||
|
List<String> departmentPostIds = createDepartmentPostIds(departmentId, position);
|
||
|
userBean.setDepartmentPostIds(departmentPostIds);
|
||
|
}
|
||
|
return userBean;
|
||
|
}
|
||
|
|
||
|
/**
|
||
|
* 转为部门职务组合
|
||
|
*
|
||
|
* @param departmentPostId
|
||
|
* @param positionId
|
||
|
* @return
|
||
|
* @throws Exception
|
||
|
*/
|
||
|
private List<String> createDepartmentPostIds(String departmentPostId, String positionId) throws Exception {
|
||
|
List<String> departmentPostIds = new ArrayList<>();
|
||
|
if (StringKit.isBlank(departmentPostId) || StringKit.equals(departmentPostId, "null")) {
|
||
|
return departmentPostIds;
|
||
|
}
|
||
|
if (StringKit.isNotBlank(positionId)) {
|
||
|
String positionName = PositionServiceKit.getInstance().getPostNameById(positionId);
|
||
|
List<DepartmentPostBean> departmentPostBeanList = PositionService.getInstance().getPositionsUnderParentDepartment(getAdminUserId(), departmentPostId, positionName);
|
||
|
if (departmentPostBeanList == null || departmentPostBeanList.isEmpty()) {
|
||
|
try {
|
||
|
AuthorityContext.getInstance().getPostController().addPostToDepartment(positionId, departmentPostId);
|
||
|
} catch (Exception e) {
|
||
|
LogKit.info("sso-UserServiceKit-createDepartmentPostIds-addPostToDepartmentFailed-position:{}, departmentId:{}", positionId + positionName, departmentPostId);
|
||
|
LogKit.error(e.getMessage(), e);
|
||
|
}
|
||
|
}
|
||
|
departmentPostId = departmentPostId + "@@@" + positionId;
|
||
|
}
|
||
|
departmentPostIds.add(departmentPostId);
|
||
|
return departmentPostIds;
|
||
|
}
|
||
|
|
||
|
/**
|
||
|
* 获取管理员id
|
||
|
*
|
||
|
* @return
|
||
|
* @throws Exception
|
||
|
*/
|
||
|
public String getAdminUserId() throws Exception {
|
||
|
List<String> adminUserIdList = UserService.getInstance().getAdminUserIdList();
|
||
|
if (adminUserIdList.isEmpty()) {
|
||
|
return "admin";
|
||
|
}
|
||
|
return StringKit.isNotBlank(adminUserIdList.get(0)) ? adminUserIdList.get(0) : "admin";
|
||
|
}
|
||
|
|
||
|
public void editUser(UserBean userBean) throws Exception {
|
||
|
super.editUser(userBean, this.getAdminUserId());
|
||
|
}
|
||
|
|
||
|
public UserBean updateUserBean(JSONObject account) throws Exception {
|
||
|
User user = UserService.getInstance().getUserByUserName(account.getString(USER_NAME));
|
||
|
if (user == null) {
|
||
|
return null;
|
||
|
}
|
||
|
UserBean userBean = new UserBean();
|
||
|
userBean.setId(user.getId());
|
||
|
userBean.setUsername(user.getUserName());
|
||
|
userBean.setRealName(account.getString(REAL_NAME));
|
||
|
userBean.setEnable(user.isEnable());
|
||
|
userBean.setEmail(account.getString(EMAIL));
|
||
|
userBean.setPassword(TransmissionTool.defaultEncrypt(account.getString(PASSWORD)));
|
||
|
String departmentId;
|
||
|
String position;
|
||
|
try {
|
||
|
departmentId = account.getString(ORG_CODE);
|
||
|
position = account.getString(JOB_CODE);
|
||
|
} catch (Exception e) {
|
||
|
departmentId = StringKit.EMPTY;
|
||
|
position = StringKit.EMPTY;
|
||
|
}
|
||
|
if (StringKit.isNotBlank(departmentId)) {
|
||
|
List<String> departmentPostIds = createDepartmentPostIds(departmentId, position);
|
||
|
userBean.setDepartmentPostIds(departmentPostIds);
|
||
|
}
|
||
|
return userBean;
|
||
|
}
|
||
|
}
|