|
|
|
@ -2,7 +2,6 @@ package org.gcube.portal.ldapexport;
|
|
|
|
|
|
|
|
|
|
import java.util.List;
|
|
|
|
|
import java.util.Properties;
|
|
|
|
|
import java.util.ResourceBundle;
|
|
|
|
|
|
|
|
|
|
import javax.naming.Context;
|
|
|
|
|
import javax.naming.NamingEnumeration;
|
|
|
|
@ -31,15 +30,18 @@ import com.liferay.portal.service.UserLocalServiceUtil;
|
|
|
|
|
public class LDAPSync implements Runnable {
|
|
|
|
|
private static final Logger _log = LoggerFactory.getLogger(LDAPSync.class);
|
|
|
|
|
|
|
|
|
|
private ResourceBundle rb;
|
|
|
|
|
private static final String ldapUrl = "ldap://ldap-liferay.d4science.org";
|
|
|
|
|
private static final String ldapPrincipal = "cn=admin,dc=d4science,dc=org";
|
|
|
|
|
private static final String ldapPwd = "poiwefhaewfkhj";
|
|
|
|
|
private static final String filter = "(objectClass=inetOrgPerson)";
|
|
|
|
|
|
|
|
|
|
public LDAPSync() {
|
|
|
|
|
super();
|
|
|
|
|
_log.debug("LDAPSync()");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
public void run() {
|
|
|
|
|
ResourceBundle rb = ResourceBundle.getBundle("org.gcube.portal.settings");
|
|
|
|
|
_log.debug("Reading Portal Users ...");
|
|
|
|
|
List<User> users = null;
|
|
|
|
|
try {
|
|
|
|
@ -50,10 +52,7 @@ public class LDAPSync implements Runnable {
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
_log.debug("Initializing LDAP exporter ...");
|
|
|
|
|
String ldapUrl = rb.getString("LDAP_PROVIDER_URL");
|
|
|
|
|
String ldapPrincipal = rb.getString("LDAP_SECURITY_PRINCIPAL");
|
|
|
|
|
String ldapPwd = rb.getString("LDAP_SECURITY_CREDENTIALS");
|
|
|
|
|
String filter = rb.getString("LDAP_FILTER");
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Properties env = new Properties();
|
|
|
|
|
env.put(Context.INITIAL_CONTEXT_FACTORY,"com.sun.jndi.ldap.LdapCtxFactory");
|
|
|
|
@ -66,9 +65,9 @@ public class LDAPSync implements Runnable {
|
|
|
|
|
_log.debug("Initiating LDAP Sync ...");
|
|
|
|
|
for (User user : users) {
|
|
|
|
|
updateUserInLDAP(user.getScreenName(), user.getFirstName(), user.getLastName(), user.getEmailAddress(), "{SHA}"+user.getPassword(), ctx, filter);
|
|
|
|
|
_log.debug("Updated " + user.getScreenName());
|
|
|
|
|
//_log.debug("Updated " + user.getScreenName());
|
|
|
|
|
}
|
|
|
|
|
_log.debug("LDAP Sync done ... reading LDAP users now ..");
|
|
|
|
|
_log.debug("LDAP Sync cycle done");
|
|
|
|
|
|
|
|
|
|
} catch (NamingException e) {
|
|
|
|
|
_log.error("Something went Wrong during LDAP Sync");
|
|
|
|
@ -83,10 +82,16 @@ public class LDAPSync implements Runnable {
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private boolean checkIfLDAPUserExists(String username, DirContext ctx, String filter) throws NamingException {
|
|
|
|
|
private boolean checkIfLDAPUserExists(String username, DirContext ctx, String filter) {
|
|
|
|
|
SearchControls ctls = new SearchControls();
|
|
|
|
|
ctls.setSearchScope(SearchControls.SUBTREE_SCOPE);
|
|
|
|
|
NamingEnumeration<SearchResult> answer = ctx.search(getSubContext(username), filter, ctls);
|
|
|
|
|
NamingEnumeration<SearchResult> answer;
|
|
|
|
|
try {
|
|
|
|
|
answer = ctx.search(getSubContext(username), filter, ctls);
|
|
|
|
|
} catch (NamingException e) {
|
|
|
|
|
_log.info("user: " + username + " not found checkIfLDAPUserExists returning false");
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
return answer.hasMoreElements();
|
|
|
|
|
}
|
|
|
|
|
/**
|
|
|
|
@ -121,12 +126,12 @@ public class LDAPSync implements Runnable {
|
|
|
|
|
attributes.put(userPassword);
|
|
|
|
|
|
|
|
|
|
if (checkIfLDAPUserExists(username, ctx, filter)) {
|
|
|
|
|
_log.debug("User " + username + " already exists, replacing attributes");
|
|
|
|
|
//_log.debug("User " + username + " already exists, replacing attributes");
|
|
|
|
|
ctx.modifyAttributes(getSubContext(username), DirContext.REPLACE_ATTRIBUTE, attributes);
|
|
|
|
|
}
|
|
|
|
|
else {
|
|
|
|
|
ctx.createSubcontext(getSubContext(username),attributes);
|
|
|
|
|
_log.debug("New User with uid=" + username + " created");
|
|
|
|
|
_log.debug("New User Found with uid=" + username + " created");
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|