|
|
@@ -3,17 +3,19 @@ package com.uas.eis.serviceImpl;
|
|
|
import com.uas.eis.core.LdapConnectionManager;
|
|
|
import com.uas.eis.core.config.ADConfig;
|
|
|
import com.uas.eis.dao.BaseDao;
|
|
|
+import com.uas.eis.entity.ADUser;
|
|
|
+import com.uas.eis.entity.Employee;
|
|
|
import com.uas.eis.entity.HrOrg;
|
|
|
+import com.uas.eis.utils.PinyinUtils;
|
|
|
import com.uas.eis.utils.StringUtil;
|
|
|
import org.apache.directory.api.ldap.model.cursor.CursorException;
|
|
|
import org.apache.directory.api.ldap.model.cursor.EntryCursor;
|
|
|
import org.apache.directory.api.ldap.model.entry.*;
|
|
|
import org.apache.directory.api.ldap.model.exception.LdapException;
|
|
|
import org.apache.directory.api.ldap.model.exception.LdapInvalidDnException;
|
|
|
-import org.apache.directory.api.ldap.model.message.SearchResultEntry;
|
|
|
+import org.apache.directory.api.ldap.model.message.ModifyDnRequest;
|
|
|
import org.apache.directory.api.ldap.model.message.SearchScope;
|
|
|
import org.apache.directory.api.ldap.model.name.Rdn;
|
|
|
-import org.apache.directory.api.util.Strings;
|
|
|
import org.apache.directory.ldap.client.api.LdapConnection;
|
|
|
import org.apache.directory.api.ldap.model.name.Dn;
|
|
|
import org.slf4j.Logger;
|
|
|
@@ -21,7 +23,6 @@ import org.slf4j.LoggerFactory;
|
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
|
import org.springframework.context.annotation.Lazy;
|
|
|
import org.springframework.stereotype.Service;
|
|
|
-
|
|
|
import java.io.IOException;
|
|
|
import java.io.UnsupportedEncodingException;
|
|
|
import java.util.*;
|
|
|
@@ -41,97 +42,155 @@ public class ADSyncService {
|
|
|
@Autowired
|
|
|
private BaseDao baseDao;
|
|
|
@Autowired
|
|
|
- private OrgService orgService;
|
|
|
+ private UasSyncService uasSyncService;
|
|
|
|
|
|
- public void addOrg(HrOrg org, LdapConnection connection) {
|
|
|
- if(connection==null || !connection.isConnected()){
|
|
|
- connection=ldapConnectionManager.getConnection();
|
|
|
+ public void syncUser() throws IOException {
|
|
|
+ LdapConnection connection = ldapConnectionManager.getConnection();
|
|
|
+ List<Employee> employeeList = uasSyncService.getADUserList();
|
|
|
+ List<ADUser> adUserList = getUsers(connection);
|
|
|
+ List<HrOrg> orgList = uasSyncService.getADOrgList();
|
|
|
+ for (Employee employee : employeeList) {
|
|
|
+ if(employee.getEm_adid()!=null){
|
|
|
+ //判断是否需要更新组织
|
|
|
+ if("离职".equals(employee.getEm_class())){
|
|
|
+ deleteUser(employee.getEm_adid(),connection);
|
|
|
+ }
|
|
|
+ Optional<HrOrg> orgOptional = orgList.stream().filter(org -> org.getOr_code().equals(employee.getOrcode())).findFirst();
|
|
|
+ logger.info("同步用户更新:{}",employee.getEm_name());
|
|
|
+ if(orgOptional.isPresent()){
|
|
|
+ String newOUPath = getOUPath(orgOptional.get().getOr_path());
|
|
|
+ logger.info("同步用户更新:oldpath{},newpath{}",getUserOUPath(employee.getEm_adid()),newOUPath);
|
|
|
+ if(employee.getEm_code().equals("ADMIN") && !getUserOUPath(employee.getEm_adid()).equals(newOUPath)){
|
|
|
+ moveUser(employee.getEm_adid(),"CN="+employee.getEm_name()+","+newOUPath,connection);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ }else if (!"生产序列--普工".equals(employee.getEm_emptype()) && !"其它人员".equals(employee.getEm_emptype())) {
|
|
|
+ Optional<HrOrg> orgOptional = orgList.stream().filter(org -> org.getOr_code().equals(employee.getOrcode())).findFirst();
|
|
|
+ if(orgOptional.isPresent()){
|
|
|
+ String ouPath = getOUPath(orgOptional.get().getOr_path());
|
|
|
+ addUser(PinyinUtils.getCustomPinyin(employee.getEm_name()),employee.getEm_name(),ouPath,employee.getEm_password());
|
|
|
+ baseDao.updateByCondition("employee","em_adid='CN="+employee.getEm_name()+","+ouPath+"'","em_code='"+employee.getEm_code()+"'");
|
|
|
+ }
|
|
|
+
|
|
|
+ }
|
|
|
}
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ public void addUser(String userName,String displayName, String ouName, String password) {
|
|
|
+ LdapConnection connection = ldapConnectionManager.getConnection();
|
|
|
+ Dn dn = null;
|
|
|
+ logger.info("添加用户:{},displayName:{},ouName:{},password{}",userName,displayName,ouName,password);
|
|
|
try {
|
|
|
- logger.info("添加组织{}",org.getOr_path());
|
|
|
- Dn dn = new Dn(getOUPath(org.getOr_path()));
|
|
|
+ dn = new Dn("CN="+displayName+"," + ouName);
|
|
|
Entry entry = new DefaultEntry(
|
|
|
dn,
|
|
|
"objectClass: top",
|
|
|
- "objectClass: organizationalUnit"
|
|
|
+ "objectClass: person",
|
|
|
+ "objectClass: organizationalPerson",
|
|
|
+ "objectClass: user",
|
|
|
+ "sAMAccountName: " + userName,
|
|
|
+ "userPrincipalName: " + userName + "@" + adConfig.getBaseDn().replace("DC=", "").replace(",", "."),
|
|
|
+ "userPassword: " + password
|
|
|
);
|
|
|
- entry.add("description", org.getOr_code());
|
|
|
+ //处理中文写入异常问题
|
|
|
+ entry.add("cn",displayName);
|
|
|
+ entry.add("displayName",displayName);
|
|
|
+ entry.add("givenName", displayName.substring(0, 1));
|
|
|
+ entry.add("sn", displayName.substring(1));
|
|
|
connection.add(entry);
|
|
|
- logger.info("添加组织{}成功",org.getOr_path());
|
|
|
} catch (Exception e) {
|
|
|
- logger.error("添加组织失败",e);
|
|
|
+ logger.error("添加用户失败:{},错误{}",userName,e.getMessage());
|
|
|
}
|
|
|
|
|
|
}
|
|
|
- public void deleteOrg(String ouName, LdapConnection connection) {
|
|
|
- Dn dn = null;
|
|
|
- logger.info("删除组织{}",ouName);
|
|
|
+ public void deleteUser(String userDn, LdapConnection connection) {
|
|
|
+ if(connection==null || !connection.isConnected()){
|
|
|
+ connection = ldapConnectionManager.getConnection();
|
|
|
+ }
|
|
|
+ try {
|
|
|
+ logger.info("删除用户:{}",userDn);
|
|
|
+ Dn dn = new Dn(userDn);
|
|
|
+ connection.delete(dn);
|
|
|
+ logger.info("删除用户成功:{}",userDn);
|
|
|
+ } catch (LdapException e) {
|
|
|
+ logger.error("删除用户失败:{}",userDn);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ public void moveUser(String oldUserDnStr, String newRdnStr, LdapConnection connection) {
|
|
|
+ logger.info("用户 {} 移动组织 {}", oldUserDnStr, newRdnStr);
|
|
|
+ if (connection == null || !connection.isConnected()) {
|
|
|
+ connection = ldapConnectionManager.getConnection();
|
|
|
+ }
|
|
|
try {
|
|
|
- dn = new Dn( ouName + ",OU=" + adConfig.getBaseDn());
|
|
|
- String filter = "(objectClass=organizationalUnit)||(objectClass=user)";
|
|
|
- EntryCursor result = connection.search(
|
|
|
- ouName,
|
|
|
- filter,
|
|
|
- SearchScope.SUBTREE, // 搜索所有子节点
|
|
|
- "dn"
|
|
|
- );
|
|
|
- if (result.iterator().hasNext()) {
|
|
|
- logger.info("组织下存在下级,无法删除{}",ouName);
|
|
|
- }else {
|
|
|
- connection.delete(dn);
|
|
|
- }
|
|
|
+ connection.moveAndRename(oldUserDnStr, newRdnStr,true);
|
|
|
+
|
|
|
+ logger.info("用户 {} 已成功移动到组织 {}", oldUserDnStr, newRdnStr);
|
|
|
} catch (Exception e) {
|
|
|
- throw new RuntimeException(e);
|
|
|
+ logger.error("移动用户失败: {}", oldUserDnStr, e);
|
|
|
+ throw new RuntimeException("移动用户失败: " + e.getMessage());
|
|
|
}
|
|
|
- logger.info("删除组织{}成功",ouName);
|
|
|
-
|
|
|
}
|
|
|
- public void updateOrg(String oldOUName,String newOuName, LdapConnection connection) {
|
|
|
+ //初始化用户
|
|
|
+ public void initUser() throws IOException {
|
|
|
+ LdapConnection connection = ldapConnectionManager.getConnection();
|
|
|
+ List<Employee> employeeList = uasSyncService.getADUserList();
|
|
|
+ List<ADUser> adUserList = getUsers(connection);
|
|
|
+ for (ADUser adUser : adUserList) {
|
|
|
+ for (Employee employee : employeeList) {
|
|
|
+ if(employee.getEm_name().equals(adUser.getUserCn())){
|
|
|
+ baseDao.updateByCondition("employee","em_adid='"+adUser.getUserDn()+"'", "em_code ='"+employee.getEm_code()+"'");
|
|
|
+ break;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ public List<ADUser> getUsers(LdapConnection connection) throws IOException {
|
|
|
+ List<ADUser> userList = new ArrayList<>();
|
|
|
if(connection==null || !connection.isConnected()){
|
|
|
- connection=ldapConnectionManager.getConnection();
|
|
|
+ connection = ldapConnectionManager.getConnection();
|
|
|
}
|
|
|
- logger.info("更新组织{}为{}",oldOUName,newOuName);
|
|
|
try {
|
|
|
- Dn dn = new Dn(oldOUName);
|
|
|
- Rdn newRdn = new Rdn( newOuName);
|
|
|
- connection.rename(dn, newRdn, true);
|
|
|
- } catch (Exception e) {
|
|
|
- logger.error(e.getMessage());
|
|
|
+ // 搜索所有用户
|
|
|
+ String filter = "(objectClass=organizationalPerson)";
|
|
|
+ EntryCursor result = connection.search(
|
|
|
+ "OU=User,"+adConfig.getBaseDn(), // AD基础DN,从配置文件获取
|
|
|
+ filter,
|
|
|
+ SearchScope.SUBTREE
|
|
|
+ );
|
|
|
+ Entry entry;
|
|
|
+ while (result.next()) {
|
|
|
+ try {
|
|
|
+ entry =result.get();
|
|
|
+ ADUser adUser = new ADUser();
|
|
|
+ adUser.setUserDn(entry.getDn().toString());
|
|
|
+ adUser.setUserCn(entry.get("cn").get().getString());
|
|
|
+ adUser.setAccountName(entry.get("sAMAccountName").get().getString());
|
|
|
+ adUser.setUserPrincipalName(entry.get("userPrincipalName").get().getString());
|
|
|
+ userList.add(adUser);
|
|
|
+
|
|
|
+ } catch (CursorException e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return userList;
|
|
|
+ } catch (LdapException e) {
|
|
|
e.printStackTrace();
|
|
|
+ } catch (CursorException e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ } finally {
|
|
|
+ if (connection != null) {
|
|
|
+ connection.close();
|
|
|
+ }
|
|
|
}
|
|
|
- logger.info("更新组织{}为{} 成功",oldOUName,newOuName);
|
|
|
|
|
|
+ return null;
|
|
|
}
|
|
|
|
|
|
- public void addUser(String userName,String displayName, String ouName, String password) throws LdapException, UnsupportedEncodingException {
|
|
|
- LdapConnection connection = ldapConnectionManager.getConnection();
|
|
|
- Dn dn = new Dn("CN="+displayName+"," + ouName + "," + adConfig.getBaseDn());
|
|
|
- Entry entry = new DefaultEntry(
|
|
|
- dn,
|
|
|
- "objectClass: top",
|
|
|
- "objectClass: person",
|
|
|
- "objectClass: organizationalPerson",
|
|
|
- "objectClass: user",
|
|
|
- "sAMAccountName: " + userName,
|
|
|
- "userPrincipalName: " + userName + "@" + adConfig.getBaseDn().replace("DC=", "").replace(",", "."),
|
|
|
- "userPassword: " + password
|
|
|
- );
|
|
|
- //处理中文写入异常问题
|
|
|
- entry.add("cn",displayName);
|
|
|
- entry.add("displayName",displayName);
|
|
|
- entry.add("givenName", displayName.substring(0, 1));
|
|
|
- entry.add("sn", displayName.substring(1));
|
|
|
- connection.add(entry);
|
|
|
- }
|
|
|
- public void deleteUser(String userName, String ouName) throws Exception {
|
|
|
- LdapConnection connection = ldapConnectionManager.getConnection();
|
|
|
- Dn dn = new Dn("CN=测试,OU=人力资源部,OU=经营班子,OU=董事会,OU=User,DC=si,DC=ad");
|
|
|
- connection.delete(dn);
|
|
|
- }
|
|
|
-
|
|
|
public void syncOrg() throws IOException {
|
|
|
LdapConnection connection = ldapConnectionManager.getConnection();
|
|
|
- List<HrOrg> orgList = orgService.getOrgList();
|
|
|
+ List<HrOrg> orgList = uasSyncService.getADOrgList();
|
|
|
logger.info("同步组织数量:{}",orgList.size());
|
|
|
List<String> orgDns = getOrganizations(connection);
|
|
|
//判断组织是否存在
|
|
|
@@ -145,8 +204,9 @@ public class ADSyncService {
|
|
|
// 组织编号匹配成功
|
|
|
if(StringUtil.hasText(orgDescription) && orgDescription.equals(org.getOr_code())){
|
|
|
//组织编号一致
|
|
|
- if(! orgPath.startsWith(org.getOr_path())){
|
|
|
+ if(! orgPath.startsWith(getOUPath(org.getOr_path()))){
|
|
|
//组织路径不一致,则更新组织层级信息
|
|
|
+ logger.info("updateOrg 更新组织信息:old{},new{},orgCode{}",orgPath,getOUPath(org.getOr_path()) , orgDescription);
|
|
|
updateOrg(orgPath, getOUPath(org.getOr_path()), connection);
|
|
|
}
|
|
|
isExist =true;
|
|
|
@@ -176,13 +236,74 @@ public class ADSyncService {
|
|
|
for(String orgDn : orgDns){
|
|
|
String orgDescription = orgDn.substring(orgDn.indexOf(";")+1);
|
|
|
if(StringUtil.hasText(orgDescription)){
|
|
|
- if(!orgList.stream().anyMatch(org -> org.getOr_code().equals(orgDescription))){
|
|
|
+ if(!orgList.stream().anyMatch(org -> !"已禁用".equals(org.getOr_status()) && org.getOr_code().equals(orgDescription))){
|
|
|
//删除AD域组织
|
|
|
- deleteOrg(orgDn, connection);
|
|
|
+ deleteOrg(orgDn.split(";")[0], connection);
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
+ public void addOrg(HrOrg org, LdapConnection connection) {
|
|
|
+ if(connection==null || !connection.isConnected()){
|
|
|
+ connection=ldapConnectionManager.getConnection();
|
|
|
+ }
|
|
|
+ try {
|
|
|
+ logger.info("添加组织{}",org.getOr_path());
|
|
|
+ Dn dn = new Dn(getOUPath(org.getOr_path()));
|
|
|
+ Entry entry = new DefaultEntry(
|
|
|
+ dn,
|
|
|
+ "objectClass: top",
|
|
|
+ "objectClass: organizationalUnit"
|
|
|
+ );
|
|
|
+ entry.add("description", org.getOr_code());
|
|
|
+ connection.add(entry);
|
|
|
+ logger.info("添加组织{}成功",org.getOr_path());
|
|
|
+ } catch (Exception e) {
|
|
|
+ logger.error("添加组织失败",e);
|
|
|
+ }
|
|
|
+
|
|
|
+ }
|
|
|
+ public void deleteOrg(String ouName, LdapConnection connection) {
|
|
|
+ if(connection==null || !connection.isConnected()){
|
|
|
+ connection=ldapConnectionManager.getConnection();
|
|
|
+ }
|
|
|
+ Dn dn = null;
|
|
|
+ logger.info("删除组织{}",ouName);
|
|
|
+ try {
|
|
|
+ dn = new Dn( ouName );
|
|
|
+ String filter = "(objectClass=organizationalUnit)||(objectClass=user)";
|
|
|
+ EntryCursor result = connection.search(
|
|
|
+ ouName,
|
|
|
+ filter,
|
|
|
+ SearchScope.ONELEVEL, // 搜索所有子节点
|
|
|
+ "dn"
|
|
|
+ );
|
|
|
+ if (result.next()) {
|
|
|
+ System.out.println(result.get().toString());
|
|
|
+ logger.info("组织下存在下级,无法删除{}",ouName);
|
|
|
+ }else {
|
|
|
+ connection.delete(dn);
|
|
|
+ }
|
|
|
+ } catch (Exception e) {
|
|
|
+ throw new RuntimeException(e);
|
|
|
+ }
|
|
|
+ logger.info("删除组织{}成功",ouName);
|
|
|
+
|
|
|
+ }
|
|
|
+ public void updateOrg(String oldOUName,String newOuName, LdapConnection connection) {
|
|
|
+ if(connection==null || !connection.isConnected()){
|
|
|
+ connection=ldapConnectionManager.getConnection();
|
|
|
+ }
|
|
|
+ logger.info("更新组织{}为{}",oldOUName,newOuName);
|
|
|
+ try {
|
|
|
+ connection.moveAndRename(oldOUName,newOuName,true);
|
|
|
+ } catch (Exception e) {
|
|
|
+ logger.error(e.getMessage());
|
|
|
+ e.printStackTrace();
|
|
|
+ }
|
|
|
+ logger.info("更新组织{}为{} 成功",oldOUName,newOuName);
|
|
|
+
|
|
|
+ }
|
|
|
/**
|
|
|
* 修改自定义description属性
|
|
|
* */
|
|
|
@@ -223,7 +344,6 @@ public class ADSyncService {
|
|
|
);
|
|
|
|
|
|
List<String> orgDns = new ArrayList<>();
|
|
|
- System.out.println(result.iterator().hasNext());
|
|
|
return orgDns;
|
|
|
} catch (Exception e) {
|
|
|
e.printStackTrace();
|
|
|
@@ -244,6 +364,10 @@ public class ADSyncService {
|
|
|
return ouPath.substring(0,ouPath.length()-1)+",OU=User,"+adConfig.getBaseDn();
|
|
|
}
|
|
|
|
|
|
+ private String getUserOUPath(String userPath){
|
|
|
+ return userPath.substring(userPath.indexOf(",")+1);
|
|
|
+ }
|
|
|
+
|
|
|
public List<String> getOrganizations(LdapConnection connection) throws IOException {
|
|
|
if(connection==null || !connection.isConnected()){
|
|
|
connection = ldapConnectionManager.getConnection();
|
|
|
@@ -265,7 +389,7 @@ public class ADSyncService {
|
|
|
//排除掉根目录
|
|
|
if(!entry.getDn().toString().startsWith("OU=User")){
|
|
|
orgDns.add(String.format("%s;%s", entry.getDn().toString(),
|
|
|
- StringUtil.hasText(entry.get("description"))?entry.get("description").toString():""));
|
|
|
+ StringUtil.hasText(entry.get("description"))?entry.get("description").get():""));
|
|
|
|
|
|
}
|
|
|
|
|
|
@@ -286,19 +410,5 @@ public class ADSyncService {
|
|
|
|
|
|
return null;
|
|
|
}
|
|
|
- public void deleteOrgs() {
|
|
|
- LdapConnection connection = null;
|
|
|
- try {
|
|
|
- connection = ldapConnectionManager.getConnection();
|
|
|
- List<HrOrg> orgList = orgService.getOrgList();
|
|
|
- for(HrOrg org : orgList){
|
|
|
- deleteOrg(getOUPath(org.getOr_path()), connection);
|
|
|
- }
|
|
|
- } catch (Exception e) {
|
|
|
- throw new RuntimeException(e);
|
|
|
- } finally {
|
|
|
-
|
|
|
- }
|
|
|
- }
|
|
|
|
|
|
}
|