diff --git a/src/main/java/org/gcube/common/portal/ContextUserUtil.java b/src/main/java/org/gcube/common/portal/ContextUserUtil.java new file mode 100644 index 0000000..9dba846 --- /dev/null +++ b/src/main/java/org/gcube/common/portal/ContextUserUtil.java @@ -0,0 +1,62 @@ +package org.gcube.common.portal; + +import java.io.UnsupportedEncodingException; +import java.security.Key; + +import javax.servlet.http.Cookie; +import javax.servlet.http.HttpServletRequest; + +import com.liferay.portal.model.Company; +import com.liferay.portal.service.CompanyLocalServiceUtil; +import com.liferay.util.Encryptor; +/** + * + * @author Massimiliano Assante, CNR-ISTI + * + */ +public class ContextUserUtil { + /** + * + * @param httpServletRequest + * @returnthe current user LR id + */ + protected static Long getCurrentUserId(HttpServletRequest httpServletRequest) { + Cookie[] cookies = httpServletRequest.getCookies(); + String userId = null; + String companyId = null; + if (cookies != null) { + for (Cookie c : cookies) { + if ("COMPANY_ID".equals(c.getName())) { + companyId = c.getValue(); + } else if ("ID".equals(c.getName())) { + userId = hexStringToStringByAscii(c.getValue()); + } + } + if (userId != null && companyId != null) { + try { + Company company = CompanyLocalServiceUtil.getCompany(Long.parseLong(companyId)); + Key key = company.getKeyObj(); + String userIdPlain = Encryptor.decrypt(key, userId); + return Long.valueOf(userIdPlain); + + } catch (Exception pException) { + throw new RuntimeException(pException); + } + } + } + return null; + } + + private static String hexStringToStringByAscii(String hexString) { + byte[] bytes = new byte[hexString.length() / 2]; + for (int i = 0; i < hexString.length() / 2; i++) { + String oneHexa = hexString.substring(i * 2, i * 2 + 2); + bytes[i] = Byte.parseByte(oneHexa, 16); + } + try { + return new String(bytes, "ASCII"); + } catch (UnsupportedEncodingException e) { + throw new RuntimeException(e); + } + } +} diff --git a/src/main/java/org/gcube/common/portal/PortalContext.java b/src/main/java/org/gcube/common/portal/PortalContext.java index 401e8f6..d018fdb 100644 --- a/src/main/java/org/gcube/common/portal/PortalContext.java +++ b/src/main/java/org/gcube/common/portal/PortalContext.java @@ -5,6 +5,7 @@ import static org.gcube.common.authorization.client.Constants.authorizationServi import java.io.File; import java.io.FileInputStream; import java.io.IOException; +import java.io.UnsupportedEncodingException; import java.util.ArrayList; import java.util.List; import java.util.Properties; @@ -17,6 +18,7 @@ import org.gcube.vomanagement.usermanagement.impl.LiferayGroupManager; import org.gcube.vomanagement.usermanagement.impl.LiferayUserManager; import org.gcube.vomanagement.usermanagement.model.CustomAttributeKeys; import org.gcube.vomanagement.usermanagement.model.Email; +import org.gcube.vomanagement.usermanagement.model.GCubeGroup; import org.gcube.vomanagement.usermanagement.model.GCubeUser; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -59,7 +61,9 @@ public class PortalContext { private static final String DEV_USER_LASTNAME_ATTR = "user.lastname"; private static final String DEV_USER_EMAIL_ATTR = "user.email"; - private static final String DEV_SCOPE_ATTR = "development.scope"; + private static final String DEV_SCOPE_ATTR = "development.context"; + private static final String DEV_GROUP_NAME_ATTR = "development.groupname"; + private static final String DEV_GROUP_ID_ATTR = "development.groupid"; private static final String DEV_TOKEN_ATTR = "user.token"; @@ -126,25 +130,20 @@ public class PortalContext { /** * * @param httpServletRequest the httpServletRequest object - * @return the instance of the user + * @return the instance of the current user * @see GCubeUser */ public GCubeUser getCurrentUser(HttpServletRequest httpServletRequest) { - String userIdNo = httpServletRequest.getHeader(USER_ID_ATTR_NAME); + Long userIdNo = ContextUserUtil.getCurrentUserId(httpServletRequest); if (userIdNo != null) { - long userId = -1; try { - userId = Long.parseLong(userIdNo); - return new LiferayUserManager().getUserById(userId); - } catch (NumberFormatException e) { - _log.error("The userId is not a number -> " + userId); - } catch (Exception e) { - _log.error("The userId does not belong to any user -> " + userId); + return new LiferayUserManager().getUserById(userIdNo); + } catch (Exception e) { + _log.error("The userId does not belong to any user -> " + userIdNo); } } else { if (isWithinPortal()) { - _log.error("It seems your app is running in Liferay but not context was set on this (HttpServletRequest) request, " - + "make sure you have set the gCube ClientContext correctly in your code."); + _log.warn("Could not read the current userid, either session expired or user not logged in"); } else { GCubeUser toReturn = readUserFromPropertyFile(); _log.debug("getCurrentUser devMode into IDE detected, returning testing user: " + toReturn.toString()); @@ -153,12 +152,10 @@ public class PortalContext { } return null; } - /** * * @param httpServletRequest the httpServletRequest object - * @return the instance of the user - * @see GCubeUser + * @return the scope (context) */ public String getCurrentScope(HttpServletRequest httpServletRequest) { String groupIdNo = httpServletRequest.getHeader(VRE_ID_ATTR_NAME); @@ -177,11 +174,8 @@ public class PortalContext { _log.error("This groupId does not belong to any group in this portal -> " + groupId); } } else { - if (isWithinPortal()) { - _log.error("It seems your app is running in Liferay but not context was set on this (HttpServletRequest) request, " - + "make sure you have set the gCube ClientContext correctly in your code."); - } else { - String toReturn = readScopePropertyFile(); + if (!isWithinPortal()) { + String toReturn = readContextPropertyFile(); _log.debug("getCurrentScope devMode into IDE detected, returning scope: " + toReturn.toString()); _log.debug("The PortalBeanLocatorUtil stacktrace (java.lang.Exception) is acceptable in dev"); return toReturn; @@ -189,6 +183,64 @@ public class PortalContext { } return null; } + /** + * + * @param httpServletRequest the httpServletRequest object + * @return the instance of the user + * @see GCubeUser + */ + public String getCurrentGroupName(HttpServletRequest httpServletRequest) { + String groupIdNo = httpServletRequest.getHeader(VRE_ID_ATTR_NAME); + if (groupIdNo != null) { + long groupId = -1; + try { + groupId = Long.parseLong(groupIdNo); + LiferayGroupManager gm = new LiferayGroupManager(); + return gm.getGroup(groupId).getGroupName(); + } catch (NumberFormatException e) { + _log.error("The groupId is not a number -> " + groupId); + } catch (Exception e) { + _log.error("This groupId does not belong to any group in this portal -> " + groupId); + } + } else { + if (!isWithinPortal()) { + String toReturn = readGroupNamePropertyFile(); + _log.debug("getCurrentGroupName devMode into IDE detected, returning group name: " + toReturn.toString()); + _log.debug("The PortalBeanLocatorUtil stacktrace (java.lang.Exception) is acceptable in dev"); + return toReturn; + } + } + return null; + } + /** + * + * @param httpServletRequest the httpServletRequest object + * @return the instance of the user + * @see GCubeUser + */ + public long getCurrentGroupId(HttpServletRequest httpServletRequest) { + String groupIdNo = httpServletRequest.getHeader(VRE_ID_ATTR_NAME); + if (groupIdNo != null) { + long groupId = -1; + try { + groupId = Long.parseLong(groupIdNo); + return groupId; + } catch (NumberFormatException e) { + _log.error("The groupId is not a number -> " + groupId); + } catch (Exception e) { + _log.error("This groupId does not belong to any group in this portal -> " + groupId); + } + } else { + if (!isWithinPortal()) { + long toReturn = readGroupIdPropertyFile(); + _log.debug("getCurrentGroup devMode into IDE detected, returning groupid = " + toReturn); + _log.debug("The PortalBeanLocatorUtil stacktrace (java.lang.Exception) is acceptable in dev"); + return toReturn; + } + } + return -1; + } + /** * @@ -210,8 +262,7 @@ public class PortalContext { } } else { if (isWithinPortal()) { - _log.error("It seems your app is running in Liferay but not context was set on this (HttpServletRequest) request, " - + "make sure you have set the gCube ClientContext correctly in your code."); + _log.warn("It seems your app is running in Liferay but not context was set on this (HttpServletRequest) request"); } else { String toReturn = readTokenPropertyFile(); _log.debug("getCurrentToken devMode into IDE detected, returning scope: " + toReturn.toString()); @@ -221,6 +272,7 @@ public class PortalContext { } return null; } + /** * * @param username @@ -591,10 +643,12 @@ public class PortalContext { _log.error(GCUBE_DEV__CONTEXT_PROPERTY_FILENAME + " file not found under $CATALINA_HOME dir"); return null; } - }/** + } + + /** * for development purposes only */ - private static String readScopePropertyFile() { + private static String readContextPropertyFile() { Properties props = new Properties(); try { StringBuilder sb = new StringBuilder(getCatalinaHome()); @@ -617,6 +671,69 @@ public class PortalContext { return null; } } + /** + * for development purposes only + */ + private static String readGroupNamePropertyFile() { + Properties props = new Properties(); + try { + StringBuilder sb = new StringBuilder(getCatalinaHome()); + sb.append(File.separator) + .append(GCUBE_DEV__CONTEXT_PROPERTY_FILENAME); + String propertyfile = sb.toString(); + File propsFile = new File(propertyfile); + FileInputStream fis = new FileInputStream(propsFile); + props.load( fis); + String groupName = props.getProperty(DEV_GROUP_NAME_ATTR); + if (groupName != null && !groupName.isEmpty()) + return groupName; + else { + _log.error("groupName is not valid, check property " + DEV_GROUP_NAME_ATTR); + return null; + } + } + catch(IOException e) { + _log.error(GCUBE_DEV__CONTEXT_PROPERTY_FILENAME + " file not found under $CATALINA_HOME dir"); + return null; + } + } + + /** + * for development purposes only + */ + private static long readGroupIdPropertyFile() { + Properties props = new Properties(); + try { + StringBuilder sb = new StringBuilder(getCatalinaHome()); + sb.append(File.separator) + .append(GCUBE_DEV__CONTEXT_PROPERTY_FILENAME); + String propertyfile = sb.toString(); + File propsFile = new File(propertyfile); + FileInputStream fis = new FileInputStream(propsFile); + props.load( fis); + String groupIdNo = props.getProperty(DEV_GROUP_ID_ATTR); + long groupId = -1; + if (groupIdNo != null) { + try { + groupId = Long.parseLong(groupIdNo); + return groupId; + } catch (NumberFormatException e) { + _log.error("The groupId is not a number -> " + groupIdNo); + } + } + else { + _log.error("groupId is not valid, check property " + DEV_GROUP_ID_ATTR); + return -1L; + } + } + catch(IOException e) { + _log.error(GCUBE_DEV__CONTEXT_PROPERTY_FILENAME + " file not found under $CATALINA_HOME dir"); + return -1L; + } + return -1L; + } + + /** * * @return $CATALINA_HOME @@ -625,4 +742,7 @@ public class PortalContext { return (System.getenv("CATALINA_HOME").endsWith("/") ? System.getenv("CATALINA_HOME") : System.getenv("CATALINA_HOME")+"/"); } + + + }