Ported to Liferay 6.2 API

git-svn-id: http://svn.research-infrastructures.eu/public/d4science/gcube/trunk/portal/ldap-export-servlet@129867 82a268e6-3cf1-43bd-a215-b396298e98cf
This commit is contained in:
Massimiliano Assante 2016-07-04 13:37:55 +00:00
parent 48ca69e423
commit 2c01090876
6 changed files with 51 additions and 56 deletions

View File

@ -1,5 +1,10 @@
<ReleaseNotes> <ReleaseNotes>
<Changeset component="org.gcube.portal.ldap-export-servlet.1-0-0" date="2015-12-11"> <Changeset component="org.gcube.portal.ldap-export-servlet.1-1-0"
date="2016-07-04">
<Change>Updated to use Liferay 6.2 API</Change>
</Changeset>
<Changeset component="org.gcube.portal.ldap-export-servlet.1-0-0"
date="2015-12-11">
<Change>First Release</Change> <Change>First Release</Change>
</Changeset> </Changeset>
</ReleaseNotes> </ReleaseNotes>

View File

@ -38,10 +38,5 @@
<source>target/${build.finalName}.war</source> <source>target/${build.finalName}.war</source>
<outputDirectory>/${artifactId}</outputDirectory> <outputDirectory>/${artifactId}</outputDirectory>
</file> </file>
<file>
<source>${distroDirectory}/svnpath.txt</source>
<outputDirectory>/${artifactId}</outputDirectory>
<filtered>true</filtered>
</file>
</files> </files>
</assembly> </assembly>

View File

@ -6,7 +6,7 @@
<Description>${description}</Description> <Description>${description}</Description>
<Class>Portal</Class> <Class>Portal</Class>
<Name>${artifactId}</Name> <Name>${artifactId}</Name>
<Version>1.0.0</Version> <Version>${version}</Version>
<Packages> <Packages>
<Software> <Software>
<Name>${artifactId}</Name> <Name>${artifactId}</Name>

View File

@ -11,13 +11,13 @@
<groupId>org.gcube.portal</groupId> <groupId>org.gcube.portal</groupId>
<artifactId>ldap-export-servlet</artifactId> <artifactId>ldap-export-servlet</artifactId>
<packaging>war</packaging> <packaging>war</packaging>
<version>1.0.0-SNAPSHOT</version> <version>1.1.0-SNAPSHOT</version>
<name>ldap-export-servlet Maven Webapp</name> <name>ldap-export-servlet Maven Webapp</name>
<url>http://maven.apache.org</url> <url>http://maven.apache.org</url>
<description>This component read periodically exports users and their groups in LDAP</description> <description>This component read periodically exports users and their groups in LDAP</description>
<properties> <properties>
<java-version>1.7</java-version> <java-version>1.7</java-version>
<liferay-version>6.0.6</liferay-version> <liferay-version>6.2.5</liferay-version>
<distroDirectory>${project.basedir}/distro</distroDirectory> <distroDirectory>${project.basedir}/distro</distroDirectory>
<webappDirectory>${project.build.directory}/${project.build.finalName}</webappDirectory> <webappDirectory>${project.build.directory}/${project.build.finalName}</webappDirectory>
<distroDirectory>distro</distroDirectory> <distroDirectory>distro</distroDirectory>
@ -51,6 +51,11 @@
<artifactId>custom-portal-handler</artifactId> <artifactId>custom-portal-handler</artifactId>
<scope>provided</scope> <scope>provided</scope>
</dependency> </dependency>
<dependency>
<groupId>org.gcube.dvos</groupId>
<artifactId>usermanagement-core</artifactId>
<scope>provided</scope>
</dependency>
<dependency> <dependency>
<groupId>org.gcube.resources.discovery</groupId> <groupId>org.gcube.resources.discovery</groupId>
<artifactId>ic-client</artifactId> <artifactId>ic-client</artifactId>

View File

