ws-thredds-sync-widget/src/main/java/org/gcube/portlets/widgets/wsthreddssync/server/WsUtil.java

148 lines
5.3 KiB
Java
Raw Normal View History

/**
*
*/
package org.gcube.portlets.widgets.wsthreddssync.server;
2021-03-03 18:14:53 +01:00
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import org.gcube.common.portal.PortalContext;
2021-03-03 18:14:53 +01:00
import org.gcube.portlets.widgets.wsthreddssync.shared.GatewayRolesThredds;
import org.gcube.vomanagement.usermanagement.GroupManager;
import org.gcube.vomanagement.usermanagement.RoleManager;
import org.gcube.vomanagement.usermanagement.exception.GroupRetrievalFault;
import org.gcube.vomanagement.usermanagement.exception.UserManagementSystemException;
import org.gcube.vomanagement.usermanagement.exception.UserRetrievalFault;
import org.gcube.vomanagement.usermanagement.impl.LiferayGroupManager;
import org.gcube.vomanagement.usermanagement.impl.LiferayRoleManager;
import org.gcube.vomanagement.usermanagement.model.GCubeGroup;
import org.gcube.vomanagement.usermanagement.model.GCubeRole;
//import org.gcube.portlets.user.workspace.server.util.WsUtil;
import org.gcube.vomanagement.usermanagement.model.GCubeUser;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.liferay.portal.service.UserLocalServiceUtil;
/**
* The Class WsUtil.
*
2021-05-11 13:01:01 +02:00
* @author Francesco Mangiacrapa francesco.mangiacrapa@isti.cnr.it Nov 25, 2016
*/
public class WsUtil {
2021-03-03 18:14:53 +01:00
/** The logger. */
private static Logger logger = LoggerFactory.getLogger(WsUtil.class);
/**
* Checks if is within portal.
*
* @return true if you're running into the portal, false if in development
*/
public static boolean isWithinPortal() {
try {
UserLocalServiceUtil.getService();
return true;
2021-05-11 13:01:01 +02:00
} catch (Exception ex) {
logger.trace("Development Mode ON");
return false;
}
}
/**
* Checks if is session expired.
*
* @param httpServletRequest the http servlet request
* @return true, if is session expired
* @throws Exception the exception
*/
public static boolean isSessionExpired(HttpServletRequest httpServletRequest) throws Exception {
logger.trace("workspace session validating...");
2021-05-11 13:01:01 +02:00
return PortalContext.getConfiguration().getCurrentUser(httpServletRequest) == null;
}
2021-03-03 18:14:53 +01:00
/**
2021-05-11 13:01:01 +02:00
* Gets the list of Scopes (Root-VO, VOs and VREs) for user and the Thredds
* roles that user has in them.
2021-03-03 18:14:53 +01:00
*
* @param user the user
* @return the VREs and Thredds roles for a given user
2021-03-03 18:14:53 +01:00
*/
2021-05-11 13:01:01 +02:00
public static Map<String, GatewayRolesThredds> getScopesWithThreddsRolesForUser(GCubeUser user) {
logger.info("called getScopesThreddsRolesForUser user: " + user + ", in all contexts");
2021-03-03 18:14:53 +01:00
GroupManager groupManager = new LiferayGroupManager();
Map<String, GatewayRolesThredds> mapRoleByGroupSingleVre = new HashMap<String, GatewayRolesThredds>();
2021-03-03 18:14:53 +01:00
try {
2021-05-11 13:01:01 +02:00
// Retrieving the list of VOs and VREs
2021-03-03 18:14:53 +01:00
List<GCubeGroup> listOfGroups = groupManager.listGroupsByUser(user.getUserId());
2021-05-11 13:01:01 +02:00
// adding also the ROOT-VO
2021-03-03 18:14:53 +01:00
listOfGroups.add(groupManager.getRootVO());
for (GCubeGroup gCubeGroup : listOfGroups) {
GatewayRolesThredds threddsRole = getThreddsRoleFor(user, gCubeGroup);
2021-05-11 13:01:01 +02:00
if (threddsRole != null) {
2021-03-17 11:28:25 +01:00
String toFullScope = groupManager.getInfrastructureScope(gCubeGroup.getGroupId());
mapRoleByGroupSingleVre.put(toFullScope, threddsRole);
2021-03-03 18:14:53 +01:00
}
}
2021-05-11 13:01:01 +02:00
logger.info("For user: " + user + ", returning Map (VRE, ThreddsRoles) " + mapRoleByGroupSingleVre);
2021-03-03 18:14:53 +01:00
return mapRoleByGroupSingleVre;
2021-05-11 13:01:01 +02:00
} catch (UserManagementSystemException | UserRetrievalFault | GroupRetrievalFault e) {
logger.error("An error occurred during geThreddsVreRolesForUser: " + user, e);
2021-03-03 18:14:53 +01:00
return null;
2021-05-11 13:01:01 +02:00
}
2021-03-03 18:14:53 +01:00
}
/**
* Gets the (highest) thredds role for the user in the scope
2021-03-03 18:14:53 +01:00
*
2021-05-11 13:01:01 +02:00
* @param user the user
* @param scope the vre
2021-03-03 18:14:53 +01:00
* @return the thredds role for
*/
2021-05-11 13:01:01 +02:00
public static GatewayRolesThredds getThreddsRoleFor(GCubeUser user, GCubeGroup scope) {
if (user == null || scope == null) {
logger.warn("called getThreddsRoleFor with invalid parameter user: " + user + ", in the scope: " + scope,
", returning null");
2021-03-08 11:52:41 +01:00
return null;
}
2021-05-11 13:01:01 +02:00
logger.info("called getThreddsRoleFor user: " + user.getUsername() + ", in the scope: " + scope.getGroupName());
2021-03-03 18:14:53 +01:00
try {
RoleManager roleManager = new LiferayRoleManager();
List<GCubeRole> roles = roleManager.listRolesByUserAndGroup(user.getUserId(), scope.getGroupId());
2021-03-03 18:14:53 +01:00
List<GatewayRolesThredds> threddsRoles = new ArrayList<GatewayRolesThredds>();
for (GCubeRole gCubeRole : roles) {
2021-05-11 13:01:01 +02:00
if (gCubeRole.getRoleName().equalsIgnoreCase(GatewayRolesThredds.DATA_MANAGER.getRoleName())) {
threddsRoles.add(GatewayRolesThredds.DATA_MANAGER);
2021-03-03 18:14:53 +01:00
}
2021-05-11 13:01:01 +02:00
if (gCubeRole.getRoleName().equalsIgnoreCase(GatewayRolesThredds.DATA_EDITOR.getRoleName())) {
threddsRoles.add(GatewayRolesThredds.DATA_EDITOR);
2021-03-03 18:14:53 +01:00
}
}
2021-05-11 13:01:01 +02:00
logger.info("For user: " + user.getUsername() + " in the scope: " + scope.getGroupName()
+ " read the role/s: " + threddsRoles);
GatewayRolesThredds toReturn = null;
if (threddsRoles.contains(GatewayRolesThredds.DATA_MANAGER))
toReturn = GatewayRolesThredds.DATA_MANAGER;
else if (threddsRoles.contains(GatewayRolesThredds.DATA_EDITOR))
toReturn = GatewayRolesThredds.DATA_EDITOR;
2021-05-11 13:01:01 +02:00
2021-03-08 11:52:41 +01:00
logger.info("returning highest role: " + toReturn);
return toReturn;
2021-05-11 13:01:01 +02:00
} catch (UserRetrievalFault | GroupRetrievalFault e) {
logger.error("An error occurred during getVreRoleForUser: " + user, e);
2021-03-03 18:14:53 +01:00
return null;
}
}
}