Fixed how PortalContext retrieves the token

git-svn-id: https://svn.d4science.research-infrastructures.eu/gcube/trunk/portlets/user/statistical-algorithms-importer@135060 82a268e6-3cf1-43bd-a215-b396298e98cf
This commit is contained in:
Giancarlo Panichi 2016-11-29 16:42:57 +00:00
parent f6ac94563a
commit ea21cfe3bc
1 changed files with 65 additions and 16 deletions

View File

@ -19,6 +19,9 @@ import org.gcube.portlets.user.statisticalalgorithmsimporter.shared.exception.St
import org.gcube.portlets.user.statisticalalgorithmsimporter.shared.file.FileUploadMonitor;
import org.gcube.portlets.user.statisticalalgorithmsimporter.shared.project.Project;
import org.gcube.portlets.user.statisticalalgorithmsimporter.shared.session.SessionConstants;
import org.gcube.vomanagement.usermanagement.impl.LiferayGroupManager;
import org.gcube.vomanagement.usermanagement.model.GCubeGroup;
import org.gcube.vomanagement.usermanagement.model.GCubeUser;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@ -46,6 +49,8 @@ public class SessionUtil {
String userName = null;
String scope = null;
String token = null;
String groupId = null;
String groupName = null;
if (Constants.DEBUG_MODE) {
logger.info("No credential found in session, use test user!");
@ -58,10 +63,20 @@ public class SessionUtil {
} else {
logger.info("Retrieving credential in session!");
PortalContext pContext = PortalContext.getConfiguration();
boolean hasScopeGroupId = false;
if (scopeGroupId != null && !scopeGroupId.isEmpty()) {
hasScopeGroupId = true;
} else {
hasScopeGroupId = false;
}
if (hasScopeGroupId) {
scope = pContext.getCurrentScope(scopeGroupId);
} else {
scope = pContext.getCurrentScope(httpServletRequest);
}
if (scope == null || scope.isEmpty()) {
@ -70,8 +85,16 @@ public class SessionUtil {
throw new StatAlgoImporterServiceException(error);
}
userName = pContext.getCurrentUser(httpServletRequest)
.getUsername();
GCubeUser gCubeUser = pContext.getCurrentUser(httpServletRequest);
if (gCubeUser == null) {
String error = "Error retrieving gCubeUser in scope " + scope
+ ": " + gCubeUser;
logger.error(error);
throw new StatAlgoImporterServiceException(error);
}
userName = gCubeUser.getUsername();
if (userName == null || userName.isEmpty()) {
String error = "Error retrieving username in scope " + scope
@ -80,7 +103,7 @@ public class SessionUtil {
throw new StatAlgoImporterServiceException(error);
}
token = pContext.getCurrentUserToken(httpServletRequest);
token = pContext.getCurrentUserToken(scope, httpServletRequest);
if (token == null || token.isEmpty()) {
String error = "Error retrieving token for " + userName
@ -89,21 +112,47 @@ public class SessionUtil {
throw new StatAlgoImporterServiceException(error);
}
String name = pContext.getCurrentUser(httpServletRequest)
.getFirstName();
String lastName = pContext.getCurrentUser(httpServletRequest)
.getLastName();
String fullName = pContext.getCurrentUser(httpServletRequest)
.getFullname();
String name = gCubeUser.getFirstName();
String lastName = gCubeUser.getLastName();
String fullName = gCubeUser.getFullname();
String userAvatarURL = pContext.getCurrentUser(httpServletRequest)
.getUserAvatarURL();
String userAvatarURL = gCubeUser.getUserAvatarURL();
String email = pContext.getCurrentUser(httpServletRequest)
.getEmail();
String groupId = String.valueOf(pContext
.getCurrentGroupId(httpServletRequest));
String groupName = pContext.getCurrentGroupName(httpServletRequest);
String email = gCubeUser.getEmail();
if (hasScopeGroupId) {
groupId = scopeGroupId;
long gId;
try {
gId = Long.parseLong(scopeGroupId);
} catch (Throwable e) {
String error = "Error retrieving groupId: " + scopeGroupId;
logger.error(error, e);
throw new StatAlgoImporterServiceException(error);
}
GCubeGroup group;
try {
group = new LiferayGroupManager().getGroup(gId);
} catch (Throwable e) {
String error = "Error retrieving group: " + groupName;
logger.error(error);
throw new StatAlgoImporterServiceException(error);
}
groupName = group.getGroupName();
} else {
groupId = String.valueOf(pContext
.getCurrentGroupId(httpServletRequest));
groupName = pContext.getCurrentGroupName(httpServletRequest);
}
sCredentials = new ServiceCredentials(userName, fullName, name,
lastName, email, scope, groupId, groupName, userAvatarURL,