2018-03-02 17:03:57 +01:00
|
|
|
/**
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
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;
|
|
|
|
|
2018-03-02 17:03:57 +01:00
|
|
|
import javax.servlet.http.HttpServletRequest;
|
|
|
|
|
2019-08-02 15:04:28 +02:00
|
|
|
//import org.gcube.common.homelibrary.home.HomeLibrary;
|
|
|
|
//import org.gcube.common.homelibrary.home.exceptions.HomeNotFoundException;
|
|
|
|
//import org.gcube.common.homelibrary.home.exceptions.InternalErrorException;
|
|
|
|
//import org.gcube.common.homelibrary.home.workspace.Workspace;
|
|
|
|
//import org.gcube.common.homelibrary.home.workspace.exceptions.WorkspaceFolderNotFoundException;
|
2018-03-02 17:03:57 +01:00
|
|
|
import org.gcube.common.portal.PortalContext;
|
|
|
|
import org.gcube.common.scope.api.ScopeProvider;
|
2019-08-02 15:04:28 +02:00
|
|
|
import org.gcube.common.storagehubwrapper.server.StorageHubWrapper;
|
|
|
|
import org.gcube.common.storagehubwrapper.server.tohl.Workspace;
|
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;
|
2019-08-02 15:04:28 +02:00
|
|
|
//import org.gcube.portlets.user.workspace.server.util.WsUtil;
|
2018-03-02 17:03:57 +01:00
|
|
|
import org.gcube.vomanagement.usermanagement.model.GCubeUser;
|
|
|
|
import org.slf4j.Logger;
|
|
|
|
import org.slf4j.LoggerFactory;
|
|
|
|
|
|
|
|
import com.liferay.portal.service.UserLocalServiceUtil;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* The Class WsUtil.
|
|
|
|
*
|
|
|
|
* @author Francesco Mangiacrapa francesco.mangiacrapa@isti.cnr.it
|
|
|
|
* Nov 25, 2016
|
|
|
|
*/
|
|
|
|
public class WsUtil {
|
2021-03-03 18:14:53 +01:00
|
|
|
|
|
|
|
|
2018-03-02 17:03:57 +01:00
|
|
|
|
2021-03-03 18:14:53 +01:00
|
|
|
/** The logger. */
|
2018-03-02 17:03:57 +01:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
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...");
|
|
|
|
return PortalContext.getConfiguration().getCurrentUser(httpServletRequest)==null;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2019-08-02 15:04:28 +02:00
|
|
|
* Gets the workspace from storage hub.
|
2018-03-02 17:03:57 +01:00
|
|
|
*
|
|
|
|
* @param httpServletRequest the http servlet request
|
2019-08-02 15:04:28 +02:00
|
|
|
* @return the workspace from storage hub
|
|
|
|
* @throws Exception the exception
|
2018-03-02 17:03:57 +01:00
|
|
|
*/
|
2019-08-02 15:04:28 +02:00
|
|
|
public Workspace getWorkspaceFromStorageHub(HttpServletRequest httpServletRequest) throws Exception {
|
2018-03-02 17:03:57 +01:00
|
|
|
logger.trace("Get Workspace");
|
2019-08-02 15:04:28 +02:00
|
|
|
// String scope = PortalContext.getConfiguration().getCurrentScope(httpServletRequest);
|
2018-03-02 17:03:57 +01:00
|
|
|
|
2019-08-02 15:04:28 +02:00
|
|
|
GCubeUser user = null;
|
|
|
|
|
|
|
|
try {
|
|
|
|
String scope = PortalContext.getConfiguration().getCurrentScope(httpServletRequest);
|
|
|
|
user = PortalContext.getConfiguration().getCurrentUser(httpServletRequest);
|
|
|
|
if (user == null || user.getUsername().isEmpty())
|
|
|
|
throw new Exception("Session expired");
|
|
|
|
|
|
|
|
ScopeProvider.instance.set(scope);
|
|
|
|
logger.trace("Scope provider instancied at: "+scope);
|
|
|
|
|
|
|
|
logger.debug("Getting " + StorageHubWrapper.class.getSimpleName() + " for user: " + user.getUsername()
|
|
|
|
+ " by using the scope: " + scope);
|
2018-03-02 17:03:57 +01:00
|
|
|
|
2019-08-02 15:04:28 +02:00
|
|
|
String token = PortalContext.getConfiguration().getCurrentUserToken(scope, user.getUsername());
|
|
|
|
StorageHubWrapper shWrapper = new StorageHubWrapper(scope, token, false, false, true);
|
|
|
|
return shWrapper.getWorkspace();
|
|
|
|
} catch (Exception e) {
|
|
|
|
logger.error("Error on getting the Workspace via SHUB wrapper", e);
|
|
|
|
throw new Exception("Error on gettig the Workspace for userId: " + user);
|
|
|
|
}
|
2018-03-02 17:03:57 +01:00
|
|
|
}
|
|
|
|
|
2021-03-03 18:14:53 +01:00
|
|
|
/**
|
2021-03-04 16:24:54 +01: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
|
2021-03-04 16:24:54 +01:00
|
|
|
* @return the VREs and Thredds roles for a given user
|
2021-03-03 18:14:53 +01:00
|
|
|
*/
|
2021-03-04 16:24:54 +01:00
|
|
|
public static Map<String, GatewayRolesThredds> getScopesWithThreddsRolesForUser(GCubeUser user){
|
2021-03-03 18:14:53 +01:00
|
|
|
logger.info("called getScopesThreddsRolesForUser user: "+user+", in all contexts");
|
|
|
|
|
|
|
|
GroupManager groupManager = new LiferayGroupManager();
|
2021-03-04 16:24:54 +01:00
|
|
|
Map<String, GatewayRolesThredds> mapRoleByGroupSingleVre = new HashMap<String, GatewayRolesThredds>();
|
2021-03-03 18:14:53 +01:00
|
|
|
try {
|
|
|
|
|
|
|
|
//Retrieving the list of VOs and VREs
|
|
|
|
List<GCubeGroup> listOfGroups = groupManager.listGroupsByUser(user.getUserId());
|
|
|
|
//adding also the ROOT-VO
|
|
|
|
listOfGroups.add(groupManager.getRootVO());
|
|
|
|
for (GCubeGroup gCubeGroup : listOfGroups) {
|
2021-03-04 16:24:54 +01:00
|
|
|
GatewayRolesThredds threddsRole = getThreddsRoleFor(user, gCubeGroup);
|
|
|
|
if(threddsRole != null) {
|
|
|
|
mapRoleByGroupSingleVre.put(gCubeGroup.getGroupName(), threddsRole);
|
2021-03-03 18:14:53 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
logger.info("For user: "+user+", returning Map (VRE, ThreddsRoles) " + mapRoleByGroupSingleVre);
|
|
|
|
return mapRoleByGroupSingleVre;
|
|
|
|
}catch (UserManagementSystemException | UserRetrievalFault | GroupRetrievalFault e) {
|
|
|
|
logger.error("An error occurred during geThreddsVreRolesForUser: "+user, e);
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
2021-03-04 16:24:54 +01:00
|
|
|
* Gets the (highest) thredds role for the user in the scope
|
2021-03-03 18:14:53 +01:00
|
|
|
*
|
|
|
|
* @param user the user
|
2021-03-04 16:24:54 +01:00
|
|
|
* @param scope the vre
|
2021-03-03 18:14:53 +01:00
|
|
|
* @return the thredds role for
|
|
|
|
*/
|
2021-03-04 16:24:54 +01:00
|
|
|
public static GatewayRolesThredds getThreddsRoleFor(GCubeUser user, GCubeGroup scope){
|
|
|
|
logger.info("called getThreddsRoleFor user: "+user+", in the scope: "+scope.getGroupName());
|
2021-03-03 18:14:53 +01:00
|
|
|
try {
|
|
|
|
RoleManager roleManager = new LiferayRoleManager();
|
2021-03-04 16:24:54 +01:00
|
|
|
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-03-08 10:55:48 +01:00
|
|
|
if(gCubeRole.getRoleName().equalsIgnoreCase(GatewayRolesThredds.DATA_MANAGER.getRoleName())){
|
|
|
|
threddsRoles.add(GatewayRolesThredds.DATA_MANAGER);
|
2021-03-03 18:14:53 +01:00
|
|
|
}
|
2021-03-08 10:55:48 +01:00
|
|
|
if(gCubeRole.getRoleName().equalsIgnoreCase(GatewayRolesThredds.DATA_EDITOR.getRoleName())){
|
|
|
|
threddsRoles.add(GatewayRolesThredds.DATA_EDITOR);
|
2021-03-03 18:14:53 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-03-04 16:24:54 +01:00
|
|
|
logger.info("For user: "+user+" in the scope: "+scope.getGroupName()+" read the role/s: " + threddsRoles);
|
|
|
|
|
|
|
|
GatewayRolesThredds toReturn = null;
|
2021-03-08 10:55:48 +01:00
|
|
|
if (threddsRoles.contains(GatewayRolesThredds.DATA_MANAGER))
|
|
|
|
toReturn = GatewayRolesThredds.DATA_MANAGER;
|
|
|
|
else if (threddsRoles.contains(GatewayRolesThredds.DATA_EDITOR))
|
|
|
|
toReturn = GatewayRolesThredds.DATA_EDITOR;
|
2021-03-04 16:24:54 +01:00
|
|
|
|
|
|
|
logger.info("returning role: " + toReturn);
|
|
|
|
return toReturn;
|
2021-03-03 18:14:53 +01:00
|
|
|
}catch (UserRetrievalFault | GroupRetrievalFault e) {
|
|
|
|
logger.error("An error occurred during getVreRoleForUser: "+user, e);
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2018-03-02 17:03:57 +01:00
|
|
|
}
|