Synchronizer

This commit is contained in:
MaxKey
2021-05-23 21:10:54 +08:00
parent 97be43544a
commit 804bddf1a7
11 changed files with 552 additions and 99 deletions

View File

@@ -0,0 +1,115 @@
package org.maxkey.entity;
import java.io.Serializable;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Table;
import org.apache.mybatis.jpa.persistence.JpaBaseEntity;
@Entity
@Table(name = "MXK_HISTORY_SYNCHRONIZER")
public class HistorySynchronizer extends JpaBaseEntity implements Serializable{
/**
*
*/
private static final long serialVersionUID = -1184644499009162756L;
@Id
@Column
@GeneratedValue(strategy=GenerationType.AUTO,generator="snowflakeid")
String id;
@Column
String syncId;
@Column
String syncName;
@Column
String objectId;
@Column
String objectType;
@Column
String objectName;
String syncTime;
@Column
String result;
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getSyncId() {
return syncId;
}
public void setSyncId(String syncId) {
this.syncId = syncId;
}
public String getSyncName() {
return syncName;
}
public void setSyncName(String syncName) {
this.syncName = syncName;
}
public String getObjectId() {
return objectId;
}
public void setObjectId(String objectId) {
this.objectId = objectId;
}
public String getObjectType() {
return objectType;
}
public void setObjectType(String objectType) {
this.objectType = objectType;
}
public String getObjectName() {
return objectName;
}
public void setObjectName(String objectName) {
this.objectName = objectName;
}
public String getSyncTime() {
return syncTime;
}
public void setSyncTime(String syncTime) {
this.syncTime = syncTime;
}
public String getResult() {
return result;
}
public void setResult(String result) {
this.result = result;
}
public HistorySynchronizer() {
super();
}
@Override
public String toString() {
StringBuilder builder = new StringBuilder();
builder.append("HistorySynchronizer [id=");
builder.append(id);
builder.append(", syncId=");
builder.append(syncId);
builder.append(", syncName=");
builder.append(syncName);
builder.append(", objectId=");
builder.append(objectId);
builder.append(", objectType=");
builder.append(objectType);
builder.append(", objectName=");
builder.append(objectName);
builder.append(", syncTime=");
builder.append(syncTime);
builder.append(", result=");
builder.append(result);
builder.append("]");
return builder.toString();
}
}

View File

