141 lines
4.8 KiB
Java
141 lines
4.8 KiB
Java
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;
|
|
import javax.naming.NamingException;
|
|
import javax.naming.directory.Attribute;
|
|
import javax.naming.directory.Attributes;
|
|
import javax.naming.directory.BasicAttribute;
|
|
import javax.naming.directory.BasicAttributes;
|
|
import javax.naming.directory.DirContext;
|
|
import javax.naming.directory.InitialDirContext;
|
|
import javax.naming.directory.SearchControls;
|
|
import javax.naming.directory.SearchResult;
|
|
|
|
import org.gcube.common.portal.PortalContext;
|
|
import org.gcube.portal.custom.communitymanager.OrganizationsUtil;
|
|
import org.slf4j.Logger;
|
|
import org.slf4j.LoggerFactory;
|
|
|
|
import com.liferay.portal.kernel.exception.PortalException;
|
|
import com.liferay.portal.kernel.exception.SystemException;
|
|
import com.liferay.portal.model.Organization;
|
|
import com.liferay.portal.model.User;
|
|
import com.liferay.portal.service.OrganizationLocalServiceUtil;
|
|
import com.liferay.portal.service.UserLocalServiceUtil;
|
|
|
|
public class LDAPSync implements Runnable {
|
|
private static final Logger _log = LoggerFactory.getLogger(LDAPSync.class);
|
|
|
|
private ResourceBundle rb;
|
|
|
|
public LDAPSync() {
|
|
_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 {
|
|
users = getAllLiferayUsers();
|
|
_log.debug("Read " + users.size() + " from LR DB");
|
|
} catch (Exception e1) {
|
|
e1.printStackTrace();
|
|
}
|
|
|
|
_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");
|
|
env.put(Context.PROVIDER_URL, ldapUrl);
|
|
env.put(Context.SECURITY_PRINCIPAL, ldapPrincipal);
|
|
env.put(Context.SECURITY_CREDENTIALS, ldapPwd);
|
|
|
|
try {
|
|
DirContext ctx = new InitialDirContext(env);
|
|
_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("LDAP Sync done ... reading LDAP users now ..");
|
|
|
|
} catch (NamingException e) {
|
|
_log.error("Something went Wrong during LDAP Sync");
|
|
e.printStackTrace();
|
|
}
|
|
_log.info("LDAP Sync Completed OK!");
|
|
|
|
}
|
|
|
|
private String getSubContext(String username) {
|
|
return "uid="+username+",ou=People,o=Liferay,ou=Organizations,dc=d4science,dc=org";
|
|
}
|
|
|
|
|
|
private boolean checkIfLDAPUserExists(String username, DirContext ctx, String filter) throws NamingException {
|
|
SearchControls ctls = new SearchControls();
|
|
ctls.setSearchScope(SearchControls.SUBTREE_SCOPE);
|
|
NamingEnumeration<SearchResult> answer = ctx.search(getSubContext(username), filter, ctls);
|
|
return answer.hasMoreElements();
|
|
}
|
|
/**
|
|
*
|
|
* @param username
|
|
* @param name
|
|
* @param lastName
|
|
* @param email
|
|
* @param passwd
|
|
* @param ctx
|
|
* @throws NamingException
|
|
*/
|
|
private void updateUserInLDAP(String username, String name, String lastName, String email, String passwd, DirContext ctx, String filter) throws NamingException {
|
|
Attributes attributes=new BasicAttributes();
|
|
Attribute objectClass=new BasicAttribute("objectClass");
|
|
objectClass.add("inetOrgPerson");
|
|
attributes.put(objectClass);
|
|
|
|
Attribute sn = new BasicAttribute("sn");
|
|
Attribute cn = new BasicAttribute("cn");
|
|
Attribute mail = new BasicAttribute("mail");
|
|
Attribute userPassword = new BasicAttribute("userPassword");
|
|
|
|
sn.add(name);
|
|
cn.add(lastName);
|
|
mail.add(email);
|
|
userPassword.add(passwd);
|
|
|
|
attributes.put(sn);
|
|
attributes.put(cn);
|
|
attributes.put(mail);
|
|
attributes.put(userPassword);
|
|
|
|
if (checkIfLDAPUserExists(username, ctx, filter)) {
|
|
_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");
|
|
}
|
|
}
|
|
|
|
private List<User> getAllLiferayUsers() throws SystemException, PortalException {
|
|
String infraName = PortalContext.getConfiguration().getInfrastructureName();
|
|
_log.debug("Reading users belonging to: /" + infraName);
|
|
|
|
Organization rootInfra = OrganizationLocalServiceUtil.getOrganization(OrganizationsUtil.getCompany().getCompanyId(), infraName);
|
|
return UserLocalServiceUtil.getOrganizationUsers(rootInfra.getOrganizationId());
|
|
}
|
|
}
|