JSD-9377 IAM用户数据集插件(数据集、用户集成)
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.

173 lines
7.2 KiB

/*
* Copyright (C), 2018-2021
* Project: starter
* FileName: UserServiceKit
* Author: Louis
* Date: 2021/5/14 8:28
*/
package com.fr.plugin.icgq.kit;
import com.fanruan.api.log.LogKit;
import com.fanruan.api.util.StringKit;
import com.fr.decision.authority.AuthorityContext;
import com.fr.decision.authority.data.Department;
import com.fr.decision.authority.data.Post;
import com.fr.decision.authority.data.User;
import com.fr.decision.privilege.TransmissionTool;
import com.fr.decision.webservice.bean.user.DepartmentPostBean;
import com.fr.decision.webservice.bean.user.UserBean;
import com.fr.decision.webservice.v10.user.PositionService;
import com.fr.decision.webservice.v10.user.UserService;
import com.fr.json.JSONObject;
import com.fr.stable.query.QueryFactory;
import com.fr.stable.query.condition.QueryCondition;
import com.fr.stable.query.restriction.RestrictionFactory;
import java.util.ArrayList;
import java.util.List;
import static com.fr.plugin.icgq.kit.DepartmentServiceKit.IDT_ORG__NAME;
/**
* <Function Description><br>
* <UserServiceKit>
*
* @author fr.open
* @since 1.0.0
*/
public class UserServiceKit extends UserService {
public static final String USER_NAME = "app_account__account_no";
public static final String ACCOUNT_NAME = "app_account__account_name";
public static final String ACCOUNT_NO = "app_account__account_no";
public static final String ENABLE = "app_account__status";
public static final String EMAIL = "idt_user__email";
public static final String MOBILE = "idt_user__mobile";
public static final String POSITION = "idt_job__name";
public static final String PASSWORD = "idt_user__pwd";
private static volatile UserServiceKit userServiceKit = null;
public UserServiceKit() {
}
public static UserServiceKit getInstance() {
if (userServiceKit == null) {
userServiceKit = new UserServiceKit();
}
return userServiceKit;
}
public UserBean createUserBean(JSONObject account) throws Exception {
UserBean userBean = new UserBean();
userBean.setUsername(account.getString(USER_NAME));
userBean.setRealName(account.getString(ACCOUNT_NAME));
userBean.setEnable(true);
userBean.setEmail(account.getString(EMAIL));
userBean.setMobile(account.getString(MOBILE));
userBean.setPassword(TransmissionTool.defaultEncrypt(account.getString(USER_NAME) + "123456"));
String departmentId;
String position = StringKit.EMPTY;
try {
String depName = account.getJSONArray("orgs").getJSONObject(0).getString(IDT_ORG__NAME);
QueryCondition condition = QueryFactory.create().addRestriction(RestrictionFactory.and(RestrictionFactory.eq("name", depName), RestrictionFactory.eq("parentId", null)));
Department department = AuthorityContext.getInstance().getDepartmentController().findOne(condition);
departmentId = department.getId();
} catch (Exception e) {
departmentId = StringKit.EMPTY;
}
if (StringKit.isNotBlank(departmentId)) {
List<String> departmentPostIds = UserServiceKit.getInstance().createDepartmentPostIds(departmentId, position);
userBean.setDepartmentPostIds(departmentPostIds);
}
return userBean;
}
/**
* 部门id转为部门职务组合list
*
* @param departmentPostId
* @param title
* @return
* @throws Exception
*/
public List<String> createDepartmentPostIds(String departmentPostId, String title) throws Exception {
List<String> departmentPostIds = new ArrayList<>();
// 职务处理
// String positionId = positionSynOperation(title, departmentPostId);
// if (StringKit.isNotBlank(positionId)) {
// departmentPostId = departmentPostId + "@@@" + positionId;
// }
departmentPostIds.add(departmentPostId);
return departmentPostIds;
}
/**
* 职务同步操作
*
* @param title
* @return
* @throws Exception
*/
private String positionSynOperation(String title, String departmentId) throws Exception {
String position = StringKit.isNotBlank(title) ? title : "职员";
Post post = AuthorityContext.getInstance().getPostController().findOne(QueryFactory.create().addRestriction(RestrictionFactory.eq("name", position)));
String positionId;
if (post == null) {
positionId = PositionService.getInstance().addPosition(position, position);
} else {
positionId = post.getId();
}
List<DepartmentPostBean> departmentPostBeanList = PositionService.getInstance().getPositionsUnderParentDepartment(getAdminUserId(), departmentId, position);
if (departmentPostBeanList == null || departmentPostBeanList.isEmpty()) {
try {
AuthorityContext.getInstance().getPostController().addPostToDepartment(positionId, departmentId);
} catch (Exception e) {
LogKit.info("icgq-UserServiceKit-positionSynOperation-addPostToDepartmentFailed-position:{}, departmentId:{}", positionId + position, departmentId);
LogKit.error(e.getMessage(), e);
}
}
return positionId;
}
/**
* 获取管理员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 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(ACCOUNT_NAME));
userBean.setEnable(true);
userBean.setEmail(account.getString(EMAIL));
userBean.setMobile(account.getString(MOBILE));
String departmentId;
String position = StringKit.EMPTY;
try {
String depName = account.getJSONArray("orgs").getJSONObject(0).getString(IDT_ORG__NAME);
QueryCondition condition = QueryFactory.create().addRestriction(RestrictionFactory.and(RestrictionFactory.eq("name", depName), RestrictionFactory.eq("parentId", null)));
Department department = AuthorityContext.getInstance().getDepartmentController().findOne(condition);
departmentId = department.getId();
} catch (Exception e) {
departmentId = StringKit.EMPTY;
}
if (StringKit.isNotBlank(departmentId)) {
List<String> departmentPostIds = UserServiceKit.getInstance().createDepartmentPostIds(departmentId, position);
userBean.setDepartmentPostIds(departmentPostIds);
}
return userBean;
}
}