@@ -0,0 +1,129 @@
package org.maxkey.synchronizer;
import java.util.HashMap;
import java.util.List;
import org.maxkey.entity.Organizations;
import org.maxkey.entity.Synchronizers;
import org.maxkey.persistence.service.HistorySynchronizerService;
import org.maxkey.persistence.service.OrganizationsService;
import org.maxkey.persistence.service.UserInfoService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
public abstract class AbstractSynchronizerService {
private static final Logger _logger =
LoggerFactory.getLogger(AbstractSynchronizerService.class);
@Autowired
protected OrganizationsService organizationsService;
@Autowired
protected UserInfoService userInfoService;
@Autowired
protected HistorySynchronizerService historySynchronizerService;
protected Synchronizers synchronizer;
protected HashMap<String,Organizations> orgsNamePathMap;
protected Organizations rootOrganization = null;
public void loadOrgsById(String orgId) {
List<Organizations> orgsList = organizationsService.query(null);
if(orgId== null || orgId.equals("")) {
orgId="1";
}
for(Organizations org : orgsList) {
if(org.getId().equals(orgId)&&orgId.equals("1")) {
rootOrganization = org;
rootOrganization.setNamePath("/"+rootOrganization.getName());
rootOrganization.setCodePath("/1");
rootOrganization.setParentId("-1");
rootOrganization.setParentName("");
}else if(org.getId().equals(orgId)){
rootOrganization = org;
}
}
orgsNamePathMap =new HashMap<String,Organizations>();
orgsNamePathMap.put(rootOrganization.getNamePath(), rootOrganization);
push(orgsNamePathMap,orgsList,rootOrganization);
_logger.trace("orgsNamePathMap " + orgsNamePathMap);
}
public void push(HashMap<String,Organizations> orgsNamePathMap,
List<Organizations> orgsList,
Organizations parentOrg) {
for(Organizations org : orgsList) {
if(org.getParentId().equals(parentOrg.getId())) {
if(org.getNamePath() == null
|| !org.getNamePath().equals(parentOrg.getNamePath()+"/"+org.getName())) {
org.setParentName(parentOrg.getName());
org.setNamePath(parentOrg.getNamePath()+"/"+org.getName());
org.setCodePath(parentOrg.getCodePath()+"/"+org.getId());
organizationsService.update(org);
}
orgsNamePathMap.put(org.getNamePath(), org);
push(orgsNamePathMap,orgsList,org);
}
}
return;
}
public OrganizationsService getOrganizationsService() {
return organizationsService;
}
public void setOrganizationsService(OrganizationsService organizationsService) {
this.organizationsService = organizationsService;
}
public UserInfoService getUserInfoService() {
return userInfoService;
}
public void setUserInfoService(UserInfoService userInfoService) {
this.userInfoService = userInfoService;
}
public HashMap<String, Organizations> getOrgsNamePathMap() {
return orgsNamePathMap;
}
public void setOrgsNamePathMap(HashMap<String, Organizations> orgsNamePathMap) {
this.orgsNamePathMap = orgsNamePathMap;
}
public Organizations getRootOrganization() {
return rootOrganization;
}
public void setRootOrganization(Organizations rootOrganization) {
this.rootOrganization = rootOrganization;
}
public Synchronizers getSynchronizer() {
return synchronizer;
}
public void setSynchronizer(Synchronizers synchronizer) {
this.synchronizer = synchronizer;
}
public HistorySynchronizerService getHistorySynchronizerService() {
return historySynchronizerService;
}
public void setHistorySynchronizerService(HistorySynchronizerService historySynchronizerService) {
this.historySynchronizerService = historySynchronizerService;
}
}

View File

