mirror of
https://gitee.com/dromara/MaxKey.git
synced 2025-12-07 17:38:32 +08:00
synchronizers optimize
This commit is contained in:
parent
b56496a6df
commit
50dd3ef566
@ -53,7 +53,7 @@ public class Organizations extends JpaBaseEntity implements Serializable {
|
||||
@Column
|
||||
private String namePath;
|
||||
@Column
|
||||
private String level;
|
||||
private int level;
|
||||
@Column
|
||||
private String hasChild;
|
||||
@Column
|
||||
@ -174,11 +174,11 @@ public class Organizations extends JpaBaseEntity implements Serializable {
|
||||
this.type = type;
|
||||
}
|
||||
|
||||
public String getLevel() {
|
||||
public int getLevel() {
|
||||
return level;
|
||||
}
|
||||
|
||||
public void setLevel(String level) {
|
||||
public void setLevel(int level) {
|
||||
this.level = level;
|
||||
}
|
||||
|
||||
|
||||
@ -44,7 +44,7 @@ public class ScimOrganization extends ScimResource{
|
||||
|
||||
private String namePath;
|
||||
|
||||
private String level;
|
||||
private int level;
|
||||
|
||||
private String division;
|
||||
|
||||
@ -130,11 +130,11 @@ public class ScimOrganization extends ScimResource{
|
||||
this.namePath = namePath;
|
||||
}
|
||||
|
||||
public String getLevel() {
|
||||
public int getLevel() {
|
||||
return level;
|
||||
}
|
||||
|
||||
public void setLevel(String level) {
|
||||
public void setLevel(int level) {
|
||||
this.level = level;
|
||||
}
|
||||
|
||||
|
||||
@ -17,12 +17,16 @@
|
||||
|
||||
package org.maxkey.synchronizer.activedirectory;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashMap;
|
||||
import javax.naming.NamingEnumeration;
|
||||
import javax.naming.NamingException;
|
||||
import javax.naming.directory.Attribute;
|
||||
import javax.naming.directory.SearchControls;
|
||||
import javax.naming.directory.SearchResult;
|
||||
|
||||
import org.apache.commons.lang3.StringUtils;
|
||||
import org.maxkey.constants.ConstsStatus;
|
||||
import org.maxkey.constants.ldap.OrganizationalUnit;
|
||||
import org.maxkey.entity.HistorySynchronizer;
|
||||
import org.maxkey.entity.Organizations;
|
||||
@ -42,78 +46,112 @@ public class ActiveDirectoryOrganizationService extends AbstractSynchronizerSer
|
||||
|
||||
public void sync() {
|
||||
loadOrgsById("1");
|
||||
_logger.info("Sync Organizations ...");
|
||||
_logger.info("Sync ActiveDirectory Organizations ...");
|
||||
try {
|
||||
SearchControls constraints = new SearchControls();
|
||||
constraints.setSearchScope(ldapUtils.getSearchScope());
|
||||
NamingEnumeration<SearchResult> results = ldapUtils.getConnection()
|
||||
.search(ldapUtils.getBaseDN(), "(&(objectClass=OrganizationalUnit))", constraints);
|
||||
String filter = "(&(objectClass=OrganizationalUnit))";
|
||||
if(StringUtils.isNotBlank(this.getSynchronizer().getFilters())) {
|
||||
//filter = this.getSynchronizer().getFilters();
|
||||
}
|
||||
|
||||
long recordCount = 0;
|
||||
NamingEnumeration<SearchResult> results =
|
||||
ldapUtils.getConnection().search(ldapUtils.getBaseDN(), filter, constraints);
|
||||
|
||||
ArrayList<Organizations> orgsList = new ArrayList<Organizations>();
|
||||
int maxLevel = 0;
|
||||
long recordCount = 0;
|
||||
while (null != results && results.hasMoreElements()) {
|
||||
Object obj = results.nextElement();
|
||||
if (obj instanceof SearchResult) {
|
||||
recordCount ++;
|
||||
SearchResult si = (SearchResult) obj;
|
||||
_logger.info("Sync OrganizationalUnit Record " + recordCount+" --------------------------------------------------");
|
||||
_logger.trace("name " + si.getName());
|
||||
_logger.info("NameInNamespace " + si.getNameInNamespace());
|
||||
SearchResult sr = (SearchResult) obj;
|
||||
if("OU=Domain Controllers,DC=maxkey,DC=top".endsWith(sr.getNameInNamespace())) {
|
||||
_logger.info("Skip 'OU=Domain Controllers' .");
|
||||
continue;
|
||||
}
|
||||
_logger.debug("Sync OrganizationalUnit {} , name {} , NameInNamespace {}" ,
|
||||
(++recordCount),sr.getName(),sr.getNameInNamespace());
|
||||
|
||||
HashMap<String,Attribute> attributeMap = new HashMap<String,Attribute>();
|
||||
NamingEnumeration<? extends Attribute> attrs = si.getAttributes().getAll();
|
||||
NamingEnumeration<? extends Attribute> attrs = sr.getAttributes().getAll();
|
||||
while (null != attrs && attrs.hasMoreElements()) {
|
||||
Attribute objAttrs = attrs.nextElement();
|
||||
_logger.trace("attribute "+objAttrs.getID() + " : " + objAttrs.get());
|
||||
attributeMap.put(objAttrs.getID().toLowerCase(), objAttrs);
|
||||
}
|
||||
|
||||
Organizations organization = buildOrganization(attributeMap,si.getName(),si.getNameInNamespace());
|
||||
organizationsService.saveOrUpdate(organization);
|
||||
_logger.info("Organizations " + organization);
|
||||
Organizations organization = buildOrganization(attributeMap,sr.getName(),sr.getNameInNamespace());
|
||||
if(organization != null) {
|
||||
orgsList.add(organization);
|
||||
maxLevel = (maxLevel < organization.getLevel()) ? organization.getLevel() : maxLevel ;
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
for (int level = 2 ; level <= maxLevel ; level++) {
|
||||
for(Organizations organization : orgsList) {
|
||||
if(organization.getLevel() == level) {
|
||||
String parentNamePath= organization.getNamePath().substring(0, organization.getNamePath().lastIndexOf("/"));
|
||||
|
||||
if(orgsNamePathMap.get(organization.getNamePath())!=null) {
|
||||
_logger.info("org " + orgsNamePathMap.get(organization.getNamePath()).getNamePath()+" exists.");
|
||||
continue;
|
||||
}
|
||||
|
||||
Organizations parentOrg = orgsNamePathMap.get(parentNamePath);
|
||||
if(parentOrg == null ) {
|
||||
parentOrg = rootOrganization;
|
||||
}
|
||||
organization.setParentId(parentOrg.getId());
|
||||
organization.setParentName(parentOrg.getName());
|
||||
organization.setCodePath(parentOrg.getCodePath()+"/"+organization.getId());
|
||||
_logger.info("parentNamePath " + parentNamePath+" , namePah " + organization.getNamePath());
|
||||
|
||||
organizationsService.saveOrUpdate(organization);
|
||||
orgsNamePathMap.put(organization.getNamePath(), organization);
|
||||
|
||||
HistorySynchronizer historySynchronizer =new HistorySynchronizer();
|
||||
historySynchronizer.setId(historySynchronizer.generateId());
|
||||
historySynchronizer.setSyncId(this.synchronizer.getId());
|
||||
historySynchronizer.setSyncName(this.synchronizer.getName());
|
||||
historySynchronizer.setObjectId(organization.getId());
|
||||
historySynchronizer.setObjectName(organization.getName());
|
||||
historySynchronizer.setObjectType(Organizations.class.getSimpleName());
|
||||
historySynchronizer.setInstId(synchronizer.getInstId());
|
||||
historySynchronizer.setResult("success");
|
||||
this.historySynchronizerService.insert(historySynchronizer);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
//ldapUtils.close();
|
||||
} catch (NamingException e) {
|
||||
e.printStackTrace();
|
||||
_logger.error("NamingException " , e);
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
public Organizations buildOrganization(HashMap<String,Attribute> attributeMap,String name,String nameInNamespace) {
|
||||
if("OU=Domain Controllers,DC=maxkey,DC=top".endsWith(nameInNamespace)) {
|
||||
_logger.info("to skip.");
|
||||
return null;
|
||||
}
|
||||
Organizations org = new Organizations();
|
||||
org.setLdapDn(nameInNamespace);
|
||||
nameInNamespace = nameInNamespace.replaceAll(",OU=", "/").replaceAll("OU=", "/");
|
||||
nameInNamespace = nameInNamespace.substring(0, nameInNamespace.length() - ldapUtils.getBaseDN().length() - 1);
|
||||
String []namePaths = nameInNamespace.split("/");
|
||||
String namePah= "/"+rootOrganization.getName();
|
||||
for(int i = namePaths.length -1 ; i>=0 ;i--) {
|
||||
namePah = namePah + "/"+namePaths[i];
|
||||
}
|
||||
namePah = namePah.substring(0, namePah.length() -1);
|
||||
String parentNamePath= namePah.substring(0, namePah.lastIndexOf("/"));
|
||||
|
||||
if(orgsNamePathMap.get(namePah)!=null) {
|
||||
_logger.info("org " + orgsNamePathMap.get(namePah).getNamePath()+" exists.");
|
||||
return null;
|
||||
}
|
||||
|
||||
Organizations parentOrg = orgsNamePathMap.get(parentNamePath);
|
||||
org.setId(org.generateId());
|
||||
org.setNamePath(namePah);
|
||||
org.setParentId(parentOrg.getId());
|
||||
org.setParentName(parentOrg.getName());
|
||||
org.setCodePath(parentOrg.getCodePath()+"/"+org.getId());
|
||||
_logger.info("parentNamePath " + parentNamePath+" , namePah " + namePah);
|
||||
|
||||
try {
|
||||
Organizations org = new Organizations();
|
||||
org.setLdapDn(nameInNamespace);
|
||||
nameInNamespace = nameInNamespace.replaceAll(",OU=", "/").replaceAll("OU=", "/");
|
||||
nameInNamespace = nameInNamespace.substring(0, nameInNamespace.length() - ldapUtils.getBaseDN().length() - 1);
|
||||
String []namePaths = nameInNamespace.split("/");
|
||||
String namePah= "/"+rootOrganization.getName();
|
||||
for(int i = namePaths.length -1 ; i >= 0 ; i --) {
|
||||
namePah = namePah + "/" + namePaths[i];
|
||||
}
|
||||
|
||||
namePah = namePah.substring(0, namePah.length() - 1);
|
||||
|
||||
org.setId(org.generateId());
|
||||
org.setCode(org.getId());
|
||||
org.setNamePath(namePah);
|
||||
org.setLevel(namePaths.length);
|
||||
org.setName(LdapUtils.getAttributeStringValue(OrganizationalUnit.OU,attributeMap));
|
||||
|
||||
org.setCountry(LdapUtils.getAttributeStringValue(OrganizationalUnit.CO,attributeMap));
|
||||
org.setRegion(LdapUtils.getAttributeStringValue(OrganizationalUnit.ST,attributeMap));
|
||||
org.setLocality(LdapUtils.getAttributeStringValue(OrganizationalUnit.L,attributeMap));
|
||||
@ -121,22 +159,14 @@ public class ActiveDirectoryOrganizationService extends AbstractSynchronizerSer
|
||||
org.setPostalCode(LdapUtils.getAttributeStringValue(OrganizationalUnit.POSTALCODE,attributeMap));
|
||||
org.setDescription(LdapUtils.getAttributeStringValue(OrganizationalUnit.DESCRIPTION,attributeMap));
|
||||
org.setInstId(this.synchronizer.getInstId());
|
||||
orgsNamePathMap.put(org.getNamePath(), org);
|
||||
_logger.info("org " + org);
|
||||
organizationsService.merge(org);
|
||||
HistorySynchronizer historySynchronizer =new HistorySynchronizer();
|
||||
historySynchronizer.setId(historySynchronizer.generateId());
|
||||
historySynchronizer.setSyncId(this.synchronizer.getId());
|
||||
historySynchronizer.setSyncName(this.synchronizer.getName());
|
||||
historySynchronizer.setObjectId(org.getId());
|
||||
historySynchronizer.setObjectName(org.getName());
|
||||
historySynchronizer.setObjectType(Organizations.class.getSimpleName());
|
||||
historySynchronizer.setResult("success");
|
||||
this.historySynchronizerService.insert(historySynchronizer);
|
||||
org.setStatus(ConstsStatus.ACTIVE);
|
||||
|
||||
_logger.debug("Organization " + org);
|
||||
return org;
|
||||
} catch (NamingException e) {
|
||||
e.printStackTrace();
|
||||
_logger.error("NamingException " , e);
|
||||
}
|
||||
return org;
|
||||
return null;
|
||||
}
|
||||
|
||||
|
||||
|
||||
@ -23,6 +23,8 @@ import javax.naming.NamingException;
|
||||
import javax.naming.directory.Attribute;
|
||||
import javax.naming.directory.SearchControls;
|
||||
import javax.naming.directory.SearchResult;
|
||||
|
||||
import org.apache.commons.lang3.StringUtils;
|
||||
import org.maxkey.constants.ldap.ActiveDirectoryUser;
|
||||
import org.maxkey.entity.HistorySynchronizer;
|
||||
import org.maxkey.entity.Organizations;
|
||||
@ -42,57 +44,55 @@ public class ActiveDirectoryUsersService extends AbstractSynchronizerService
|
||||
ActiveDirectoryUtils ldapUtils;
|
||||
|
||||
public void sync() {
|
||||
_logger.info("Sync Users...");
|
||||
_logger.info("Sync ActiveDirectory Users...");
|
||||
loadOrgsById("1");
|
||||
try {
|
||||
SearchControls constraints = new SearchControls();
|
||||
constraints.setSearchScope(ldapUtils.getSearchScope());
|
||||
NamingEnumeration<SearchResult> results = ldapUtils.getConnection()
|
||||
.search(ldapUtils.getBaseDN(), "(&(objectClass=User))", constraints);
|
||||
String filter = StringUtils.isNotBlank(this.getSynchronizer().getFilters())?
|
||||
getSynchronizer().getFilters() : "(&(objectClass=User))";
|
||||
NamingEnumeration<SearchResult> results =
|
||||
ldapUtils.getConnection().search(ldapUtils.getBaseDN(), filter, constraints);
|
||||
|
||||
long recordCount = 0;
|
||||
while (null != results && results.hasMoreElements()) {
|
||||
Object obj = results.nextElement();
|
||||
if (obj instanceof SearchResult) {
|
||||
recordCount ++;
|
||||
SearchResult si = (SearchResult) obj;
|
||||
_logger.info("Sync Users Record " + recordCount+" --------------------------------------------------");
|
||||
_logger.trace("name " + si.getName());
|
||||
_logger.info("NameInNamespace " + si.getNameInNamespace());
|
||||
SearchResult sr = (SearchResult) obj;
|
||||
if(sr.getNameInNamespace().indexOf("CN=Users,DC=maxkey,DC=top")>-1
|
||||
||sr.getNameInNamespace().indexOf("OU=Domain Controllers,DC=maxkey,DC=top")>-1) {
|
||||
_logger.info("to skip.");
|
||||
continue;
|
||||
}
|
||||
_logger.debug("Sync User {} , name {} , NameInNamespace {}" ,
|
||||
(++recordCount),sr.getName(),sr.getNameInNamespace());
|
||||
|
||||
HashMap<String,Attribute> attributeMap = new HashMap<String,Attribute>();
|
||||
NamingEnumeration<? extends Attribute> attrs = si.getAttributes().getAll();
|
||||
NamingEnumeration<? extends Attribute> attrs = sr.getAttributes().getAll();
|
||||
while (null != attrs && attrs.hasMoreElements()) {
|
||||
Attribute objAttrs = attrs.nextElement();
|
||||
_logger.trace("attribute "+objAttrs.getID() + " : " + objAttrs.get());
|
||||
attributeMap.put(objAttrs.getID().toLowerCase(), objAttrs);
|
||||
}
|
||||
|
||||
UserInfo userInfo =buildUserInfo(attributeMap,si.getName(),si.getNameInNamespace());
|
||||
|
||||
userInfo.setPassword(userInfo.getUsername() + "Maxkey@888");
|
||||
userInfoService.saveOrUpdate(userInfo);
|
||||
_logger.info("userInfo " + userInfo);
|
||||
UserInfo userInfo =buildUserInfo(attributeMap,sr.getName(),sr.getNameInNamespace());
|
||||
if(userInfo != null) {
|
||||
userInfo.setPassword(userInfo.getUsername() + "Maxkey@888");
|
||||
userInfoService.saveOrUpdate(userInfo);
|
||||
_logger.info("userInfo " + userInfo);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
//ldapUtils.close();
|
||||
} catch (NamingException e) {
|
||||
e.printStackTrace();
|
||||
_logger.error("NamingException " , e);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
public void postSync(UserInfo userInfo) {
|
||||
|
||||
}
|
||||
|
||||
public UserInfo buildUserInfo(HashMap<String,Attribute> attributeMap,String name,String nameInNamespace) {
|
||||
if(nameInNamespace.indexOf("CN=Users,DC=maxkey,DC=top")>-1
|
||||
||nameInNamespace.indexOf("OU=Domain Controllers,DC=maxkey,DC=top")>-1) {
|
||||
_logger.info("to skip.");
|
||||
return null;
|
||||
}
|
||||
|
||||
UserInfo userInfo = new UserInfo();
|
||||
userInfo.setLdapDn(nameInNamespace);
|
||||
nameInNamespace = nameInNamespace.replaceAll(",OU=", "/").replaceAll("OU=", "/").replaceAll("CN=", "/");
|
||||
@ -107,6 +107,10 @@ public class ActiveDirectoryUsersService extends AbstractSynchronizerService
|
||||
String deptNamePath= namePah.substring(0, namePah.lastIndexOf("/"));
|
||||
_logger.info("deptNamePath " + deptNamePath);
|
||||
Organizations deptOrg = orgsNamePathMap.get(deptNamePath);
|
||||
if(deptOrg == null ) {
|
||||
deptOrg = rootOrganization;
|
||||
}
|
||||
|
||||
userInfo.setDepartment(deptOrg.getName());
|
||||
userInfo.setDepartmentId(deptOrg.getId());
|
||||
try {
|
||||
@ -153,25 +157,18 @@ public class ActiveDirectoryUsersService extends AbstractSynchronizerService
|
||||
userInfo.setTimeZone("Asia/Shanghai");
|
||||
userInfo.setStatus(1);
|
||||
userInfo.setInstId(this.synchronizer.getInstId());
|
||||
UserInfo quser=new UserInfo();
|
||||
quser.setUsername(userInfo.getUsername());
|
||||
UserInfo loadedUser=userInfoService.load(quser);
|
||||
if(loadedUser == null) {
|
||||
|
||||
userInfo.setPassword(userInfo.generateId());
|
||||
userInfoService.insert(userInfo);
|
||||
HistorySynchronizer historySynchronizer =new HistorySynchronizer();
|
||||
historySynchronizer.setId(historySynchronizer.generateId());
|
||||
historySynchronizer.setSyncId(this.synchronizer.getId());
|
||||
historySynchronizer.setSyncName(this.synchronizer.getName());
|
||||
historySynchronizer.setObjectId(userInfo.getId());
|
||||
historySynchronizer.setObjectName(userInfo.getUsername());
|
||||
historySynchronizer.setObjectType(Organizations.class.getSimpleName());
|
||||
historySynchronizer.setResult("success");
|
||||
this.historySynchronizerService.insert(historySynchronizer);
|
||||
}else {
|
||||
_logger.info("username " + userInfo.getUsername()+" exists.");
|
||||
}
|
||||
|
||||
HistorySynchronizer historySynchronizer =new HistorySynchronizer();
|
||||
historySynchronizer.setId(historySynchronizer.generateId());
|
||||
historySynchronizer.setSyncId(this.synchronizer.getId());
|
||||
historySynchronizer.setSyncName(this.synchronizer.getName());
|
||||
historySynchronizer.setObjectId(userInfo.getId());
|
||||
historySynchronizer.setObjectName(userInfo.getUsername());
|
||||
historySynchronizer.setObjectType(Organizations.class.getSimpleName());
|
||||
historySynchronizer.setInstId(synchronizer.getInstId());
|
||||
historySynchronizer.setResult("success");
|
||||
this.historySynchronizerService.insert(historySynchronizer);
|
||||
|
||||
|
||||
} catch (NamingException e) {
|
||||
e.printStackTrace();
|
||||
|
||||
@ -42,7 +42,7 @@ public class DingtalkOrganizationService extends AbstractSynchronizerService im
|
||||
String access_token;
|
||||
|
||||
public void sync() {
|
||||
_logger.info("Sync Organizations ...");
|
||||
_logger.info("Sync Dingtalk Organizations ...");
|
||||
LinkedBlockingQueue<Long> deptsQueue = new LinkedBlockingQueue<Long>();
|
||||
deptsQueue.add(1L);
|
||||
HashMap<Long,DeptBaseResponse> deptMap = new HashMap<Long,DeptBaseResponse>();
|
||||
@ -77,8 +77,6 @@ public class DingtalkOrganizationService extends AbstractSynchronizerService im
|
||||
return rspDepts;
|
||||
}
|
||||
|
||||
|
||||
|
||||
public Organizations buildOrganization(DeptBaseResponse dept,DeptBaseResponse parentDept) {
|
||||
Organizations org = new Organizations();
|
||||
org.setId(dept.getDeptId()+"");
|
||||
|
||||
@ -44,7 +44,7 @@ public class DingtalkUsersService extends AbstractSynchronizerService implement
|
||||
String access_token;
|
||||
|
||||
public void sync() {
|
||||
_logger.info("Sync Users...");
|
||||
_logger.info("Sync Dingtalk Users...");
|
||||
try {
|
||||
|
||||
List<Organizations> organizations =
|
||||
@ -55,6 +55,7 @@ public class DingtalkUsersService extends AbstractSynchronizerService implement
|
||||
for(Organizations dept : organizations) {
|
||||
DingTalkClient client = new DefaultDingTalkClient("https://oapi.dingtalk.com/topapi/v2/user/list");
|
||||
OapiV2UserListRequest req = new OapiV2UserListRequest();
|
||||
_logger.info("DingTalk deptId : {}" , dept.getCode());
|
||||
req.setDeptId(Long.parseLong(dept.getCode()));
|
||||
req.setCursor(0L);
|
||||
req.setSize(100L);
|
||||
@ -62,16 +63,15 @@ public class DingtalkUsersService extends AbstractSynchronizerService implement
|
||||
req.setContainAccessLimit(true);
|
||||
req.setLanguage("zh_CN");
|
||||
OapiV2UserListResponse rsp = client.execute(req, access_token);
|
||||
_logger.info("response : " + rsp.getBody());
|
||||
_logger.trace("response : {}" , rsp.getBody());
|
||||
|
||||
if(rsp.getErrcode()==0) {
|
||||
for(ListUserResponse user :rsp.getResult().getList()) {
|
||||
_logger.info("name : " + user.getName()+" , "+user.getLoginId()+" , "+user.getUserid());
|
||||
_logger.debug("name : {} , {} , {}", user.getName(),user.getLoginId(),user.getUserid());
|
||||
UserInfo userInfo = buildUserInfo(user);
|
||||
_logger.info("userInfo " + userInfo);
|
||||
_logger.trace("userInfo {}" , userInfo);
|
||||
userInfo.setPassword(userInfo.getUsername() + "Maxkey@888");
|
||||
userInfoService.saveOrUpdate(userInfo);
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -83,10 +83,6 @@ public class DingtalkUsersService extends AbstractSynchronizerService implement
|
||||
|
||||
}
|
||||
|
||||
public void postSync(UserInfo userInfo) {
|
||||
|
||||
}
|
||||
|
||||
public UserInfo buildUserInfo(ListUserResponse user) {
|
||||
UserInfo userInfo = new UserInfo();
|
||||
|
||||
|
||||
@ -43,7 +43,7 @@ public class FeishuOrganizationService extends AbstractSynchronizerService imple
|
||||
static String DEPTS_URL="https://open.feishu.cn/open-apis/contact/v3/departments/%s/children?page_size=50";
|
||||
|
||||
public void sync() {
|
||||
_logger.info("Sync Organizations ...");
|
||||
_logger.info("Sync Feishu Organizations ...");
|
||||
|
||||
LinkedBlockingQueue<String> deptsQueue = new LinkedBlockingQueue<String>();
|
||||
deptsQueue.add("0");
|
||||
|
||||
@ -44,7 +44,7 @@ public class FeishuUsersService extends AbstractSynchronizerService implements I
|
||||
static String USERS_URL="https://open.feishu.cn/open-apis/contact/v3/users/find_by_department";
|
||||
|
||||
public void sync() {
|
||||
_logger.info("Sync Users...");
|
||||
_logger.info("Sync Feishu Users...");
|
||||
try {
|
||||
List<Organizations> organizations =
|
||||
organizationsService.find("instid = ?",
|
||||
|
||||
@ -17,6 +17,7 @@
|
||||
|
||||
package org.maxkey.synchronizer.ldap;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashMap;
|
||||
|
||||
import javax.naming.NamingEnumeration;
|
||||
@ -24,6 +25,9 @@ import javax.naming.NamingException;
|
||||
import javax.naming.directory.Attribute;
|
||||
import javax.naming.directory.SearchControls;
|
||||
import javax.naming.directory.SearchResult;
|
||||
|
||||
import org.apache.commons.lang3.StringUtils;
|
||||
import org.maxkey.constants.ConstsStatus;
|
||||
import org.maxkey.constants.ldap.OrganizationalUnit;
|
||||
import org.maxkey.entity.HistorySynchronizer;
|
||||
import org.maxkey.entity.Organizations;
|
||||
@ -41,38 +45,80 @@ public class LdapOrganizationService extends AbstractSynchronizerService implem
|
||||
LdapUtils ldapUtils;
|
||||
|
||||
public void sync() {
|
||||
_logger.info("Sync Organizations ...");
|
||||
_logger.info("Sync Ldap Organizations ...");
|
||||
loadOrgsById("1");
|
||||
try {
|
||||
SearchControls constraints = new SearchControls();
|
||||
constraints.setSearchScope(ldapUtils.getSearchScope());
|
||||
NamingEnumeration<SearchResult> results = ldapUtils.getConnection()
|
||||
.search(ldapUtils.getBaseDN(), "(&(objectClass=OrganizationalUnit))", constraints);
|
||||
String filter = "(&(objectClass=OrganizationalUnit))";
|
||||
if(StringUtils.isNotBlank(this.getSynchronizer().getFilters())) {
|
||||
//filter = this.getSynchronizer().getFilters();
|
||||
}
|
||||
NamingEnumeration<SearchResult> results =
|
||||
ldapUtils.getConnection().search(ldapUtils.getBaseDN(), filter , constraints);
|
||||
|
||||
long recordCount = 0;
|
||||
ArrayList<Organizations> orgsList = new ArrayList<Organizations>();
|
||||
int maxLevel = 0;
|
||||
long recordCount = 0;
|
||||
while (null != results && results.hasMoreElements()) {
|
||||
Object obj = results.nextElement();
|
||||
if (obj instanceof SearchResult) {
|
||||
recordCount ++;
|
||||
SearchResult si = (SearchResult) obj;
|
||||
_logger.info("Sync OrganizationalUnit Record " + recordCount+" --------------------------------------------------");
|
||||
_logger.trace("name " + si.getName());
|
||||
_logger.info("NameInNamespace " + si.getNameInNamespace());
|
||||
SearchResult sr = (SearchResult) obj;
|
||||
_logger.debug("Sync OrganizationalUnit {} , name {} , NameInNamespace {}" ,
|
||||
(++recordCount),sr.getName(),sr.getNameInNamespace());
|
||||
|
||||
HashMap<String,Attribute> attributeMap = new HashMap<String,Attribute>();
|
||||
NamingEnumeration<? extends Attribute> attrs = si.getAttributes().getAll();
|
||||
NamingEnumeration<? extends Attribute> attrs = sr.getAttributes().getAll();
|
||||
while (null != attrs && attrs.hasMoreElements()) {
|
||||
Attribute objAttrs = attrs.nextElement();
|
||||
_logger.trace("attribute "+objAttrs.getID() + " : " + objAttrs.get());
|
||||
attributeMap.put(objAttrs.getID().toLowerCase(), objAttrs);
|
||||
}
|
||||
|
||||
Organizations organization = buildOrganization(attributeMap,si.getName(),si.getNameInNamespace());
|
||||
organizationsService.saveOrUpdate(organization);
|
||||
_logger.info("Organizations " + organization);
|
||||
Organizations organization = buildOrganization(attributeMap,sr.getName(),sr.getNameInNamespace());
|
||||
if(organization != null) {
|
||||
orgsList.add(organization);
|
||||
maxLevel = (maxLevel < organization.getLevel()) ? organization.getLevel() : maxLevel ;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
for (int level = 2 ; level <= maxLevel ; level++) {
|
||||
for(Organizations organization : orgsList) {
|
||||
if(organization.getLevel() == level) {
|
||||
String parentNamePath= organization.getNamePath().substring(0, organization.getNamePath().lastIndexOf("/"));
|
||||
|
||||
if(orgsNamePathMap.get(organization.getNamePath())!=null) {
|
||||
_logger.info("org " + orgsNamePathMap.get(organization.getNamePath()).getNamePath()+" exists.");
|
||||
continue;
|
||||
}
|
||||
|
||||
Organizations parentOrg = orgsNamePathMap.get(parentNamePath);
|
||||
if(parentOrg == null ) {
|
||||
parentOrg = rootOrganization;
|
||||
}
|
||||
organization.setParentId(parentOrg.getId());
|
||||
organization.setParentName(parentOrg.getName());
|
||||
organization.setCodePath(parentOrg.getCodePath()+"/"+organization.getId());
|
||||
_logger.info("parentNamePath " + parentNamePath+" , namePah " + organization.getNamePath());
|
||||
|
||||
organizationsService.saveOrUpdate(organization);
|
||||
orgsNamePathMap.put(organization.getNamePath(), organization);
|
||||
|
||||
_logger.info("Organizations " + organization);
|
||||
HistorySynchronizer historySynchronizer =new HistorySynchronizer();
|
||||
historySynchronizer.setId(historySynchronizer.generateId());
|
||||
historySynchronizer.setSyncId(this.synchronizer.getId());
|
||||
historySynchronizer.setSyncName(this.synchronizer.getName());
|
||||
historySynchronizer.setObjectId(organization.getId());
|
||||
historySynchronizer.setObjectName(organization.getName());
|
||||
historySynchronizer.setObjectType(Organizations.class.getSimpleName());
|
||||
historySynchronizer.setInstId(synchronizer.getInstId());
|
||||
historySynchronizer.setResult("success");
|
||||
this.historySynchronizerService.insert(historySynchronizer);
|
||||
}
|
||||
}
|
||||
}
|
||||
//ldapUtils.close();
|
||||
} catch (NamingException e) {
|
||||
e.printStackTrace();
|
||||
@ -81,33 +127,23 @@ public class LdapOrganizationService extends AbstractSynchronizerService implem
|
||||
}
|
||||
|
||||
public Organizations buildOrganization(HashMap<String,Attribute> attributeMap,String name,String nameInNamespace) {
|
||||
Organizations org = new Organizations();
|
||||
org.setLdapDn(nameInNamespace);
|
||||
nameInNamespace = nameInNamespace.replaceAll(",ou=", "/").replaceAll("ou=", "/");
|
||||
nameInNamespace = nameInNamespace.substring(0, nameInNamespace.length() - ldapUtils.getBaseDN().length() - 1);
|
||||
String []namePaths = nameInNamespace.split("/");
|
||||
String namePah= "/"+rootOrganization.getName();
|
||||
for(int i = namePaths.length -1 ; i>=0 ;i--) {
|
||||
namePah = namePah + "/"+namePaths[i];
|
||||
}
|
||||
namePah = namePah.substring(0, namePah.length() -1);
|
||||
String parentNamePath= namePah.substring(0, namePah.lastIndexOf("/"));
|
||||
|
||||
if(orgsNamePathMap.get(namePah)!=null) {
|
||||
_logger.info("org " + orgsNamePathMap.get(namePah).getNamePath()+" exists.");
|
||||
return null;
|
||||
}
|
||||
|
||||
Organizations parentOrg = orgsNamePathMap.get(parentNamePath);
|
||||
org.setId(org.generateId());
|
||||
org.setNamePath(namePah);
|
||||
org.setParentId(parentOrg.getId());
|
||||
org.setParentName(parentOrg.getName());
|
||||
org.setCodePath(parentOrg.getCodePath()+"/"+org.getId());
|
||||
_logger.info("parentNamePath " + parentNamePath+" , namePah " + namePah);
|
||||
try {
|
||||
org.setName(LdapUtils.getAttributeStringValue(OrganizationalUnit.OU,attributeMap));
|
||||
Organizations org = new Organizations();
|
||||
org.setLdapDn(nameInNamespace);
|
||||
nameInNamespace = nameInNamespace.replaceAll(",ou=", "/").replaceAll("ou=", "/");
|
||||
nameInNamespace = nameInNamespace.substring(0, nameInNamespace.length() - ldapUtils.getBaseDN().length() - 1);
|
||||
String []namePaths = nameInNamespace.split("/");
|
||||
String namePah= "/"+rootOrganization.getName();
|
||||
for(int i = namePaths.length -1 ; i>=0 ;i--) {
|
||||
namePah = namePah + "/"+namePaths[i];
|
||||
}
|
||||
namePah = namePah.substring(0, namePah.length() -1);
|
||||
|
||||
org.setId(org.generateId());
|
||||
org.setCode(org.getId());
|
||||
org.setNamePath(namePah);
|
||||
org.setLevel(namePaths.length);
|
||||
org.setName(LdapUtils.getAttributeStringValue(OrganizationalUnit.OU,attributeMap));
|
||||
//org.setCountry(LdapUtils.getAttributeStringValue(OrganizationalUnit.CO,attributeMap));
|
||||
org.setRegion(LdapUtils.getAttributeStringValue(OrganizationalUnit.ST,attributeMap));
|
||||
org.setLocality(LdapUtils.getAttributeStringValue(OrganizationalUnit.L,attributeMap));
|
||||
@ -118,9 +154,8 @@ public class LdapOrganizationService extends AbstractSynchronizerService implem
|
||||
org.setFax(LdapUtils.getAttributeStringValue(OrganizationalUnit.FACSIMILETELEPHONENUMBER,attributeMap));
|
||||
org.setDescription(LdapUtils.getAttributeStringValue(OrganizationalUnit.DESCRIPTION,attributeMap));
|
||||
org.setInstId(this.synchronizer.getInstId());
|
||||
orgsNamePathMap.put(org.getNamePath(), org);
|
||||
org.setStatus(ConstsStatus.ACTIVE);
|
||||
_logger.info("org " + org);
|
||||
organizationsService.insert(org);
|
||||
HistorySynchronizer historySynchronizer =new HistorySynchronizer();
|
||||
historySynchronizer.setId(historySynchronizer.generateId());
|
||||
historySynchronizer.setSyncId(this.synchronizer.getId());
|
||||
@ -128,12 +163,14 @@ public class LdapOrganizationService extends AbstractSynchronizerService implem
|
||||
historySynchronizer.setObjectId(org.getId());
|
||||
historySynchronizer.setObjectName(org.getName());
|
||||
historySynchronizer.setObjectType(Organizations.class.getSimpleName());
|
||||
historySynchronizer.setInstId(synchronizer.getInstId());
|
||||
historySynchronizer.setResult("success");
|
||||
this.historySynchronizerService.insert(historySynchronizer);
|
||||
return org;
|
||||
} catch (NamingException e) {
|
||||
e.printStackTrace();
|
||||
_logger.error("NamingException " , e);
|
||||
}
|
||||
return org;
|
||||
return null;
|
||||
}
|
||||
|
||||
|
||||
|
||||
@ -23,6 +23,8 @@ import javax.naming.NamingException;
|
||||
import javax.naming.directory.Attribute;
|
||||
import javax.naming.directory.SearchControls;
|
||||
import javax.naming.directory.SearchResult;
|
||||
|
||||
import org.apache.commons.lang3.StringUtils;
|
||||
import org.maxkey.constants.ldap.InetOrgPerson;
|
||||
import org.maxkey.entity.HistorySynchronizer;
|
||||
import org.maxkey.entity.Organizations;
|
||||
@ -41,30 +43,33 @@ public class LdapUsersService extends AbstractSynchronizerService implements IS
|
||||
LdapUtils ldapUtils;
|
||||
|
||||
public void sync() {
|
||||
_logger.info("Sync Users...");
|
||||
_logger.info("Sync Ldap Users ...");
|
||||
loadOrgsById("1");
|
||||
try {
|
||||
SearchControls constraints = new SearchControls();
|
||||
constraints.setSearchScope(ldapUtils.getSearchScope());
|
||||
NamingEnumeration<SearchResult> results = ldapUtils.getConnection()
|
||||
.search(ldapUtils.getBaseDN(), "(&(objectClass=inetOrgPerson))", constraints);
|
||||
String filter = StringUtils.isNotBlank(this.getSynchronizer().getFilters()) ?
|
||||
getSynchronizer().getFilters() : "(&(objectClass=inetOrgPerson))";
|
||||
NamingEnumeration<SearchResult> results =
|
||||
ldapUtils.getConnection().search(ldapUtils.getBaseDN(), filter, constraints);
|
||||
|
||||
long recordCount = 0;
|
||||
while (null != results && results.hasMoreElements()) {
|
||||
Object obj = results.nextElement();
|
||||
if (obj instanceof SearchResult) {
|
||||
SearchResult si = (SearchResult) obj;
|
||||
_logger.trace("name " + si.getName());
|
||||
_logger.info("NameInNamespace " + si.getNameInNamespace());
|
||||
SearchResult sr = (SearchResult) obj;
|
||||
_logger.debug("Sync User {} , name {} , NameInNamespace {}" ,
|
||||
(++recordCount),sr.getName(),sr.getNameInNamespace());
|
||||
|
||||
HashMap<String,Attribute> attributeMap = new HashMap<String,Attribute>();
|
||||
NamingEnumeration<? extends Attribute> attrs = si.getAttributes().getAll();
|
||||
NamingEnumeration<? extends Attribute> attrs = sr.getAttributes().getAll();
|
||||
while (null != attrs && attrs.hasMoreElements()) {
|
||||
Attribute objAttrs = attrs.nextElement();
|
||||
_logger.trace("attribute "+objAttrs.getID() + " , " + objAttrs.get());
|
||||
attributeMap.put(objAttrs.getID(), objAttrs);
|
||||
}
|
||||
|
||||
UserInfo userInfo = buildUserInfo(attributeMap,si.getName(),si.getNameInNamespace());
|
||||
UserInfo userInfo = buildUserInfo(attributeMap,sr.getName(),sr.getNameInNamespace());
|
||||
userInfo.setPassword(userInfo.getUsername() + "Maxkey@888");
|
||||
userInfoService.saveOrUpdate(userInfo);
|
||||
_logger.info("userInfo " + userInfo);
|
||||
@ -141,24 +146,18 @@ public class LdapUsersService extends AbstractSynchronizerService implements IS
|
||||
userInfo.setTimeZone("Asia/Shanghai");
|
||||
userInfo.setStatus(1);
|
||||
userInfo.setInstId(this.synchronizer.getInstId());
|
||||
UserInfo quser=new UserInfo();
|
||||
quser.setUsername(userInfo.getUsername());
|
||||
UserInfo loadedUser=userInfoService.load(quser);
|
||||
if(loadedUser == null) {
|
||||
userInfo.setPassword(userInfo.generateId());
|
||||
userInfoService.insert(userInfo);
|
||||
HistorySynchronizer historySynchronizer =new HistorySynchronizer();
|
||||
historySynchronizer.setId(historySynchronizer.generateId());
|
||||
historySynchronizer.setSyncId(this.synchronizer.getId());
|
||||
historySynchronizer.setSyncName(this.synchronizer.getName());
|
||||
historySynchronizer.setObjectId(userInfo.getId());
|
||||
historySynchronizer.setObjectName(userInfo.getUsername());
|
||||
historySynchronizer.setObjectType(Organizations.class.getSimpleName());
|
||||
historySynchronizer.setResult("success");
|
||||
this.historySynchronizerService.insert(historySynchronizer);
|
||||
}else {
|
||||
_logger.info("username " + userInfo.getUsername()+" exists.");
|
||||
}
|
||||
|
||||
HistorySynchronizer historySynchronizer =new HistorySynchronizer();
|
||||
historySynchronizer.setId(historySynchronizer.generateId());
|
||||
historySynchronizer.setSyncId(this.synchronizer.getId());
|
||||
historySynchronizer.setSyncName(this.synchronizer.getName());
|
||||
historySynchronizer.setObjectId(userInfo.getId());
|
||||
historySynchronizer.setObjectName(userInfo.getUsername());
|
||||
historySynchronizer.setObjectType(Organizations.class.getSimpleName());
|
||||
historySynchronizer.setInstId(synchronizer.getInstId());
|
||||
historySynchronizer.setResult("success");
|
||||
this.historySynchronizerService.insert(historySynchronizer);
|
||||
|
||||
} catch (NamingException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
|
||||
@ -38,7 +38,7 @@ public class WorkweixinOrganizationService extends AbstractSynchronizerService i
|
||||
static String DEPTS_URL="https://qyapi.weixin.qq.com/cgi-bin/department/list?access_token=%s";
|
||||
|
||||
public void sync() {
|
||||
_logger.info("Sync Organizations ...");
|
||||
_logger.info("Sync Workweixin Organizations ...");
|
||||
|
||||
try {
|
||||
WorkWeixinDeptsResponse rsp = requestDepartmentList(access_token);
|
||||
|
||||
@ -42,7 +42,7 @@ public class WorkweixinUsersService extends AbstractSynchronizerService implemen
|
||||
static String USERS_URL="https://qyapi.weixin.qq.com/cgi-bin/user/list?access_token=%s&department_id=%s&fetch_child=0";
|
||||
|
||||
public void sync() {
|
||||
_logger.info("Sync Users...");
|
||||
_logger.info("Sync Workweixin Users...");
|
||||
try {
|
||||
List<Organizations> organizations =
|
||||
organizationsService.find("instid = ?",
|
||||
|
||||
@ -49,7 +49,7 @@ public abstract class AbstractSynchronizerService {
|
||||
protected Organizations rootOrganization = null;
|
||||
|
||||
|
||||
public void loadOrgsById(String orgId) {
|
||||
public HashMap<String,Organizations> loadOrgsById(String orgId) {
|
||||
List<Organizations> orgsList = organizationsService.query(null);
|
||||
if(orgId== null || orgId.equals("")) {
|
||||
orgId="1";
|
||||
@ -72,7 +72,7 @@ public abstract class AbstractSynchronizerService {
|
||||
push(orgsNamePathMap,orgsList,rootOrganization);
|
||||
|
||||
_logger.trace("orgsNamePathMap " + orgsNamePathMap);
|
||||
|
||||
return orgsNamePathMap;
|
||||
}
|
||||
|
||||
public void push(HashMap<String,Organizations> orgsNamePathMap,
|
||||
|
||||
@ -248,7 +248,7 @@ public class OrganizationsController {
|
||||
organization.setDivision(ExcelUtils.getValue(row, 8));
|
||||
// 级别
|
||||
String level = ExcelUtils.getValue(row, 9);
|
||||
organization.setLevel(level.equals("") ? "1" : level);
|
||||
organization.setLevel(level.equals("") ? 1 : Integer.parseInt(level));
|
||||
// 排序
|
||||
String sortIndex = ExcelUtils.getValue(row, 10);
|
||||
organization.setSortIndex(sortIndex.equals("") ? 1 : Integer.parseInt(sortIndex));
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user