2016-04-05 17:24:13 +02:00
|
|
|
package org.gcube.portal.landingpage;
|
|
|
|
|
|
|
|
import java.util.ArrayList;
|
|
|
|
import java.util.List;
|
|
|
|
|
|
|
|
import javax.servlet.http.HttpServletRequest;
|
|
|
|
|
2016-07-05 17:03:49 +02:00
|
|
|
import org.gcube.common.portal.PortalContext;
|
|
|
|
import org.gcube.portal.notifications.thread.NewUserSiteRegistrationNotificationThread;
|
2017-01-26 17:34:50 +01:00
|
|
|
import org.gcube.vomanagement.usermanagement.impl.LiferayRoleManager;
|
|
|
|
import org.gcube.vomanagement.usermanagement.impl.LiferayUserManager;
|
2016-04-05 17:24:13 +02:00
|
|
|
|
|
|
|
import com.liferay.portal.kernel.exception.PortalException;
|
|
|
|
import com.liferay.portal.kernel.exception.SystemException;
|
2020-02-03 16:59:35 +01:00
|
|
|
import com.liferay.portal.kernel.log.Log;
|
|
|
|
import com.liferay.portal.kernel.log.LogFactoryUtil;
|
2016-04-05 17:24:13 +02:00
|
|
|
import com.liferay.portal.kernel.util.PropsKeys;
|
|
|
|
import com.liferay.portal.kernel.util.PropsUtil;
|
|
|
|
import com.liferay.portal.kernel.util.StringBundler;
|
|
|
|
import com.liferay.portal.kernel.util.StringPool;
|
|
|
|
import com.liferay.portal.model.Group;
|
|
|
|
import com.liferay.portal.model.User;
|
|
|
|
import com.liferay.portal.model.VirtualHost;
|
|
|
|
import com.liferay.portal.service.GroupLocalServiceUtil;
|
|
|
|
import com.liferay.portal.service.LayoutSetLocalServiceUtil;
|
|
|
|
import com.liferay.portal.service.UserLocalServiceUtil;
|
|
|
|
import com.liferay.portal.service.VirtualHostLocalServiceUtil;
|
|
|
|
import com.liferay.portal.util.PortalUtil;
|
|
|
|
|
|
|
|
/**
|
|
|
|
*
|
|
|
|
* @author Massimiliano Assante, CNR-ISTI
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
public class LandingPageManager {
|
2020-02-03 16:59:35 +01:00
|
|
|
private static Log _log = LogFactoryUtil.getLog(LandingPageManager.class);
|
2016-04-05 17:24:13 +02:00
|
|
|
public static final String GUEST_GROUP_FRIENDLY_URL = "/guest";
|
|
|
|
public static final String PRIVATE_GROUP_SERVLET_MAPPING = PropsUtil.get(PropsKeys.LAYOUT_FRIENDLY_URL_PRIVATE_GROUP_SERVLET_MAPPING);
|
|
|
|
public static final String PORTAL_CONTEXT = PortalUtil.getPathContext();
|
2018-01-10 17:21:23 +01:00
|
|
|
private static final String SBD_GATEWAY_VHOST = "sobigdata.d4science.org";
|
2016-07-05 17:03:49 +02:00
|
|
|
|
2016-04-05 17:24:13 +02:00
|
|
|
public static String getLandingPagePath(final HttpServletRequest request) throws PortalException, SystemException {
|
|
|
|
User currentUser = PortalUtil.getUser(request);
|
2020-02-03 16:59:35 +01:00
|
|
|
_log.info("getLandingPagePath called for user " + currentUser.getScreenName());
|
2016-04-05 17:24:13 +02:00
|
|
|
return getLandingPagePath(request, currentUser);
|
|
|
|
}
|
|
|
|
/**
|
|
|
|
*
|
|
|
|
* @param request
|
|
|
|
* @param currentUser
|
|
|
|
* @return
|
|
|
|
* @throws PortalException
|
|
|
|
* @throws SystemException
|
|
|
|
*/
|
|
|
|
public static String getLandingPagePath(final HttpServletRequest request, User currentUser) throws PortalException, SystemException {
|
|
|
|
String sitePath = StringPool.BLANK;
|
|
|
|
|
|
|
|
String currentVirtualHost = request.getServerName();
|
2020-02-03 16:59:35 +01:00
|
|
|
_log.info("currentHost is " + currentVirtualHost);
|
2016-04-05 17:24:13 +02:00
|
|
|
Group site = null;
|
|
|
|
List<VirtualHost> vHosts = VirtualHostLocalServiceUtil.getVirtualHosts(0, VirtualHostLocalServiceUtil.getVirtualHostsCount());
|
|
|
|
for (VirtualHost virtualHost : vHosts) {
|
|
|
|
_log.debug("Found " + virtualHost.getHostname());
|
|
|
|
if (virtualHost.getHostname().compareTo("localhost") != 0 &&
|
|
|
|
virtualHost.getLayoutSetId() != 0 &&
|
|
|
|
virtualHost.getHostname().compareTo(currentVirtualHost) == 0) {
|
|
|
|
long layoutSetId = virtualHost.getLayoutSetId();
|
|
|
|
site = LayoutSetLocalServiceUtil.getLayoutSet(layoutSetId).getGroup();
|
|
|
|
_log.debug("Found match! Your site is " + site.getName());
|
|
|
|
List<Group> userSites = getSites(currentUser.getUserId());
|
|
|
|
boolean isRegistered = false;
|
|
|
|
for (Group group : userSites) {
|
|
|
|
if (group.getGroupId() == site.getGroupId()) {
|
|
|
|
isRegistered = true;
|
|
|
|
_log.debug("user " + currentUser.getFullName() + " is registered to " + site.getName() + ". redirecting ...");
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2016-07-05 17:03:49 +02:00
|
|
|
if (! isRegistered) {
|
2020-04-23 09:34:29 +02:00
|
|
|
_log.info("But user " + currentUser.getFullName() + " is not registered to " + site.getName());
|
|
|
|
boolean siteIsGateway = site.getParentGroupId() < 1;
|
|
|
|
_log.info("Is this a Gateway? " + (siteIsGateway) +". If true going to register ...");
|
|
|
|
if (siteIsGateway) {
|
|
|
|
String portalURL = PortalContext.getConfiguration().getGatewayURL(request);
|
|
|
|
registerUserToSite(currentUser, site, portalURL);
|
|
|
|
//only for SBD Gateway we check if the user belongs to resource catalogue VRE and SoBigDataLab VRE
|
|
|
|
if (currentVirtualHost.equalsIgnoreCase(SBD_GATEWAY_VHOST)) {
|
|
|
|
_log.debug("currentVirtualHost.equalsIgnoreCase ->" + SBD_GATEWAY_VHOST);
|
|
|
|
Thread resourceCatalogueSBDThread = new Thread(new ResourceCatalogueAndSBDLabRegistrationThread(userSites, currentUser, portalURL));
|
|
|
|
resourceCatalogueSBDThread.start();
|
|
|
|
}
|
|
|
|
}
|
2016-07-05 17:03:49 +02:00
|
|
|
}
|
2016-04-05 17:24:13 +02:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (site.getPrivateLayoutsPageCount() > 0) {
|
|
|
|
sitePath = getGroupFriendlyURL(request, site);
|
|
|
|
} else {
|
|
|
|
_log.debug(site.getName() + " site doesn't have any private page. Default landing page will be used");
|
|
|
|
}
|
|
|
|
return sitePath;
|
|
|
|
}
|
2018-01-10 17:21:23 +01:00
|
|
|
|
2016-04-05 17:24:13 +02:00
|
|
|
/**
|
|
|
|
* this method is used to register the user to the group if does not belong to it yet
|
|
|
|
* IMPORTANT: it does not add the user to the Site's private pages if the Site Membership type is different from Private
|
|
|
|
* @param user
|
|
|
|
* @param site
|
|
|
|
* @throws SystemException
|
|
|
|
*/
|
2018-01-10 17:21:23 +01:00
|
|
|
protected static void registerUserToSite(User user, Group site, String siteURL) throws SystemException {
|
2016-04-05 17:24:13 +02:00
|
|
|
UserLocalServiceUtil.addGroupUser(site.getGroupId(), user.getUserId());
|
|
|
|
_log.debug("User " + user.getScreenName() +" registered to " + site.getName());
|
2017-01-26 17:34:50 +01:00
|
|
|
Thread emailSiteManagersThread = new Thread(new NewUserSiteRegistrationNotificationThread(new LiferayUserManager(), new LiferayRoleManager() ,user, site, siteURL));
|
2016-07-05 17:03:49 +02:00
|
|
|
emailSiteManagersThread.start();
|
2016-04-05 17:24:13 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
public static List<Group> getSites(final long userId) throws PortalException, SystemException {
|
|
|
|
List<Group> sites = new ArrayList<Group>();
|
|
|
|
for (Group group : GroupLocalServiceUtil.getUserGroups(userId)) {
|
|
|
|
if (group.isRegularSite()
|
|
|
|
&& !GUEST_GROUP_FRIENDLY_URL.equalsIgnoreCase(group.getFriendlyURL())) {
|
|
|
|
sites.add(group);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return sites;
|
|
|
|
}
|
|
|
|
/**
|
|
|
|
* @param request
|
|
|
|
* @param currentGroup
|
|
|
|
* @param isPrivate
|
|
|
|
* @param isUser
|
|
|
|
* @return
|
|
|
|
* @throws PortalException
|
|
|
|
* @throws SystemException
|
|
|
|
*/
|
|
|
|
public static String getGroupFriendlyURL(final HttpServletRequest request, final Group currentGroup) throws PortalException, SystemException {
|
|
|
|
String friendlyURL = PRIVATE_GROUP_SERVLET_MAPPING;
|
|
|
|
StringBundler sb = new StringBundler();
|
|
|
|
sb.append(PORTAL_CONTEXT);
|
|
|
|
sb.append(friendlyURL);
|
|
|
|
sb.append(currentGroup.getFriendlyURL());
|
|
|
|
return sb.toString();
|
|
|
|
}
|
|
|
|
}
|