2015-03-03 12:31:46 +01:00
|
|
|
package org.gcube.portlets.user.joinvre.server;
|
2015-03-03 11:21:17 +01:00
|
|
|
|
2015-03-04 10:56:16 +01:00
|
|
|
import java.io.File;
|
|
|
|
import java.io.FileInputStream;
|
|
|
|
import java.io.IOException;
|
2015-03-03 11:21:17 +01:00
|
|
|
import java.util.ArrayList;
|
|
|
|
import java.util.Collections;
|
2015-03-04 10:56:16 +01:00
|
|
|
import java.util.List;
|
|
|
|
import java.util.Properties;
|
2015-03-03 11:21:17 +01:00
|
|
|
|
2015-03-04 15:51:15 +01:00
|
|
|
import javax.servlet.http.HttpServletRequest;
|
|
|
|
|
2015-03-03 11:21:17 +01:00
|
|
|
import org.gcube.application.framework.core.session.ASLSession;
|
|
|
|
import org.gcube.application.framework.core.session.SessionManager;
|
2015-03-04 10:56:16 +01:00
|
|
|
import org.gcube.portal.custom.communitymanager.OrganizationsUtil;
|
2015-03-03 11:21:17 +01:00
|
|
|
import org.gcube.portal.custom.scopemanager.scopehelper.ScopeHelper;
|
|
|
|
import org.gcube.portlets.user.joinvre.client.JoinService;
|
2015-03-03 12:31:46 +01:00
|
|
|
import org.gcube.portlets.user.joinvre.shared.UserBelonging;
|
|
|
|
import org.gcube.portlets.user.joinvre.shared.VRE;
|
2015-03-03 11:21:17 +01:00
|
|
|
|
|
|
|
import com.google.gwt.user.server.rpc.RemoteServiceServlet;
|
2015-03-04 15:51:15 +01:00
|
|
|
import com.liferay.portal.kernel.exception.PortalException;
|
2015-03-04 10:56:16 +01:00
|
|
|
import com.liferay.portal.kernel.exception.SystemException;
|
2015-03-05 15:10:23 +01:00
|
|
|
import com.liferay.portal.kernel.log.Log;
|
|
|
|
import com.liferay.portal.kernel.log.LogFactoryUtil;
|
2015-03-04 15:51:15 +01:00
|
|
|
import com.liferay.portal.kernel.servlet.ImageServletTokenUtil;
|
|
|
|
import com.liferay.portal.kernel.util.WebKeys;
|
|
|
|
import com.liferay.portal.model.Group;
|
2015-03-04 10:56:16 +01:00
|
|
|
import com.liferay.portal.model.Organization;
|
2015-03-05 15:32:15 +01:00
|
|
|
import com.liferay.portal.model.User;
|
|
|
|
import com.liferay.portal.security.permission.PermissionChecker;
|
|
|
|
import com.liferay.portal.security.permission.PermissionCheckerFactoryUtil;
|
|
|
|
import com.liferay.portal.security.permission.PermissionThreadLocal;
|
2015-03-04 10:56:16 +01:00
|
|
|
import com.liferay.portal.service.OrganizationLocalServiceUtil;
|
2015-03-03 11:21:17 +01:00
|
|
|
import com.liferay.portal.service.UserLocalServiceUtil;
|
2015-03-04 15:51:15 +01:00
|
|
|
import com.liferay.portal.theme.ThemeDisplay;
|
2015-03-03 11:21:17 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @author Massimiliano Assante, ISTI-CNR - massimiliano.assante@isti.cnr.it
|
|
|
|
* @author Luca Frosini (ISTI - CNR) http://www.lucafrosini.com/
|
|
|
|
*/
|
|
|
|
@SuppressWarnings("serial")
|
|
|
|
public class JoinServiceImpl extends RemoteServiceServlet implements JoinService {
|
2015-03-04 10:56:16 +01:00
|
|
|
|
2015-03-05 15:10:23 +01:00
|
|
|
private static Log _log = LogFactoryUtil.getLog(JoinServiceImpl.class);
|
2015-03-03 11:21:17 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* the current ASLSession
|
|
|
|
* @return the session
|
|
|
|
*/
|
|
|
|
private ASLSession getASLSession() {
|
|
|
|
String sessionID = this.getThreadLocalRequest().getSession().getId();
|
|
|
|
String user = (String) this.getThreadLocalRequest().getSession().getAttribute(ScopeHelper.USERNAME_ATTRIBUTE);
|
|
|
|
if (user == null) {
|
|
|
|
_log.warn("USER IS NULL setting test.user and Running OUTSIDE PORTAL");
|
|
|
|
user = getDevelopmentUser();
|
|
|
|
SessionManager.getInstance().getASLSession(sessionID, user).setScope("/gcube");
|
|
|
|
}
|
|
|
|
return SessionManager.getInstance().getASLSession(sessionID, user);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* when packaging test will fail if the user is not set to test.user
|
|
|
|
* @return .
|
|
|
|
*/
|
|
|
|
public String getDevelopmentUser() {
|
|
|
|
String user = "test.user";
|
|
|
|
return user;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
*
|
|
|
|
* @return true if you're running into the portal, false if in development
|
|
|
|
*/
|
|
|
|
private boolean isWithinPortal() {
|
|
|
|
try {
|
|
|
|
UserLocalServiceUtil.getService();
|
|
|
|
return true;
|
|
|
|
} catch (com.liferay.portal.kernel.bean.BeanLocatorException ex) {
|
|
|
|
_log.trace("Development Mode ON");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
/**
|
|
|
|
*
|
|
|
|
* @param session the Asl Session
|
|
|
|
* @param withinPortal true when is on Liferay portal
|
|
|
|
* @return the users belonging to the current organization (scope)
|
|
|
|
*/
|
|
|
|
@Override
|
|
|
|
public ArrayList<VRE> getVREs() {
|
|
|
|
ArrayList<VRE> vres = new ArrayList<VRE>();
|
|
|
|
|
|
|
|
try {
|
|
|
|
if (isWithinPortal()) {
|
2015-03-05 15:10:23 +01:00
|
|
|
vres = getPortalOrganizationMappedToVRE();
|
2015-03-04 12:27:08 +01:00
|
|
|
} else {
|
|
|
|
List<String> devsecCategories = new ArrayList<String>();
|
|
|
|
devsecCategories.add("Development");
|
2015-03-04 14:59:12 +01:00
|
|
|
vres.add(new VRE(0, "devsec", "devsec VRE description", "", "", "/group/devsec", devsecCategories, UserBelonging.NOT_BELONGING, false));
|
2015-03-04 12:27:08 +01:00
|
|
|
List<String> devVRECategories = new ArrayList<String>(devsecCategories);
|
|
|
|
devVRECategories.add("Sailing");
|
2015-03-04 14:59:12 +01:00
|
|
|
vres.add(new VRE(1, "devVRE", "devVRE VRE description", "", "", "/group/devVRE", devVRECategories, UserBelonging.NOT_BELONGING, false));
|
2015-03-04 12:27:08 +01:00
|
|
|
List<String> devmodeategories = new ArrayList<String>(devsecCategories);
|
|
|
|
devmodeategories.add("Climbing");
|
2015-03-04 14:59:12 +01:00
|
|
|
vres.add(new VRE(2, "devmode", "devmode VRE description", "", "", "/group/devmode", devmodeategories, UserBelonging.NOT_BELONGING, true));
|
|
|
|
vres.add(new VRE(3, "devsec", "devsec VRE description", "", "", "/group/devsec", devsecCategories, UserBelonging.NOT_BELONGING, false));
|
|
|
|
vres.add(new VRE(4, "devsec", "devsec VRE description", "", "", "/group/devsec", devsecCategories, UserBelonging.NOT_BELONGING, false));
|
|
|
|
vres.add(new VRE(5, "devsec", "devsec VRE description", "", "", "/group/devsec", devsecCategories, UserBelonging.NOT_BELONGING, false));
|
2015-03-03 11:21:17 +01:00
|
|
|
}
|
|
|
|
} catch (Exception e) {
|
2015-03-05 15:10:23 +01:00
|
|
|
_log.error("Error getting VREs", e);
|
2015-03-03 11:21:17 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// Ordering VREs by Name
|
|
|
|
Collections.sort(vres);
|
|
|
|
return vres;
|
|
|
|
}
|
|
|
|
|
2015-03-04 15:51:15 +01:00
|
|
|
private String getPortalBasicUrl() {
|
|
|
|
HttpServletRequest request = this.getThreadLocalRequest();
|
|
|
|
String protocol = (request.isSecure()) ? "https://" : "http://" ;
|
2015-03-05 15:10:23 +01:00
|
|
|
String port = (request.getServerPort() == 80) ? "" : String.format(":%d", request.getServerPort());
|
2015-03-04 15:51:15 +01:00
|
|
|
String portalBasicUrl = String.format("%s%s%s", protocol, request.getServerName(), port);
|
2015-03-05 15:10:23 +01:00
|
|
|
_log.debug(String.format("getPortalBasicUrl : %s", portalBasicUrl));
|
2015-03-04 15:51:15 +01:00
|
|
|
return portalBasicUrl;
|
|
|
|
}
|
|
|
|
|
|
|
|
private static final String REQUEST_BASED_GROUP = "Requestbasedgroup";
|
|
|
|
|
2015-03-04 18:00:07 +01:00
|
|
|
public boolean requireAccessGrant(Organization organization) throws PortalException, SystemException {
|
|
|
|
try {
|
|
|
|
long companyId = OrganizationsUtil.getCompany().getCompanyId();
|
|
|
|
_log.trace("Setting Thread Permission");
|
|
|
|
User user = UserLocalServiceUtil.getUserByScreenName(companyId, ScopeHelper.getAdministratorUsername());
|
|
|
|
PermissionChecker permissionChecker = PermissionCheckerFactoryUtil.create(user, false);
|
|
|
|
PermissionThreadLocal.setPermissionChecker(permissionChecker);
|
|
|
|
_log.trace("Setting Permission ok!");
|
|
|
|
|
|
|
|
if (organization.getExpandoBridge().getAttribute(REQUEST_BASED_GROUP) == null || organization.getExpandoBridge().getAttribute(REQUEST_BASED_GROUP).equals("")) {
|
2015-03-05 15:32:15 +01:00
|
|
|
_log.trace(String.format("Attribute %s not initialized. In this case by default Access Grant is permitted", REQUEST_BASED_GROUP));
|
2015-03-04 18:00:07 +01:00
|
|
|
return true;
|
|
|
|
} else {
|
|
|
|
String attributeValue = (String) organization.getExpandoBridge().getAttribute(REQUEST_BASED_GROUP);
|
|
|
|
return (attributeValue.compareTo("true") == 0);
|
|
|
|
}
|
|
|
|
} catch (Exception e) {
|
2015-03-05 15:32:15 +01:00
|
|
|
return false;
|
2015-03-04 15:51:15 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-03-04 18:00:07 +01:00
|
|
|
public ArrayList<VRE> getPortalOrganizationMappedToVRE() throws SystemException, PortalException {
|
2015-03-04 15:51:15 +01:00
|
|
|
|
2015-03-04 18:00:07 +01:00
|
|
|
ArrayList<VRE> vres = new ArrayList<VRE>();
|
2015-03-04 10:56:16 +01:00
|
|
|
|
|
|
|
List<Organization> organizations = OrganizationLocalServiceUtil.getOrganizations(0, OrganizationLocalServiceUtil.getOrganizationsCount());
|
|
|
|
Organization rootOrganization = null;
|
|
|
|
for (Organization organization : organizations) {
|
|
|
|
if (organization.getName().equals(getRootOrganizationName())) {
|
|
|
|
rootOrganization = organization;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
try {
|
|
|
|
_log.info("root: " + rootOrganization.getName() );
|
|
|
|
} catch (NullPointerException e) {
|
|
|
|
_log.error("Cannot find root organziation, please check gcube-data.properties file in $CATALINA_HOME/conf folder, unless your installing the Bundle");
|
|
|
|
return new ArrayList<VRE>();
|
|
|
|
}
|
|
|
|
|
2015-03-04 15:51:15 +01:00
|
|
|
ThemeDisplay themeDisplay = (ThemeDisplay) this.getThreadLocalRequest().getSession().getAttribute(WebKeys.THEME_DISPLAY);
|
2015-03-05 15:10:23 +01:00
|
|
|
String imagePath = "/image";
|
|
|
|
if(themeDisplay!=null){
|
|
|
|
imagePath = themeDisplay.getPathImage();
|
|
|
|
} /*If themeDisplay is null the user is not logged in. Needs to find a way to get it */
|
2015-03-04 15:51:15 +01:00
|
|
|
|
2015-03-04 10:56:16 +01:00
|
|
|
//for each root sub organizations (VO)
|
|
|
|
for (Organization vOrg : rootOrganization.getSuborganizations()) {
|
2015-03-04 15:51:15 +01:00
|
|
|
for (Organization vreOrganization : vOrg.getSuborganizations()) {
|
|
|
|
long vreID = vreOrganization.getOrganizationId();
|
|
|
|
String vreName = vreOrganization.getName();
|
|
|
|
String vreDescription = (vreOrganization.getComments()!=null) ? vreOrganization.getComments() : "";
|
|
|
|
|
|
|
|
long logoId = vreOrganization.getLogoId();
|
2015-03-05 15:10:23 +01:00
|
|
|
String vreLogoURL = String.format("%s/organization_logo?img_id=%s&t=%s", imagePath, logoId, ImageServletTokenUtil.getToken(logoId));
|
|
|
|
|
2015-03-04 15:51:15 +01:00
|
|
|
|
|
|
|
String groupName = String.format("/%s/%s/%s", vOrg.getParentOrganization().getName(), vOrg.getName(), vreName);
|
|
|
|
Group vreGroup = vreOrganization.getGroup();
|
|
|
|
String friendlyURL = vreGroup.getPathFriendlyURL(true, themeDisplay) + vreGroup.getFriendlyURL();
|
|
|
|
friendlyURL = String.format("%s%s", getPortalBasicUrl(), friendlyURL);
|
|
|
|
|
|
|
|
boolean requireAccessGrant = requireAccessGrant(vreOrganization);
|
|
|
|
|
|
|
|
List<String> categories = new ArrayList<String>();
|
2015-03-05 15:10:23 +01:00
|
|
|
_log.debug(String.format("VRE preferences : %s", vreOrganization.getPreferences()));
|
2015-03-04 10:56:16 +01:00
|
|
|
|
2015-03-04 15:51:15 +01:00
|
|
|
vres.add(new VRE(vreID,vreName, vreDescription, vreLogoURL, groupName,friendlyURL, categories, UserBelonging.NOT_BELONGING, requireAccessGrant));
|
2015-03-04 10:56:16 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-03-04 18:00:07 +01:00
|
|
|
return vres;
|
2015-03-04 10:56:16 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
public static final String ROOT_ORG = "rootorganization";
|
|
|
|
|
|
|
|
/**
|
|
|
|
* read the root VO name from a property file and retuns it
|
|
|
|
*/
|
|
|
|
protected static String getRootOrganizationName() {
|
|
|
|
//get the portles to look for from the property file
|
|
|
|
Properties props = new Properties();
|
|
|
|
String toReturn = "";
|
|
|
|
|
|
|
|
try {
|
|
|
|
String propertyfile = OrganizationsUtil.getTomcatFolder()+"conf/gcube-data.properties";
|
|
|
|
File propsFile = new File(propertyfile);
|
|
|
|
FileInputStream fis = new FileInputStream(propsFile);
|
|
|
|
props.load( fis);
|
|
|
|
toReturn = props.getProperty(ROOT_ORG);
|
|
|
|
}
|
|
|
|
//catch exception in case properties file does not exist
|
|
|
|
catch(IOException e) {
|
|
|
|
toReturn = "gcube";
|
|
|
|
_log.error("gcube-data.properties file not found under $CATALINA_HOME/conf dir, returning default VO Name " + toReturn);
|
|
|
|
return toReturn;
|
|
|
|
}
|
|
|
|
_log.debug("Returning Root VO Name: " + toReturn );
|
|
|
|
return toReturn;
|
|
|
|
}
|
2015-03-04 14:59:12 +01:00
|
|
|
|
|
|
|
@Override
|
|
|
|
public Boolean joinVRE(Long vreID) {
|
|
|
|
// Here for future improvement
|
|
|
|
return new Boolean(true);
|
|
|
|
}
|
2015-03-03 11:21:17 +01:00
|
|
|
}
|