@ -18,16 +18,15 @@ import javax.naming.directory.SearchControls;
import javax.naming.directory.SearchResult; import javax.naming.directory.SearchResult;
import org.gcube.common.portal.PortalContext; import org.gcube.common.portal.PortalContext;
import org.gcube.portal.custom.communitymanager.OrganizationsUtil; import org.gcube.vomanagement.usermanagement.GroupManager;
import org.gcube.vomanagement.usermanagement.impl.LiferayGroupManager;
import org.gcube.vomanagement.usermanagement.model.GCubeGroup;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
import com.liferay.portal.kernel.cache.CacheRegistryUtil; import com.liferay.portal.kernel.cache.CacheRegistryUtil;
import com.liferay.portal.kernel.exception.PortalException;
import com.liferay.portal.kernel.exception.SystemException; import com.liferay.portal.kernel.exception.SystemException;
import com.liferay.portal.model.Organization;
import com.liferay.portal.model.User; import com.liferay.portal.model.User;
import com.liferay.portal.service.OrganizationLocalServiceUtil;
import com.liferay.portal.service.UserLocalServiceUtil; import com.liferay.portal.service.UserLocalServiceUtil;
public class LDAPSync implements Runnable { public class LDAPSync implements Runnable {
@ -56,22 +55,15 @@ public class LDAPSync implements Runnable {
* *
* @return the Liferay mapped as Root Organization * @return the Liferay mapped as Root Organization
*/ */
private Organization getRootVO() { private GCubeGroup getRootVO() {
String rootVoName = PortalContext.getConfiguration().getInfrastructureName();
_log.debug("Root organization name found: " + rootVoName);
//start of iteration of the actual groups
List<Organization> organizations;
try { try {
organizations = OrganizationLocalServiceUtil.getOrganizations(0, OrganizationLocalServiceUtil.getOrganizationsCount()); GroupManager gm = new LiferayGroupManager();
for (Organization organization : organizations) { String rootVoName = gm.getRootVOName();
if (organization.getName().equals(rootVoName)) { _log.debug("Root organization name found: " + rootVoName);
return organization; return gm.getGroup(gm.getGroupIdFromInfrastructureScope("/"+rootVoName));
} }
} catch (Exception e) {
} _log.error("There were problems retrieving root VO group", e);
catch (SystemException e) {
_log.error("There were problems retrieving root organization", e);
} }
_log.error("Could not find any root organization"); _log.error("Could not find any root organization");
return null; return null;
@ -91,7 +83,7 @@ public class LDAPSync implements Runnable {
e1.printStackTrace(); e1.printStackTrace();
} }
_log.debug("Reading Portal Organizations ..."); _log.debug("Reading Portal Organizations ...");
Organization rootVO = getRootVO(); GCubeGroup rootVO = getRootVO();
_log.debug("Initializing LDAP exporter ..."); _log.debug("Initializing LDAP exporter ...");
@ -107,7 +99,7 @@ public class LDAPSync implements Runnable {
createUsersOrganizationalUnit(ctx); createUsersOrganizationalUnit(ctx);
//crate or update the whole list of organizations (objectClass=organizationalUnit, ou="+orgName+",dc=d4science,dc=org) and groups ( objectClass=top and POSIXGroup) //crate or update the whole list of organizations (objectClass=organizationalUnit, ou="+orgName+",dc=d4science,dc=org) and groups ( objectClass=top and POSIXGroup)
updateGroups(ctx, rootVO); updateGroups(ctx, rootVO);
//and update the users list //and update the users list
exportSingleUsers(ctx, env, users); exportSingleUsers(ctx, env, users);
@ -119,7 +111,7 @@ public class LDAPSync implements Runnable {
es.printStackTrace(); es.printStackTrace();
} }
} }
/** /**
* create the following: ou=People,o=D4Science,ou=Organizations,dc=d4science,dc=org * create the following: ou=People,o=D4Science,ou=Organizations,dc=d4science,dc=org
* @param ctx * @param ctx
@ -163,18 +155,18 @@ public class LDAPSync implements Runnable {
* @throws NamingException * @throws NamingException
* @throws SystemException * @throws SystemException
*/ */
private void updateGroups(DirContext ctx, Organization root) throws NamingException, SystemException { private void updateGroups(DirContext ctx, GCubeGroup root) throws NamingException, SystemException {
String subCtx = getOrgSubContext(root.getName()); String subCtx = getOrgSubContext(root.getGroupName());
if (!checkIfLDAPOrganizationalUnitExists(ctx, subCtx)) if (!checkIfLDAPOrganizationalUnitExists(ctx, subCtx))
createOrganizationalUnit(ctx, subCtx); createOrganizationalUnit(ctx, subCtx);
for (Organization org : root.getSuborganizations()) { for (GCubeGroup vo : root.getChildren()) {
String orgSubCtx = "ou="+org.getName()+","+subCtx; String orgSubCtx = "ou="+vo.getGroupName()+","+subCtx;
if (!checkIfLDAPOrganizationalUnitExists(ctx, orgSubCtx)) if (!checkIfLDAPOrganizationalUnitExists(ctx, orgSubCtx))
createOrganizationalUnit(ctx, orgSubCtx); createOrganizationalUnit(ctx, orgSubCtx);
for (Organization vre : org.getSuborganizations()) { for (GCubeGroup vre : vo.getChildren()) {
String vreSubCtx = "cn="+vre.getName()+","+orgSubCtx; String vreSubCtx = "cn="+vre.getGroupName()+","+orgSubCtx;
if (!checkIfLDAPGroupExists(ctx, vreSubCtx)) if (!checkIfLDAPGroupExists(ctx, vreSubCtx))
createGroupVRE(ctx, vreSubCtx, vre.getName()); createGroupVRE(ctx, vreSubCtx, vre.getGroupName());
//update the list of users in such VRE //update the list of users in such VRE
updateUsersInGroup(ctx, vreSubCtx, vre); updateUsersInGroup(ctx, vreSubCtx, vre);
} }
@ -188,8 +180,8 @@ public class LDAPSync implements Runnable {
* @throws NamingException * @throws NamingException
* @throws SystemException * @throws SystemException
*/ */
private void updateUsersInGroup(DirContext ctx, String vreSubCtx, Organization vre) throws NamingException, SystemException { private void updateUsersInGroup(DirContext ctx, String vreSubCtx, GCubeGroup vre) throws NamingException, SystemException {
List<User> users = UserLocalServiceUtil.getOrganizationUsers(vre.getOrganizationId()); List<User> users = UserLocalServiceUtil.getGroupUsers(vre.getGroupId());
for (User userObj : users) { for (User userObj : users) {
String user = userObj.getScreenName(); String user = userObj.getScreenName();
try { try {
@ -211,7 +203,7 @@ public class LDAPSync implements Runnable {
private void exportSingleUsers(DirContext ctx, Properties env, List<User> users) throws NamingException { private void exportSingleUsers(DirContext ctx, Properties env, List<User> users) throws NamingException {
for (User user : users) { for (User user : users) {
updateUserInLDAP(user.getScreenName(), user.getFirstName(), user.getLastName(), user.getFullName(), user.getEmailAddress(), "{SHA}"+user.getPassword(), ctx, filter); updateUserInLDAP(user.getScreenName(), user.getFirstName(), user.getLastName(), user.getFullName(), user.getEmailAddress(), "{SHA}"+user.getPassword(), ctx, filter);
//_log.debug("Updated " + user.getScreenName()); _log.debug("Updated " + user.getScreenName());
} }
_log.debug("LDAP Users Sync cycle done"); _log.debug("LDAP Users Sync cycle done");
if (! users.isEmpty()) if (! users.isEmpty())
@ -322,7 +314,7 @@ public class LDAPSync implements Runnable {
* @param username * @param username
* @param ctx * @param ctx
* @param filter * @param filter
* @return true if exists * @return true if exists
*/ */
private boolean checkIfLDAPUserExists(String username, DirContext ctx, String filter) { private boolean checkIfLDAPUserExists(String username, DirContext ctx, String filter) {
SearchControls ctls = new SearchControls(); SearchControls ctls = new SearchControls();
@ -361,7 +353,7 @@ public class LDAPSync implements Runnable {
Attribute userPassword = new BasicAttribute("userPassword"); Attribute userPassword = new BasicAttribute("userPassword");
Attribute gidNumber = new BasicAttribute("gidNumber"); Attribute gidNumber = new BasicAttribute("gidNumber");
Attribute homeDirectory = new BasicAttribute("homeDirectory"); Attribute homeDirectory = new BasicAttribute("homeDirectory");
givenName.add(name); givenName.add(name);
cn.add(fullName); cn.add(fullName);
@ -370,7 +362,7 @@ public class LDAPSync implements Runnable {
userPassword.add(passwd); userPassword.add(passwd);
gidNumber.add(DEFAULT_GID_NUMBER); gidNumber.add(DEFAULT_GID_NUMBER);
homeDirectory.add("/home/"+username); homeDirectory.add("/home/"+username);
attributes.put(givenName); attributes.put(givenName);
attributes.put(cn); attributes.put(cn);
attributes.put(sn); attributes.put(sn);
@ -378,7 +370,7 @@ public class LDAPSync implements Runnable {
attributes.put(userPassword); attributes.put(userPassword);
attributes.put(gidNumber); attributes.put(gidNumber);
attributes.put(homeDirectory); attributes.put(homeDirectory);
if (checkIfLDAPUserExists(username, ctx, filter)) { if (checkIfLDAPUserExists(username, ctx, filter)) {
@ -407,14 +399,12 @@ public class LDAPSync implements Runnable {
private List<User> getAllLiferayUsers() { private List<User> getAllLiferayUsers() {
String infraName = PortalContext.getConfiguration().getInfrastructureName(); String infraName = PortalContext.getConfiguration().getInfrastructureName();
_log.info("TRY Reading non chached users belonging to: /" + infraName); _log.info("TRY Reading non chached users belonging to: /" + infraName);
List<User> toReturn = new ArrayList<User>(); List<User> toReturn = new ArrayList<User>();
Organization rootInfra;
try { try {
CacheRegistryUtil.clear(); //needed to avoid cache use by liferay API CacheRegistryUtil.clear(); //needed to avoid cache use by liferay API
rootInfra = OrganizationLocalServiceUtil.getOrganization(OrganizationsUtil.getCompany().getCompanyId(), infraName); long groupId = new LiferayGroupManager().getGroupIdFromInfrastructureScope("/" + infraName);
toReturn = UserLocalServiceUtil.getOrganizationUsers(rootInfra.getOrganizationId()); toReturn = UserLocalServiceUtil.getGroupUsers(groupId);
} catch (PortalException | SystemException e) { } catch (Exception e) {
_log.error("Error during LDAP Sync, could not retrieve users from LR DB: " + e.getMessage()); _log.error("Error during LDAP Sync, could not retrieve users from LR DB: " + e.getMessage());
} }
return toReturn; return toReturn;
@ -431,7 +421,7 @@ public class LDAPSync implements Runnable {
return toReturn; return toReturn;
} }
private boolean checkIfPosixUidNumberExists(DirContext ctx, int numberToCheck) { private boolean checkIfPosixUidNumberExists(DirContext ctx, int numberToCheck) {
SearchControls ctls = new SearchControls(); SearchControls ctls = new SearchControls();
ctls.setSearchScope(SearchControls.SUBTREE_SCOPE); ctls.setSearchScope(SearchControls.SUBTREE_SCOPE);

View File

@ -1,8 +1,8 @@
<!DOCTYPE web-app PUBLIC <?xml version="1.0" encoding="UTF-8"?>
"-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN" <web-app xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
"http://java.sun.com/dtd/web-app_2_3.dtd" > xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
<web-app> version="3.0">
<display-name>LDAP Export servlet</display-name> <display-name>LDAP Export servlet</display-name>
<servlet> <servlet>