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.
231 lines
9.5 KiB
231 lines
9.5 KiB
/* |
|
* Copyright (C), 2018-2021 |
|
* Project: starter |
|
* FileName: UserServiceKit |
|
* Author: xx |
|
* Date: 2021/5/14 8:28 |
|
*/ |
|
package com.fr.plugin.dingtalksyn.kit; |
|
|
|
import com.dingtalk.api.response.OapiV2UserGetResponse; |
|
import com.fanruan.api.log.LogKit; |
|
import com.fanruan.api.util.StringKit; |
|
import com.fr.decision.authority.AuthorityContext; |
|
import com.fr.decision.authority.data.BaseUserDataRecord; |
|
import com.fr.decision.authority.data.CustomRole; |
|
import com.fr.decision.authority.data.Post; |
|
import com.fr.decision.authority.data.User; |
|
import com.fr.decision.authority.data.personnel.DepRole; |
|
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.bean.user.UserUpdateBean; |
|
import com.fr.decision.webservice.utils.UserSourceFactory; |
|
import com.fr.decision.webservice.utils.WebServiceUtils; |
|
import com.fr.decision.webservice.v10.user.PositionService; |
|
import com.fr.decision.webservice.v10.user.UserService; |
|
import com.fr.stable.StringUtils; |
|
import com.fr.stable.collections.CollectionUtils; |
|
import com.fr.stable.query.QueryFactory; |
|
import com.fr.stable.query.restriction.RestrictionFactory; |
|
|
|
import java.util.ArrayList; |
|
import java.util.List; |
|
|
|
/** |
|
* <Function Description><br> |
|
* <UserServiceKit> |
|
* |
|
* @author xx |
|
* @since 1.0.0 |
|
*/ |
|
public class UserServiceKit extends UserService { |
|
private static volatile UserServiceKit userServiceKit = null; |
|
|
|
public UserServiceKit() { |
|
} |
|
|
|
public static UserServiceKit getInstance() { |
|
if (userServiceKit == null) { |
|
userServiceKit = new UserServiceKit(); |
|
} |
|
return userServiceKit; |
|
} |
|
|
|
public UserBean createUserBean(OapiV2UserGetResponse.UserGetResponse userGetResponse) throws Exception { |
|
UserBean userBean = new UserBean(); |
|
userBean.setUsername(userGetResponse.getUserid()); |
|
userBean.setRealName(userGetResponse.getName()); |
|
userBean.setEmail(userGetResponse.getEmail()); |
|
userBean.setMobile(userGetResponse.getMobile()); |
|
userBean.setPassword(TransmissionTool.defaultEncrypt("123456")); |
|
if (!CollectionUtils.isEmpty(userGetResponse.getRoleList())) { |
|
userBean.setRoleIds(oapiUserRoles2Ids(userGetResponse.getRoleList())); |
|
} |
|
if (!CollectionUtils.isEmpty(userGetResponse.getDeptIdList())) { |
|
List<String> departmentPostIds = createDepartmentPostIds(userGetResponse.getDeptIdList(), userGetResponse.getTitle()); |
|
userBean.setDepartmentPostIds(departmentPostIds); |
|
} |
|
return userBean; |
|
} |
|
|
|
/** |
|
* 钉钉部门list转为部门职务组合list |
|
* |
|
* @param deptIdList |
|
* @param title |
|
* @return |
|
* @throws Exception |
|
*/ |
|
private List<String> createDepartmentPostIds(List<Long> deptIdList, String title) throws Exception { |
|
List<String> departmentPostIds = new ArrayList<>(); |
|
String departmentPostId; |
|
for (long currentDepId : deptIdList) { |
|
departmentPostId = String.valueOf(currentDepId); |
|
if (StringKit.isBlank(departmentPostId) || StringKit.equals(departmentPostId, "null")) { |
|
continue; |
|
} |
|
// 职务处理 |
|
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("dingtalksyn-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 = this.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(OapiV2UserGetResponse.UserGetResponse userGetResponse) throws Exception { |
|
User user = this.getUserByUserName(userGetResponse.getUserid()); |
|
if (user == null) { |
|
return null; |
|
} |
|
UserBean userBean = new UserBean(); |
|
userBean.setId(user.getId()); |
|
userBean.setUsername(user.getUserName()); |
|
userBean.setRealName(userGetResponse.getName()); |
|
userBean.setEmail(userGetResponse.getEmail()); |
|
userBean.setMobile(userGetResponse.getMobile()); |
|
if (!CollectionUtils.isEmpty(userGetResponse.getRoleList())) { |
|
userBean.setRoleIds(oapiUserRoles2Ids(userGetResponse.getRoleList())); |
|
} |
|
if (!CollectionUtils.isEmpty(userGetResponse.getDeptIdList())) { |
|
List<String> departmentPostIds = createDepartmentPostIds(userGetResponse.getDeptIdList(), userGetResponse.getTitle()); |
|
userBean.setDepartmentPostIds(departmentPostIds); |
|
} |
|
return userBean; |
|
} |
|
|
|
public int updateRoleUsers(String var1, UserUpdateBean var2) throws Exception { |
|
String[] var3 = var2.getAddUserIds(); |
|
String[] var4 = var2.getRemoveUserIds(); |
|
CustomRole var5 = (CustomRole) AuthorityContext.getInstance().getCustomRoleController().getById(var1); |
|
int var6 = 0; |
|
String[] var7; |
|
int var8; |
|
int var9; |
|
String var10; |
|
if (var3 != null) { |
|
var7 = var3; |
|
var8 = var3.length; |
|
|
|
for (var9 = 0; var9 < var8; ++var9) { |
|
var10 = var7[var9]; |
|
// MetricRegistry.getMetric().submit(OperateMessage.build("Dec-Module-User_Manager", "Dec-Role_User", this.getRoleUsername(var1, var10), "Dec-Log_Add")); |
|
UserSourceFactory.getInstance().checkSource((BaseUserDataRecord) AuthorityContext.getInstance().getUserController().getById(var10), var5); |
|
AuthorityContext.getInstance().getUserController().addUserToCustomRole(var10, var1); |
|
++var6; |
|
} |
|
} |
|
|
|
if (var4 != null) { |
|
var7 = var4; |
|
var8 = var4.length; |
|
|
|
for (var9 = 0; var9 < var8; ++var9) { |
|
var10 = var7[var9]; |
|
// MetricRegistry.getMetric().submit(OperateMessage.build("Dec-Module-User_Manager", "Dec-Role_User", this.getRoleUsername(var1, var10), "Dec-Log_Delete")); |
|
UserSourceFactory.getInstance().checkSource((BaseUserDataRecord) AuthorityContext.getInstance().getUserController().getById(var10), var5); |
|
AuthorityContext.getInstance().getUserController().removeUserFromCustomRole(var10, var1); |
|
++var6; |
|
} |
|
} |
|
|
|
return var6; |
|
} |
|
|
|
/** |
|
* 增加用户部门关联 |
|
* |
|
* @param userBean |
|
* @throws Exception |
|
*/ |
|
public void addUserDepartment(UserBean userBean) throws Exception { |
|
if (CollectionUtils.isEmpty(userBean.getDepartmentPostIds())) { |
|
return; |
|
} |
|
for (String departmentPostId : userBean.getDepartmentPostIds()) { |
|
if (StringUtils.isEmpty(departmentPostId)) { |
|
continue; |
|
} |
|
User user = this.getUserByUserName(userBean.getUsername()); |
|
DepRole depRole = WebServiceUtils.parseUniqueDepartmentPostId(departmentPostId); |
|
UserSourceFactory.getInstance().checkSource(user, AuthorityContext.getInstance().getDepartmentController().getById(depRole.getDepartmentId())); |
|
AuthorityContext.getInstance().getUserController().addUserToDepartmentAndPost(user.getId(), depRole.getDepartmentId(), depRole.getPostId()); |
|
} |
|
} |
|
|
|
private String[] oapiUserRoles2Ids(List<OapiV2UserGetResponse.UserRole> oapiUserRoleList) { |
|
String[] roleIds = new String[oapiUserRoleList.size()]; |
|
for (int i = 0; i < oapiUserRoleList.size(); i++) { |
|
roleIds[i] = String.valueOf(oapiUserRoleList.get(i).getId()); |
|
} |
|
return roleIds; |
|
} |
|
} |