@@ -24,27 +24,24 @@ import javax.naming.directory.Attribute;
import javax.naming.directory.SearchControls;
import javax.naming.directory.SearchResult;
import org.maxkey.constants.ldap.OrganizationalUnit;
import org.maxkey.entity.HistorySynchronizer;
import org.maxkey.entity.Organizations;
import org.maxkey.entity.Synchronizers;
import org.maxkey.persistence.ldap.ActiveDirectoryUtils;
import org.maxkey.persistence.ldap.LdapUtils;
import org.maxkey.persistence.service.OrganizationsService;
import org.maxkey.synchronizer.AbstractSynchronizerService;
import org.maxkey.synchronizer.ISynchronizerService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
@Service
public class ActiveDirectoryOrganizationService implements ISynchronizerService{
public class ActiveDirectoryOrganizationService extends AbstractSynchronizerService implements ISynchronizerService{
final static Logger _logger = LoggerFactory.getLogger(ActiveDirectoryOrganizationService.class);
ActiveDirectoryUtils ldapUtils;
@Autowired
OrganizationsService organizationsService;
public void sync() {
loadOrgsById("1");
_logger.info("Sync Organizations ...");
try {
SearchControls constraints = new SearchControls();
@@ -85,8 +82,35 @@ public class ActiveDirectoryOrganizationService implements ISynchronizerServic
}
public Organizations buildOrganization(HashMap<String,Attribute> attributeMap,String name,String nameInNamespace) {
Organizations org = new Organizations();
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 {
org.setName(LdapUtils.getAttributeStringValue(OrganizationalUnit.OU,attributeMap));
@@ -96,13 +120,25 @@ public class ActiveDirectoryOrganizationService implements ISynchronizerServic
org.setStreet(LdapUtils.getAttributeStringValue(OrganizationalUnit.STREET,attributeMap));
org.setPostalCode(LdapUtils.getAttributeStringValue(OrganizationalUnit.POSTALCODE,attributeMap));
org.setDescription(LdapUtils.getAttributeStringValue(OrganizationalUnit.DESCRIPTION,attributeMap));
orgsNamePathMap.put(org.getNamePath(), org);
_logger.info("org " + org);
organizationsService.insert(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);
} catch (NamingException e) {
e.printStackTrace();
}
return org;
}
public ActiveDirectoryUtils getLdapUtils() {
return ldapUtils;
@@ -112,18 +148,5 @@ public class ActiveDirectoryOrganizationService implements ISynchronizerServic
this.ldapUtils = ldapUtils;
}
public OrganizationsService getOrganizationsService() {
return organizationsService;
}
public void setOrganizationsService(OrganizationsService organizationsService) {
this.organizationsService = organizationsService;
}
@Override
public void setSynchronizer(Synchronizers Synchronizer) {
// TODO Auto-generated method stub
}
}

View File

@@ -18,6 +18,7 @@
package org.maxkey.synchronizer.activedirectory;
import org.maxkey.entity.Synchronizers;
import org.maxkey.persistence.ldap.ActiveDirectoryUtils;
import org.maxkey.synchronizer.ISynchronizerService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@@ -42,11 +43,23 @@ public class ActiveDirectorySynchronizerService implements ISynchronizerServic
public void sync() {
_logger.info("Sync ...");
ActiveDirectoryUtils ldapUtils = new ActiveDirectoryUtils(
synchronizer.getProviderUrl(),
synchronizer.getPrincipal(),
synchronizer.getCredentials(),
synchronizer.getBasedn(),
synchronizer.getMsadDomain());
ldapUtils.openConnection();
activeDirectoryOrganizationService.setSynchronizer(synchronizer);
activeDirectoryOrganizationService.setLdapUtils(ldapUtils);
activeDirectoryOrganizationService.sync();
activeDirectoryUsersService.setSynchronizer(synchronizer);
activeDirectoryUsersService.setLdapUtils(ldapUtils);
activeDirectoryUsersService.sync();
ldapUtils.close();
}
public ActiveDirectoryUsersService getActiveDirectoryUsersService() {

View File

@@ -24,29 +24,26 @@ import javax.naming.directory.Attribute;
import javax.naming.directory.SearchControls;
import javax.naming.directory.SearchResult;
import org.maxkey.constants.ldap.ActiveDirectoryUser;
import org.maxkey.entity.Synchronizers;
import org.maxkey.entity.HistorySynchronizer;
import org.maxkey.entity.Organizations;
import org.maxkey.entity.UserInfo;
import org.maxkey.persistence.ldap.ActiveDirectoryUtils;
import org.maxkey.persistence.ldap.LdapUtils;
import org.maxkey.persistence.service.UserInfoService;
import org.maxkey.synchronizer.AbstractSynchronizerService;
import org.maxkey.synchronizer.ISynchronizerService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
@Service
public class ActiveDirectoryUsersService implements ISynchronizerService{
public class ActiveDirectoryUsersService extends AbstractSynchronizerService implements ISynchronizerService{
final static Logger _logger = LoggerFactory.getLogger(ActiveDirectoryUsersService.class);
ActiveDirectoryUtils ldapUtils;
@Autowired
UserInfoService userInfoService;
public void sync() {
_logger.info("Sync Users...");
loadOrgsById("1");
try {
SearchControls constraints = new SearchControls();
constraints.setSearchScope(ldapUtils.getSearchScope());
@@ -90,9 +87,29 @@ public class ActiveDirectoryUsersService implements ISynchronizerService{
}
public UserInfo buildUserInfo(HashMap<String,Attribute> attributeMap,String name,String nameInNamespace) {
UserInfo userInfo = new UserInfo();
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=", "/");
nameInNamespace = nameInNamespace.substring(0, nameInNamespace.length() - ldapUtils.getBaseDN().length() - 1);
_logger.info("nameInNamespace " + nameInNamespace);
String []namePaths = nameInNamespace.split("/");
String namePah= "/"+rootOrganization.getName();
for(int i = namePaths.length -1 ; i>=1 ;i--) {
namePah = namePah + "/"+namePaths[i];
}
//namePah = namePah.substring(0, namePah.length());
String deptNamePath= namePah.substring(0, namePah.lastIndexOf("/"));
_logger.info("deptNamePath " + deptNamePath);
Organizations deptOrg = orgsNamePathMap.get(deptNamePath);
userInfo.setDepartment(deptOrg.getName());
userInfo.setDepartmentId(deptOrg.getId());
try {
userInfo.setId(userInfo.generateId());
userInfo.setFormattedName(LdapUtils.getAttributeStringValue(ActiveDirectoryUser.CN,attributeMap));//閸忋劌鎮<E58A8C>
//鐠愶附鍩<E99984>
userInfo.setUsername(LdapUtils.getAttributeStringValue(ActiveDirectoryUser.SAMACCOUNTNAME,attributeMap));//鐠愶箑褰<E7AE91>
@@ -117,15 +134,42 @@ public class ActiveDirectoryUsersService implements ISynchronizerService{
userInfo.setWorkPostalCode(LdapUtils.getAttributeStringValue(ActiveDirectoryUser.POSTALCODE,attributeMap));//闁喚绱<E5969A>
userInfo.setWorkAddressFormatted(LdapUtils.getAttributeStringValue(ActiveDirectoryUser.POSTOFFICEBOX,attributeMap));//闁喗鏂傞柇顔绢唸
userInfo.setMobile(LdapUtils.getAttributeStringValue(ActiveDirectoryUser.MOBILE,attributeMap));//閹靛婧<EE8386>
if(LdapUtils.getAttributeStringValue(ActiveDirectoryUser.MOBILE,attributeMap).equals("")) {
userInfo.setMobile(userInfo.getId());
}else {
userInfo.setMobile(LdapUtils.getAttributeStringValue(ActiveDirectoryUser.MOBILE,attributeMap));//閹靛婧<EE8386>
}
userInfo.setHomePhoneNumber(LdapUtils.getAttributeStringValue(ActiveDirectoryUser.HOMEPHONE,attributeMap));//鐎硅泛娑甸悽浣冪樈
userInfo.setWorkFax(LdapUtils.getAttributeStringValue(ActiveDirectoryUser.FACSIMILETELEPHONENUMBER,attributeMap));//娴肩姷婀<E5A7B7>
userInfo.setHomeAddressFormatted(LdapUtils.getAttributeStringValue(ActiveDirectoryUser.INFO,attributeMap));//閻絻鐦芥径鍥ㄦ暈
userInfo.setDivision(LdapUtils.getAttributeStringValue(ActiveDirectoryUser.COMPANY,attributeMap)); //閸忣剙寰<E58999>
userInfo.setDepartment(LdapUtils.getAttributeStringValue(ActiveDirectoryUser.DEPARTMENT,attributeMap)); //闁劑妫<E58A91>
//userInfo.setDepartment(LdapUtils.getAttributeStringValue(ActiveDirectoryUser.DEPARTMENT,attributeMap)); //闁劑妫<E58A91>
//userInfo.setDepartmentId(LdapUtils.getAttributeStringValue(ActiveDirectoryUser.DEPARTMENT,attributeMap)); //闁劑妫紓鏍у娇
userInfo.setJobTitle(LdapUtils.getAttributeStringValue(ActiveDirectoryUser.TITLE,attributeMap));//閼卞苯濮<E88BAF>
userInfo.setUserState("RESIDENT");
userInfo.setUserType("EMPLOYEE");
userInfo.setTimeZone("Asia/Shanghai");
userInfo.setStatus(1);
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.");
}
} catch (NamingException e) {
e.printStackTrace();
@@ -140,20 +184,5 @@ public class ActiveDirectoryUsersService implements ISynchronizerService{
public void setLdapUtils(ActiveDirectoryUtils ldapUtils) {
this.ldapUtils = ldapUtils;
}
public UserInfoService getUserInfoService() {
return userInfoService;
}
public void setUserInfoService(UserInfoService userInfoService) {
this.userInfoService = userInfoService;
}
@Override
public void setSynchronizer(Synchronizers Synchronizer) {
// TODO Auto-generated method stub
}
}

View File

@@ -25,29 +25,24 @@ import javax.naming.directory.Attribute;
import javax.naming.directory.SearchControls;
import javax.naming.directory.SearchResult;
import org.maxkey.constants.ldap.OrganizationalUnit;
import org.maxkey.entity.HistorySynchronizer;
import org.maxkey.entity.Organizations;
import org.maxkey.entity.Synchronizers;
import org.maxkey.persistence.ldap.LdapUtils;
import org.maxkey.persistence.service.OrganizationsService;
import org.maxkey.synchronizer.AbstractSynchronizerService;
import org.maxkey.synchronizer.ISynchronizerService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
@Service
public class LdapOrganizationService implements ISynchronizerService{
public class LdapOrganizationService extends AbstractSynchronizerService implements ISynchronizerService{
final static Logger _logger = LoggerFactory.getLogger(LdapOrganizationService.class);
LdapUtils ldapUtils;
@Autowired
OrganizationsService organizationsService;
public void sync() {
_logger.info("Sync Organizations ...");
_logger.info("Sync Organizations ...");
loadOrgsById("1");
try {
SearchControls constraints = new SearchControls();
constraints.setSearchScope(ldapUtils.getSearchScope());
@@ -88,6 +83,28 @@ public class LdapOrganizationService implements ISynchronizerService{
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));
@@ -100,7 +117,18 @@ public class LdapOrganizationService implements ISynchronizerService{
org.setPhone(LdapUtils.getAttributeStringValue(OrganizationalUnit.TELEPHONENUMBER,attributeMap));
org.setFax(LdapUtils.getAttributeStringValue(OrganizationalUnit.FACSIMILETELEPHONENUMBER,attributeMap));
org.setDescription(LdapUtils.getAttributeStringValue(OrganizationalUnit.DESCRIPTION,attributeMap));
orgsNamePathMap.put(org.getNamePath(), org);
_logger.info("org " + org);
organizationsService.insert(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);
} catch (NamingException e) {
e.printStackTrace();
}
@@ -115,20 +143,6 @@ public class LdapOrganizationService implements ISynchronizerService{
public void setLdapUtils(LdapUtils ldapUtils) {
this.ldapUtils = ldapUtils;
}
public OrganizationsService getOrganizationsService() {
return organizationsService;
}
public void setOrganizationsService(OrganizationsService organizationsService) {
this.organizationsService = organizationsService;
}
@Override
public void setSynchronizer(Synchronizers Synchronizer) {
// TODO Auto-generated method stub
}
}

View File

@@ -18,6 +18,7 @@
package org.maxkey.synchronizer.ldap;
import org.maxkey.entity.Synchronizers;
import org.maxkey.persistence.ldap.LdapUtils;
import org.maxkey.synchronizer.ISynchronizerService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@@ -42,8 +43,22 @@ public class LdapSynchronizerService implements ISynchronizerService{
public void sync() {
_logger.info("Sync ...");
LdapUtils ldapUtils = new LdapUtils(
synchronizer.getProviderUrl(),
synchronizer.getPrincipal(),
synchronizer.getCredentials(),
synchronizer.getBasedn());
ldapUtils.openConnection();
ldapOrganizationService.setSynchronizer(synchronizer);
ldapUsersService.setSynchronizer(synchronizer);
ldapOrganizationService.setLdapUtils(ldapUtils);
ldapUsersService.setLdapUtils(ldapUtils);
ldapOrganizationService.sync();
ldapUsersService.sync();
ldapUtils.close();
}
public LdapUsersService getLdapUsersService() {

View File

@@ -24,27 +24,25 @@ import javax.naming.directory.Attribute;
import javax.naming.directory.SearchControls;
import javax.naming.directory.SearchResult;
import org.maxkey.constants.ldap.InetOrgPerson;
import org.maxkey.entity.Synchronizers;
import org.maxkey.entity.HistorySynchronizer;
import org.maxkey.entity.Organizations;
import org.maxkey.entity.UserInfo;
import org.maxkey.persistence.ldap.LdapUtils;
import org.maxkey.persistence.service.UserInfoService;
import org.maxkey.synchronizer.AbstractSynchronizerService;
import org.maxkey.synchronizer.ISynchronizerService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
@Service
public class LdapUsersService implements ISynchronizerService{
public class LdapUsersService extends AbstractSynchronizerService implements ISynchronizerService{
final static Logger _logger = LoggerFactory.getLogger(LdapUsersService.class);
LdapUtils ldapUtils;
@Autowired
UserInfoService userInfoService;
public void sync() {
_logger.info("Sync Users...");
loadOrgsById("1");
try {
SearchControls constraints = new SearchControls();
constraints.setSearchScope(ldapUtils.getSearchScope());
@@ -85,8 +83,23 @@ public class LdapUsersService implements ISynchronizerService{
public UserInfo buildUserInfo(HashMap<String,Attribute> attributeMap,String name,String nameInNamespace) {
UserInfo userInfo = new UserInfo();
userInfo.setLdapDn(nameInNamespace);
nameInNamespace = nameInNamespace.replaceAll(",ou=", "/").replaceAll("ou=", "/").replaceAll("uid=", "/").replaceAll("cn=", "/");
nameInNamespace = nameInNamespace.substring(0, nameInNamespace.length() - ldapUtils.getBaseDN().length() - 1);
_logger.info("nameInNamespace " + nameInNamespace);
String []namePaths = nameInNamespace.split("/");
String namePah= "/"+rootOrganization.getName();
for(int i = namePaths.length -1 ; i>=1 ;i--) {
namePah = namePah + "/"+namePaths[i];
}
//namePah = namePah.substring(0, namePah.length());
String deptNamePath= namePah.substring(0, namePah.lastIndexOf("/"));
_logger.info("deptNamePath " + deptNamePath);
Organizations deptOrg = orgsNamePathMap.get(deptNamePath);
userInfo.setDepartment(deptOrg.getName());
userInfo.setDepartmentId(deptOrg.getId());
try {
userInfo.setId(userInfo.generateId());
userInfo.setFormattedName(LdapUtils.getAttributeStringValue(InetOrgPerson.CN,attributeMap));//閸忋劌鎮<E58A8C>
//鐠愶附鍩<E99984>
userInfo.setUsername(LdapUtils.getAttributeStringValue(InetOrgPerson.UID,attributeMap));//鐠愶箑褰<E7AE91>
@@ -97,8 +110,8 @@ public class LdapUsersService implements ISynchronizerService{
userInfo.setDisplayName(LdapUtils.getAttributeStringValue(InetOrgPerson.DISPLAYNAME,attributeMap));//閺勫墽銇氶崥宥囆<E5AEA5>
userInfo.setEmployeeNumber(LdapUtils.getAttributeStringValue(InetOrgPerson.EMPLOYEENUMBER,attributeMap));
userInfo.setDepartment(LdapUtils.getAttributeStringValue(InetOrgPerson.OU,attributeMap));
userInfo.setDepartmentId(LdapUtils.getAttributeStringValue(InetOrgPerson.DEPARTMENTNUMBER,attributeMap));
//userInfo.setDepartment(LdapUtils.getAttributeStringValue(InetOrgPerson.OU,attributeMap));
//userInfo.setDepartmentId(LdapUtils.getAttributeStringValue(InetOrgPerson.DEPARTMENTNUMBER,attributeMap));
userInfo.setJobTitle(LdapUtils.getAttributeStringValue(InetOrgPerson.TITLE,attributeMap));//閼卞苯濮<E88BAF>
userInfo.setWorkOfficeName(LdapUtils.getAttributeStringValue(InetOrgPerson.PHYSICALDELIVERYOFFICENAME,attributeMap));//閸旂偛鍙曠<E98D99>癸拷
userInfo.setWorkEmail(LdapUtils.getAttributeStringValue(InetOrgPerson.MAIL,attributeMap));//闁喕娆<E59695>
@@ -112,12 +125,37 @@ public class LdapUsersService implements ISynchronizerService{
userInfo.setHomePhoneNumber(LdapUtils.getAttributeStringValue(InetOrgPerson.HOMEPHONE,attributeMap));//鐎硅泛娑甸悽浣冪樈
userInfo.setHomeAddressFormatted(LdapUtils.getAttributeStringValue(InetOrgPerson.HOMEPOSTALADDRESS,attributeMap));//閻絻鐦芥径鍥ㄦ暈
userInfo.setMobile(LdapUtils.getAttributeStringValue(InetOrgPerson.MOBILE,attributeMap));//閹靛婧<EE8386>
if(LdapUtils.getAttributeStringValue(InetOrgPerson.MOBILE,attributeMap).equals("")) {
userInfo.setMobile(userInfo.getId());
}else {
userInfo.setMobile(LdapUtils.getAttributeStringValue(InetOrgPerson.MOBILE,attributeMap));//閹靛婧<EE8386>
}
userInfo.setPreferredLanguage(LdapUtils.getAttributeStringValue(InetOrgPerson.PREFERREDLANGUAGE,attributeMap));//鐠囶叀鈻<E58F80>
userInfo.setDescription(LdapUtils.getAttributeStringValue(InetOrgPerson.DESCRIPTION,attributeMap));//閹诲繗鍫<E7B997>
userInfo.setUserState("RESIDENT");
userInfo.setUserType("EMPLOYEE");
userInfo.setTimeZone("Asia/Shanghai");
userInfo.setStatus(1);
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.");
}
} catch (NamingException e) {
e.printStackTrace();
}
@@ -132,20 +170,5 @@ public class LdapUsersService implements ISynchronizerService{
public void setLdapUtils(LdapUtils ldapUtils) {
this.ldapUtils = ldapUtils;
}
public UserInfoService getUserInfoService() {
return userInfoService;
}
public void setUserInfoService(UserInfoService userInfoService) {
this.userInfoService = userInfoService;
}
@Override
public void setSynchronizer(Synchronizers Synchronizer) {
// TODO Auto-generated method stub
}
}

View File

@@ -0,0 +1,30 @@
/*
* Copyright [2020] [MaxKey of copyright http://www.maxkey.top]
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.maxkey.persistence.mapper;
import org.apache.mybatis.jpa.persistence.IJpaBaseMapper;
import org.maxkey.entity.HistorySynchronizer;
/**
* @author Crystal.sea
*
*/
public interface HistorySynchronizerMapper extends IJpaBaseMapper<HistorySynchronizer> {
}

View File

@@ -0,0 +1,40 @@
/*
* Copyright [2020] [MaxKey of copyright http://www.maxkey.top]
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.maxkey.persistence.service;
import org.apache.mybatis.jpa.persistence.JpaBaseService;
import org.maxkey.entity.HistorySynchronizer;
import org.maxkey.persistence.mapper.HistorySynchronizerMapper;
import org.springframework.stereotype.Repository;
@Repository
public class HistorySynchronizerService extends JpaBaseService<HistorySynchronizer>{
public HistorySynchronizerService() {
super(HistorySynchronizerMapper.class);
}
/* (non-Javadoc)
* @see com.connsec.db.service.BaseService#getMapper()
*/
@Override
public HistorySynchronizerMapper getMapper() {
// TODO Auto-generated method stub
return (HistorySynchronizerMapper)super.getMapper();
}
}

View File

@@ -0,0 +1,22 @@
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="org.maxkey.persistence.mapper.HistorySynchronizerMapper" >
<sql id="where_statement">
<if test="id != null and id != ''">
and id = #{id}
</if>
</sql>
<select id="queryPageResults" parameterType="HistorySynchronizer" resultType="HistorySynchronizer">
select
*
from mxk_history_synchronizer
where 1 = 1
<include refid="where_statement"/>
order by synctime desc
</select>
</mapper>