|
|
|
@ -1,9 +1,5 @@
|
|
|
|
|
package org.gcube.portal.ldapexport;
|
|
|
|
|
|
|
|
|
|
import java.io.IOException;
|
|
|
|
|
import java.sql.Connection;
|
|
|
|
|
import java.sql.DriverManager;
|
|
|
|
|
import java.sql.SQLException;
|
|
|
|
|
import java.util.List;
|
|
|
|
|
import java.util.Properties;
|
|
|
|
|
import java.util.ResourceBundle;
|
|
|
|
@ -17,13 +13,8 @@ import javax.naming.directory.BasicAttribute;
|
|
|
|
|
import javax.naming.directory.BasicAttributes;
|
|
|
|
|
import javax.naming.directory.DirContext;
|
|
|
|
|
import javax.naming.directory.InitialDirContext;
|
|
|
|
|
import javax.naming.directory.ModificationItem;
|
|
|
|
|
import javax.naming.directory.SearchControls;
|
|
|
|
|
import javax.naming.directory.SearchResult;
|
|
|
|
|
import javax.servlet.ServletException;
|
|
|
|
|
import javax.servlet.http.HttpServlet;
|
|
|
|
|
import javax.servlet.http.HttpServletRequest;
|
|
|
|
|
import javax.servlet.http.HttpServletResponse;
|
|
|
|
|
|
|
|
|
|
import org.gcube.common.portal.PortalContext;
|
|
|
|
|
import org.gcube.portal.custom.communitymanager.OrganizationsUtil;
|
|
|
|
@ -32,35 +23,23 @@ import org.slf4j.LoggerFactory;
|
|
|
|
|
|
|
|
|
|
import com.liferay.portal.kernel.exception.PortalException;
|
|
|
|
|
import com.liferay.portal.kernel.exception.SystemException;
|
|
|
|
|
import com.liferay.portal.kernel.util.PropsUtil;
|
|
|
|
|
import com.liferay.portal.model.Organization;
|
|
|
|
|
import com.liferay.portal.model.User;
|
|
|
|
|
import com.liferay.portal.service.GroupLocalServiceUtil;
|
|
|
|
|
import com.liferay.portal.service.OrganizationLocalServiceUtil;
|
|
|
|
|
import com.liferay.portal.service.UserLocalServiceUtil;
|
|
|
|
|
|
|
|
|
|
@SuppressWarnings("serial")
|
|
|
|
|
public class LDAPExporter extends HttpServlet {
|
|
|
|
|
public class LDAPSync implements Runnable {
|
|
|
|
|
private static final Logger _log = LoggerFactory.getLogger(LDAPSync.class);
|
|
|
|
|
|
|
|
|
|
private static final Logger _log = LoggerFactory.getLogger(LDAPExporter.class);
|
|
|
|
|
private ResourceBundle rb;
|
|
|
|
|
|
|
|
|
|
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
|
|
|
|
|
sync();
|
|
|
|
|
public LDAPSync() {
|
|
|
|
|
_log.debug("LDAPSync()");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {}
|
|
|
|
|
|
|
|
|
|
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());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public void sync() {
|
|
|
|
|
ResourceBundle rb = ResourceBundle.getBundle("org.gcube.portal.ldapexport.settings");
|
|
|
|
|
@Override
|
|
|
|
|
public void run() {
|
|
|
|
|
ResourceBundle rb = ResourceBundle.getBundle("org.gcube.portal.settings");
|
|
|
|
|
_log.debug("Reading Portal Users ...");
|
|
|
|
|
List<User> users = null;
|
|
|
|
|
try {
|
|
|
|
@ -71,7 +50,6 @@ public class LDAPExporter extends HttpServlet {
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
_log.debug("Initializing LDAP exporter ...");
|
|
|
|
|
String baseDN = rb.getString("LDAP_BASE_DN");
|
|
|
|
|
String ldapUrl = rb.getString("LDAP_PROVIDER_URL");
|
|
|
|
|
String ldapPrincipal = rb.getString("LDAP_SECURITY_PRINCIPAL");
|
|
|
|
|
String ldapPwd = rb.getString("LDAP_SECURITY_CREDENTIALS");
|
|
|
|
@ -85,52 +63,27 @@ public class LDAPExporter extends HttpServlet {
|
|
|
|
|
|
|
|
|
|
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());
|
|
|
|
|
}
|
|
|
|
|
//updateUserInLDAP("testino.testone", "Testa", "LastName", "email@email.com", "SECRET", ctx);
|
|
|
|
|
|
|
|
|
|
_log.debug("LDAP Sync done ... reading LDAP users now ..");
|
|
|
|
|
|
|
|
|
|
SearchControls ctls = new SearchControls();
|
|
|
|
|
ctls.setSearchScope(SearchControls.SUBTREE_SCOPE);
|
|
|
|
|
|
|
|
|
|
NamingEnumeration<SearchResult> answer = ctx.search(baseDN, filter, ctls);
|
|
|
|
|
|
|
|
|
|
while (answer.hasMoreElements()) {
|
|
|
|
|
|
|
|
|
|
SearchResult a = answer.nextElement();
|
|
|
|
|
Attributes result = a.getAttributes();
|
|
|
|
|
|
|
|
|
|
if (result == null){
|
|
|
|
|
System.out.println("Attributes not present");
|
|
|
|
|
}else{
|
|
|
|
|
Attribute attr = result.get("uid");
|
|
|
|
|
if (attr != null){
|
|
|
|
|
for (NamingEnumeration vals = attr.getAll(); vals.hasMoreElements(); System.out.println(vals.nextElement().toString()));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
attr = result.get("userPassword");
|
|
|
|
|
|
|
|
|
|
if (attr != null){
|
|
|
|
|
for (NamingEnumeration vals = attr.getAll(); vals.hasMoreElements(); ) {
|
|
|
|
|
_log.debug("sha pwd: " + new String((byte[])vals.nextElement()));
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
} catch (NamingException e) {
|
|
|
|
|
_log.error("Something went Wrong during LDAP Sync");
|
|
|
|
|
e.printStackTrace();
|
|
|
|
|
}
|
|
|
|
|
_log.debug("LDAP Sync Completed");
|
|
|
|
|
_log.info("LDAP Sync Completed OK!");
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private static boolean checkIfLDAPUserExists(String username, DirContext ctx, String filter) throws NamingException {
|
|
|
|
|
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);
|
|
|
|
@ -146,7 +99,7 @@ public class LDAPExporter extends HttpServlet {
|
|
|
|
|
* @param ctx
|
|
|
|
|
* @throws NamingException
|
|
|
|
|
*/
|
|
|
|
|
private static void updateUserInLDAP(String username, String name, String lastName, String email, String passwd, DirContext ctx, String filter) 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");
|
|
|
|
@ -177,7 +130,11 @@ public class LDAPExporter extends HttpServlet {
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private static String getSubContext(String username) {
|
|
|
|
|
return "uid="+username+",ou=People,o=Liferay,ou=Organizations,dc=d4science,dc=org";
|
|
|
|
|
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());
|
|
|
|
|
}
|
|
|
|
|
}
|