diff --git a/src/main/java/org/gcube/common/portal/ContextUserUtil.java b/src/main/java/org/gcube/common/portal/ContextUserUtil.java index e1a251d..99d7f0e 100644 --- a/src/main/java/org/gcube/common/portal/ContextUserUtil.java +++ b/src/main/java/org/gcube/common/portal/ContextUserUtil.java @@ -19,7 +19,7 @@ import com.liferay.util.Encryptor; * */ public class ContextUserUtil { - private static final Logger _log = LoggerFactory.getLogger(PortalContext.class); + private static final Logger _log = LoggerFactory.getLogger(ContextUserUtil.class); /** * * @param httpServletRequest @@ -34,13 +34,17 @@ public class ContextUserUtil { if ("COMPANY_ID".equals(c.getName())) { companyId = c.getValue(); } else if ("ID".equals(c.getName())) { + _log.info("User id before ascii conversion is " + c.getValue()); userId = hexStringToStringByAscii(c.getValue()); } } if (userId != null && companyId != null) { try { Company company = CompanyLocalServiceUtil.getCompany(Long.parseLong(companyId)); + _log.info("company is " + company); Key key = company.getKeyObj(); + _log.info("key is " + company.getKey()); + _log.info("User id after ascii conversion is " + userId); String userIdPlain = Encryptor.decrypt(key, userId); return Long.valueOf(userIdPlain); diff --git a/src/main/java/org/gcube/common/portal/PortalContext.java b/src/main/java/org/gcube/common/portal/PortalContext.java index 6ce7aff..fbbcedc 100644 --- a/src/main/java/org/gcube/common/portal/PortalContext.java +++ b/src/main/java/org/gcube/common/portal/PortalContext.java @@ -322,7 +322,40 @@ public class PortalContext { } return userToken; } - + /** + * Another way to retrieve the user token + * @param scope the current scope + * @param userId the Liferay's user id + * @return the Token for the user in scope + */ + public String getCurrentUserToken(String scope, long userId) { + String userToken = null; + if (isWithinPortal()) { + String username = null; + try { + ScopeProvider.instance.set(scope); + username = new LiferayUserManager().getUserById(userId).getUsername(); + userToken = authorizationService().resolveTokenByUserAndContext(username, scope); + SecurityTokenProvider.instance.set(userToken); + } + catch (ObjectNotFound ex) { + userToken = generateAuthorizationToken(username, scope); + SecurityTokenProvider.instance.set(userToken); + _log.debug("generateAuthorizationToken OK for " + username + " in scope " + scope); + } + catch (Exception e) { + _log.error("Error while trying to generate token for user " + username + "in scope " + scope); + e.printStackTrace(); + return null; + } + } else { + String toReturn = readTokenPropertyFile(); + _log.debug("getCurrentToken devMode into IDE detected, returning scope: " + toReturn.toString()); + _log.debug("The PortalBeanLocatorUtil stacktrace (java.lang.Exception) is acceptable in dev"); + return toReturn; + } + return userToken; + } /** * * @param username