|
|
@@ -43,46 +43,64 @@ public class ADSyncService {
|
|
|
@Autowired
|
|
|
private OrgService orgService;
|
|
|
|
|
|
- public void addOrg(String ouName, LdapConnection connection) throws Exception {
|
|
|
+ public void addOrg(HrOrg org, LdapConnection connection) {
|
|
|
if(connection==null || !connection.isConnected()){
|
|
|
connection=ldapConnectionManager.getConnection();
|
|
|
}
|
|
|
- Dn dn = new Dn("OU=" + ouName + "," + adConfig.getBaseDn());
|
|
|
- Entry entry = new DefaultEntry(
|
|
|
- dn,
|
|
|
- "objectClass: top",
|
|
|
- "objectClass: organizationalUnit"
|
|
|
- );
|
|
|
- entry.add("description", "SE");
|
|
|
- connection.add(entry);
|
|
|
- }
|
|
|
- public void deleteOrg(String ouName, LdapConnection connection) throws Exception {
|
|
|
- Dn dn = new Dn("OU=" + ouName + "," + adConfig.getBaseDn());
|
|
|
- /*connection.delete(dn);
|
|
|
- // 1. 构建组织DN
|
|
|
- Dn orgDn = new Dn("OU=" + ouName + "," + adConfig.getBaseDn());
|
|
|
-
|
|
|
- // 2. 检查组织下是否存在用户
|
|
|
- String userFilter = FilterBuilder.equal("objectClass", "user").toString();
|
|
|
- List<Entry> userEntries = (List<Entry>) connection.search(
|
|
|
- orgDn,
|
|
|
- userFilter,
|
|
|
- SearchScope.SUBTREE,
|
|
|
- "dn"
|
|
|
- );
|
|
|
+ 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);
|
|
|
+ }
|
|
|
|
|
|
- // 3. 如果存在用户,抛出异常阻止删除
|
|
|
- if (!userEntries.isEmpty()) {
|
|
|
- logger.info("组织下存在{}个用户,无法删除: {}",userEntries.size(),ouName);
|
|
|
+ }
|
|
|
+ public void deleteOrg(String ouName, LdapConnection connection) {
|
|
|
+ Dn dn = null;
|
|
|
+ logger.info("删除组织{}",ouName);
|
|
|
+ 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);
|
|
|
+ }
|
|
|
+ } catch (Exception e) {
|
|
|
+ throw new RuntimeException(e);
|
|
|
}
|
|
|
- connection.*/
|
|
|
- // 4. 不存在用户,执行删除
|
|
|
- // connection.delete(orgDn);
|
|
|
+ logger.info("删除组织{}成功",ouName);
|
|
|
+
|
|
|
}
|
|
|
- public void updateOrg(String oldOUName,String newOuName, LdapConnection connection) throws Exception {
|
|
|
- Dn dn = new Dn("OU=" + oldOUName + "," + adConfig.getBaseDn());
|
|
|
- Rdn newRdn = new Rdn("OU=" + newOuName);
|
|
|
- connection.rename(dn, newRdn, true);
|
|
|
+ public void updateOrg(String oldOUName,String newOuName, LdapConnection connection) {
|
|
|
+ if(connection==null || !connection.isConnected()){
|
|
|
+ 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());
|
|
|
+ e.printStackTrace();
|
|
|
+ }
|
|
|
+ logger.info("更新组织{}为{} 成功",oldOUName,newOuName);
|
|
|
+
|
|
|
}
|
|
|
|
|
|
public void addUser(String userName,String displayName, String ouName, String password) throws LdapException, UnsupportedEncodingException {
|
|
|
@@ -105,22 +123,6 @@ public class ADSyncService {
|
|
|
entry.add("sn", displayName.substring(1));
|
|
|
connection.add(entry);
|
|
|
}
|
|
|
- /* public void deleteUser(String userName, String ouName) throws Exception {
|
|
|
- try (LdapConnection connection = new LdapNetworkConnection(adConfig.getUrl())) {
|
|
|
- connection.bind(adConfig.getAdminDn(), adConfig.getAdminPassword());
|
|
|
-
|
|
|
- Dn dn = new Dn("CN=" + userName + ",OU=" + ouName + "," + adConfig.getBaseDn());
|
|
|
- connection.delete(dn);
|
|
|
- }
|
|
|
- }
|
|
|
- public void deleteOrganizationalUnit(String ouName) throws Exception {
|
|
|
- try (LdapConnection connection = new LdapNetworkConnection(adConfig.getUrl())) {
|
|
|
- connection.bind(adConfig.getAdminDn(), adConfig.getAdminPassword());
|
|
|
-
|
|
|
- Dn dn = new Dn("OU=" + ouName + "," + adConfig.getBaseDn());
|
|
|
- connection.delete(dn);
|
|
|
- }
|
|
|
- }*/
|
|
|
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");
|
|
|
@@ -136,21 +138,50 @@ public class ADSyncService {
|
|
|
for (HrOrg org : orgList) {
|
|
|
if("已审核".equals(org.getOr_status())) {
|
|
|
if(!orgDns.isEmpty()){
|
|
|
- Optional<String> optionalString = orgDns.stream().filter(dn->dn.startsWith(getOUPath(org.getOr_path()))).findFirst();
|
|
|
- if (optionalString.isPresent()) {
|
|
|
- String orgDn = optionalString.get();
|
|
|
- System.out.println(orgDn);
|
|
|
- String orgDescription = orgDn.substring(orgDn.indexOf(";")+1);
|
|
|
- if(!StringUtil.hasText(orgDescription)){
|
|
|
+ boolean isExist = false;
|
|
|
+ for(String orgDn : orgDns){
|
|
|
+ String orgDescription = orgDn.substring(orgDn.indexOf(";")+1);
|
|
|
+ String orgPath = orgDn.split(";")[0];
|
|
|
+ // 组织编号匹配成功
|
|
|
+ if(StringUtil.hasText(orgDescription) && orgDescription.equals(org.getOr_code())){
|
|
|
+ //组织编号一致
|
|
|
+ if(! orgPath.startsWith(org.getOr_path())){
|
|
|
+ //组织路径不一致,则更新组织层级信息
|
|
|
+ updateOrg(orgPath, getOUPath(org.getOr_path()), connection);
|
|
|
+ }
|
|
|
+ isExist =true;
|
|
|
+ break;
|
|
|
+ }
|
|
|
+ //组织编号匹配不成功,路径匹配相同
|
|
|
+ if(orgPath.equals(getOUPath(org.getOr_path()))){
|
|
|
+ if(!StringUtil.hasText(orgDescription)){
|
|
|
+ //更新AD域组织编号信息
|
|
|
+ updateOrgDescription(orgDn.split(";")[0], org.getOr_code());
|
|
|
+ }
|
|
|
+ isExist =true;
|
|
|
+ break;
|
|
|
+ }
|
|
|
+
|
|
|
+ }
|
|
|
+ //不存在的组织
|
|
|
+ if(!isExist){
|
|
|
+ //添加组织
|
|
|
+ addOrg(org, connection);
|
|
|
+ }
|
|
|
|
|
|
- updateOrgDescription(orgDn.split(";")[0], org.getOr_code());
|
|
|
- }
|
|
|
- }
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
-
|
|
|
-
|
|
|
+ //判断AD域组织存在但没有已审核的组织信息,删除AD域组织
|
|
|
+ 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))){
|
|
|
+ //删除AD域组织
|
|
|
+ deleteOrg(orgDn, connection);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
}
|
|
|
/**
|
|
|
* 修改自定义description属性
|
|
|
@@ -210,7 +241,7 @@ public class ADSyncService {
|
|
|
for(int i=paths.length-1;i>=0;i--){
|
|
|
ouPath+="OU="+paths[i]+",";
|
|
|
}
|
|
|
- return ouPath.substring(0,ouPath.length()-1);
|
|
|
+ return ouPath.substring(0,ouPath.length()-1)+",OU=User,"+adConfig.getBaseDn();
|
|
|
}
|
|
|
|
|
|
public List<String> getOrganizations(LdapConnection connection) throws IOException {
|
|
|
@@ -231,9 +262,12 @@ public class ADSyncService {
|
|
|
while (result.next()) {
|
|
|
try {
|
|
|
entry =result.get();
|
|
|
- System.out.println(entry.toString());
|
|
|
- orgDns.add(String.format("%s;%s", entry.getDn().toString(),
|
|
|
- StringUtil.hasText(entry.get("description"))?entry.get("description").toString():""));
|
|
|
+ //排除掉根目录
|
|
|
+ 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():""));
|
|
|
+
|
|
|
+ }
|
|
|
|
|
|
} catch (CursorException e) {
|
|
|
e.printStackTrace();
|
|
|
@@ -252,6 +286,19 @@ 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 {
|
|
|
|
|
|
+ }
|
|
|
+ }
|
|
|
|
|
|
}
|