|
|
@@ -73,9 +73,11 @@ public class ADSyncService {
|
|
|
Optional<HrOrg> orgOptional = orgList.stream().filter(org -> org.getOr_code().equals(employee.getOrcode())).findFirst();
|
|
|
if(orgOptional.isPresent()){
|
|
|
String ouPath = getOUPath(orgOptional.get().getOr_path());
|
|
|
- String adid = addUser(employee.getEm_code(),PinyinUtils.getCustomPinyin(employee.getEm_name()),employee.getEm_name(),ouPath,employee.getEm_password(), employee.getEmjob());
|
|
|
+ String adaccountname = getADID(employee);
|
|
|
+ String adid = addUser(adaccountname,employee.getEm_name(),ouPath,employee.getEm_password(), employee.getEmjob());
|
|
|
+ System.out.println(adaccountname+":"+adid);
|
|
|
if(adid!=null){
|
|
|
- baseDao.updateByCondition("employee","em_adid='"+adid+"'","em_code='"+employee.getEm_code()+"'");
|
|
|
+ baseDao.updateByCondition("employee","em_adid='"+adid+"',em_adaccountname='"+adaccountname+"'","em_code='"+employee.getEm_code()+"'");
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
@@ -83,7 +85,7 @@ public class ADSyncService {
|
|
|
|
|
|
}
|
|
|
|
|
|
- public String addUser(String userCode,String userName,String displayName, String ouName, String password,String job) {
|
|
|
+ public String addUser(String userName,String displayName, String ouName, String password,String job) {
|
|
|
LdapConnection connection = ldapConnectionManager.getConnection();
|
|
|
Dn dn = null;
|
|
|
logger.info("添加用户:{},displayName:{},ouName:{},password{}",userName,displayName,ouName,password);
|
|
|
@@ -111,8 +113,8 @@ public class ADSyncService {
|
|
|
} catch (Exception e) {
|
|
|
if(e.getMessage().contains("(ENTRY_EXISTS)")){
|
|
|
return "CN="+displayName+"," + ouName;
|
|
|
- }else if(e.getMessage().contains("problem 1005 (CONSTRAINT_ATT_TYPE), data 0, Att 90290 (userPrincipalName)")) {
|
|
|
- return addUser(userCode,userName+""+userCode,displayName,ouName,password,job);
|
|
|
+ }else if (e.getMessage().contains("problem 1005 (CONSTRAINT_ATT_TYPE), data 0, Att 90290 (userPrincipalName)")){
|
|
|
+ return addUser(userName+"01",displayName,ouName,password,job);
|
|
|
}
|
|
|
logger.error("添加用户失败:{},错误{}",userName,e.getMessage());
|
|
|
return null;
|
|
|
@@ -195,10 +197,10 @@ public class ADSyncService {
|
|
|
}
|
|
|
}
|
|
|
//初始化用户
|
|
|
- public void initUser() throws IOException {
|
|
|
+ public void initUser(){
|
|
|
LdapConnection connection = ldapConnectionManager.getConnection();
|
|
|
List<Employee> employeeList = uasSyncService.getADUserList();
|
|
|
- List<ADUser> adUserList = getUsers(connection);
|
|
|
+ // List<ADUser> adUserList = getUsers(connection);
|
|
|
List<HrOrg> orgList = uasSyncService.getADOrgList();
|
|
|
/* for (ADUser adUser : adUserList) {
|
|
|
System.out.println(adUser.getUserCn());
|
|
|
@@ -210,7 +212,7 @@ public class ADSyncService {
|
|
|
}
|
|
|
}*/
|
|
|
|
|
|
- for(Employee employee:employeeList){
|
|
|
+ /* for(Employee employee:employeeList){
|
|
|
if(!StringUtil.hasText(employee.getEm_adid())){
|
|
|
Optional<ADUser> adUserOptional = adUserList.stream().filter(adUser -> adUser.getUserDn().equals(employee.getEm_adid())).findFirst();
|
|
|
if(!adUserOptional.isPresent()){
|
|
|
@@ -229,6 +231,38 @@ public class ADSyncService {
|
|
|
}
|
|
|
|
|
|
}
|
|
|
+ }*/
|
|
|
+ /* for(Employee employee:employeeList){
|
|
|
+ if(StringUtil.hasText(employee.getEm_adid())){
|
|
|
+ Dn dn = null;
|
|
|
+ try {
|
|
|
+ dn = new Dn( employee.getEm_adid());
|
|
|
+ } catch (LdapInvalidDnException e) {
|
|
|
+ throw new RuntimeException(e);
|
|
|
+ }
|
|
|
+ Entry entry = null;
|
|
|
+ try {
|
|
|
+ entry = connection.lookup(
|
|
|
+ dn,
|
|
|
+ new String[]{"sAMAccountName"}
|
|
|
+ );
|
|
|
+ } catch (LdapException e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ throw new RuntimeException(e);
|
|
|
+ }
|
|
|
+ if(entry!=null){
|
|
|
+ System.out.println(entry.get("sAMAccountName").get().toString());
|
|
|
+
|
|
|
+ baseDao.updateByCondition("employee","em_adaccountname='"+entry.get("sAMAccountName").get().toString()+"'","em_code='"+employee.getEm_code()+"'");
|
|
|
+
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }*/
|
|
|
+ for(Employee employee:employeeList){
|
|
|
+ if(!StringUtil.hasText(employee.getEm_adid()) && StringUtil.hasText(employee.getEm_adaccountname()) && employee.getEm_adaccountname().endsWith(employee.getEm_code())){
|
|
|
+ // deleteUser(employee.getEm_adid(),connection);
|
|
|
+ //baseDao.updateByCondition("employee","em_adid=null","em_code='"+employee.getEm_code()+"'");
|
|
|
+ }
|
|
|
}
|
|
|
//移动人员组织
|
|
|
}
|
|
|
@@ -273,7 +307,47 @@ public class ADSyncService {
|
|
|
|
|
|
return null;
|
|
|
}
|
|
|
+ public List<ADUser> getUserByName(LdapConnection connection) throws IOException {
|
|
|
+ List<ADUser> userList = new ArrayList<>();
|
|
|
+ if(connection==null || !connection.isConnected()){
|
|
|
+ connection = ldapConnectionManager.getConnection();
|
|
|
+ }
|
|
|
+ try {
|
|
|
+ // 搜索所有用户
|
|
|
+ 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();
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ return null;
|
|
|
+ }
|
|
|
public void syncOrg() {
|
|
|
LdapConnection connection = ldapConnectionManager.getConnection();
|
|
|
List<HrOrg> orgList = uasSyncService.getADOrgList();
|
|
|
@@ -504,4 +578,8 @@ public class ADSyncService {
|
|
|
return null;
|
|
|
}
|
|
|
|
|
|
+ public String getADID(Employee employee) {
|
|
|
+ Object accountid= baseDao.getFieldDataByCondition("dual","get_4a_id('"+employee.getEm_code()+"','"+PinyinUtils.getCustomPinyin(employee.getEm_name())+"','AD')","1=1");
|
|
|
+ return String.valueOf(accountid);
|
|
|
+ }
|
|
|
}
|