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.
179 lines
7.3 KiB
179 lines
7.3 KiB
/* |
|
* Copyright (C), 2018-2021 |
|
* Project: starter |
|
* FileName: DingTalkUserManager |
|
* Author: Louis |
|
* Date: 2021/4/21 16:18 |
|
*/ |
|
package com.fr.plugin.mqh.dingtalksyn.user; |
|
|
|
import com.dingtalk.api.response.OapiRoleListResponse; |
|
import com.dingtalk.api.response.OapiV2DepartmentListsubResponse; |
|
import com.dingtalk.api.response.OapiV2UserGetResponse; |
|
import com.fanruan.api.decision.user.UserKit; |
|
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.Department; |
|
import com.fr.decision.webservice.bean.user.RoleBean; |
|
import com.fr.decision.webservice.bean.user.UserBean; |
|
import com.fr.decision.webservice.v10.user.CustomRoleService; |
|
import com.fr.decision.webservice.v10.user.UserService; |
|
import com.fr.plugin.mqh.dingtalksyn.config.DingSynConfig; |
|
import com.fr.plugin.mqh.dingtalksyn.kit.CustomRoleServiceKit; |
|
import com.fr.plugin.mqh.dingtalksyn.kit.DepartmentServiceKit; |
|
import com.fr.plugin.mqh.dingtalksyn.kit.UserServiceKit; |
|
import com.taobao.api.ApiException; |
|
|
|
import java.util.List; |
|
|
|
import static com.fr.plugin.mqh.dingtalksyn.kit.DepartmentServiceKit.DECISION_DEP_ROOT; |
|
import static com.fr.plugin.mqh.dingtalksyn.utils.DingAPI.*; |
|
|
|
/** |
|
* <Function Description><br> |
|
* <DingTalkUserManager> |
|
* |
|
* @author Louis |
|
* @since 1.0.0 |
|
*/ |
|
public final class DingTalkUserManager { |
|
private DingSynConfig config; |
|
|
|
public DingTalkUserManager() { |
|
this.config = DingSynConfig.getInstance(); |
|
} |
|
|
|
private static class HOLDER { |
|
private static final DingTalkUserManager INSTANCE = new DingTalkUserManager(); |
|
} |
|
public static DingTalkUserManager getInstance() { |
|
return HOLDER.INSTANCE; |
|
} |
|
|
|
/** |
|
* 同步更新的字段包括:用户、邮箱、手机、部门、职位、角色。 |
|
* 用户唯一字段用户名,更新时也是基于用户名。 |
|
* |
|
* @throws Exception |
|
*/ |
|
public synchronized void synDingTalkUsers() throws Exception { |
|
LogKit.info("dingtalksyn-DingTalkUserManager-synDingTalkUsers-start"); |
|
// 同步用户角色 |
|
List<OapiRoleListResponse.OpenRoleGroup> oapiRoleGroupList = getOapiRoleList(); |
|
for (OapiRoleListResponse.OpenRoleGroup openRoleGroup : oapiRoleGroupList) { |
|
List<OapiRoleListResponse.OpenRole> openRoleList = openRoleGroup.getRoles(); |
|
for (OapiRoleListResponse.OpenRole openRole : openRoleList) { |
|
roleSynOperation(openRoleGroup.getName(), openRole); |
|
} |
|
} |
|
// 同步部门和用户信息 |
|
departmentSynLoop(this.config.getRootDepId()); |
|
LogKit.info("dingtalksyn-DingTalkUserManager-synDingTalkUsers-end"); |
|
} |
|
|
|
/** |
|
* 按部门遍历子部门并同步人员信息 |
|
* |
|
* @param deptId |
|
* @throws Exception |
|
*/ |
|
private void departmentSynLoop(long deptId) throws ApiException { |
|
List<OapiV2DepartmentListsubResponse.DeptBaseResponse> departmentList = getDepartmentList(deptId); |
|
if (departmentList == null || departmentList.isEmpty()) { |
|
return; |
|
} |
|
// 同步部门信息 |
|
for (OapiV2DepartmentListsubResponse.DeptBaseResponse oapiDepartment : departmentList) { |
|
try { |
|
departmentSynOperation(oapiDepartment); |
|
} catch (Exception e) { |
|
LogKit.error(e.getMessage(), e); |
|
} |
|
// 同步当前部门的用户信息 |
|
List<String> deptMemberList = getDeptMember(oapiDepartment.getDeptId()); |
|
for (String userId : deptMemberList) { |
|
try { |
|
OapiV2UserGetResponse.UserGetResponse userGetResponse = getUserResponse(userId); |
|
if (userGetResponse == null) { |
|
continue; |
|
} |
|
userSynOperation(userGetResponse); |
|
} catch (Exception e) { |
|
LogKit.error(e.getMessage(), e); |
|
} |
|
} |
|
// 操作子部门遍历 |
|
departmentSynLoop(oapiDepartment.getDeptId()); |
|
} |
|
} |
|
|
|
/** |
|
* 角色的新增更新操作 |
|
* |
|
* @param openRole |
|
* @throws Exception |
|
*/ |
|
private void roleSynOperation(String groupName, OapiRoleListResponse.OpenRole openRole) throws Exception { |
|
if (openRole == null) { |
|
return; |
|
} |
|
RoleBean roleBean = CustomRoleService.getInstance().getCustomRole(String.valueOf(openRole.getId())); |
|
String roleName = StringKit.isBlank(groupName) ? openRole.getName() : groupName + "-" + openRole.getName(); |
|
if (roleBean == null) { |
|
roleBean = new RoleBean(roleName, String.valueOf(openRole.getId()), roleName, ManualOperationType.KEY.toInteger()); |
|
CustomRoleServiceKit.getInstance().addCustomRole(UserServiceKit.getInstance().getAdminUserId(), roleBean); |
|
} else if(!StringKit.equals(roleBean.getText(), roleName)) { |
|
roleBean.setText(roleName); |
|
roleBean.setDescription(roleName); |
|
CustomRoleService.getInstance().editCustomRole(roleBean.getId(), roleBean); |
|
} |
|
} |
|
|
|
/** |
|
* 部门组织的新增更新操作 |
|
* |
|
* @param oapiDepartment |
|
* @throws Exception |
|
*/ |
|
private void departmentSynOperation(OapiV2DepartmentListsubResponse.DeptBaseResponse oapiDepartment) throws Exception { |
|
String departmentId = String.valueOf(oapiDepartment.getDeptId()); |
|
String parentId = String.valueOf(oapiDepartment.getParentId()); |
|
if (StringKit.equals(parentId, String.valueOf(this.config.getRootDepId()))) { parentId = DECISION_DEP_ROOT; } |
|
String depName = oapiDepartment.getName(); |
|
Department department = AuthorityContext.getInstance().getDepartmentController().getById(departmentId); |
|
if (department == null) { |
|
DepartmentServiceKit.getInstance().addDepartment(departmentId, parentId, depName); |
|
} else { |
|
DepartmentServiceKit.getInstance().editDepartment(department.getId(), depName, parentId); |
|
} |
|
} |
|
|
|
/** |
|
* 用户新增更新操作 |
|
* |
|
* @param userGetResponse |
|
* @throws Exception |
|
*/ |
|
private void userSynOperation(OapiV2UserGetResponse.UserGetResponse userGetResponse) throws Exception { |
|
UserBean userBean; |
|
if (UserKit.existUsername(userGetResponse.getUserid())) { |
|
userBean = UserServiceKit.getInstance().updateUserBean(userGetResponse); |
|
if (userBean == null) { |
|
return; |
|
} |
|
UserService.getInstance().editUser(userBean); |
|
UserServiceKit.getInstance().addUserDepartment(userBean); |
|
} else { |
|
userBean = UserServiceKit.getInstance().createUserBean(userGetResponse); |
|
try { |
|
UserService.getInstance().addUser(userBean); |
|
} catch (Exception e) { |
|
LogKit.error("dingtalksyn-DingTalkUserManager-userSynOperation-Username:{}, RealName:{}, Mobile:{}, Email:{}", |
|
userBean.getUsername(), userBean.getRealName(), userBean.getMobile(), userBean.getEmail()); |
|
LogKit.error(e.getMessage(), e); |
|
} |
|
} |
|
} |
|
} |