Fixed how PortalContext retrieves the token

git-svn-id: https://svn.d4science.research-infrastructures.eu/gcube/trunk/portlets/user/data-miner-manager@135055 82a268e6-3cf1-43bd-a215-b396298e98cf
This commit is contained in:
Giancarlo Panichi 2016-11-29 16:27:32 +00:00
parent 81d5a6e66e
commit 29cac83598
1 changed files with 97 additions and 50 deletions

View File

@ -17,6 +17,9 @@ import org.gcube.portlets.user.dataminermanager.server.dmservice.SClientDirector
import org.gcube.portlets.user.dataminermanager.server.util.ServiceCredentials; import org.gcube.portlets.user.dataminermanager.server.util.ServiceCredentials;
import org.gcube.portlets.user.dataminermanager.shared.Constants; import org.gcube.portlets.user.dataminermanager.shared.Constants;
import org.gcube.portlets.user.dataminermanager.shared.exception.ServiceException; import org.gcube.portlets.user.dataminermanager.shared.exception.ServiceException;
import org.gcube.vomanagement.usermanagement.impl.LiferayGroupManager;
import org.gcube.vomanagement.usermanagement.model.GCubeGroup;
import org.gcube.vomanagement.usermanagement.model.GCubeUser;
/** /**
* *
@ -29,19 +32,20 @@ public class SessionUtil {
private static final Logger logger = Logger.getLogger(SessionUtil.class); private static final Logger logger = Logger.getLogger(SessionUtil.class);
public static ServiceCredentials getServiceCredentials( public static ServiceCredentials getServiceCredentials(
HttpServletRequest httpServletRequest) HttpServletRequest httpServletRequest) throws ServiceException {
throws ServiceException {
return getServiceCredentials(httpServletRequest, null); return getServiceCredentials(httpServletRequest, null);
} }
public static ServiceCredentials getServiceCredentials( public static ServiceCredentials getServiceCredentials(
HttpServletRequest httpServletRequest, String scopeGroupId) HttpServletRequest httpServletRequest, String scopeGroupId)
throws ServiceException { throws ServiceException {
ServiceCredentials sCredentials = null; ServiceCredentials sCredentials = null;
String userName = null; String userName = null;
String scope = null; String scope = null;
String token = null; String token = null;
String groupId = null;
String groupName = null;
if (Constants.DEBUG_MODE) { if (Constants.DEBUG_MODE) {
logger.info("No credential found in session, use test user!"); logger.info("No credential found in session, use test user!");
@ -54,55 +58,100 @@ public class SessionUtil {
} else { } else {
logger.info("Retrieving credential in session!"); logger.info("Retrieving credential in session!");
PortalContext pContext = PortalContext.getConfiguration(); PortalContext pContext = PortalContext.getConfiguration();
if(scopeGroupId!=null&&!scopeGroupId.isEmpty()){ boolean hasScopeGroupId = false;
if (scopeGroupId != null && !scopeGroupId.isEmpty()) {
hasScopeGroupId = true;
} else {
hasScopeGroupId = false;
}
if (hasScopeGroupId) {
scope = pContext.getCurrentScope(scopeGroupId); scope = pContext.getCurrentScope(scopeGroupId);
} else { } else {
scope = pContext.getCurrentScope(httpServletRequest); scope = pContext.getCurrentScope(httpServletRequest);
}
if(scope==null||scope.isEmpty()){
String error="Error retrieving scope: "+scope;
logger.error(error);
throw new ServiceException(error);
}
userName = pContext.getCurrentUser(httpServletRequest)
.getUsername();
if(userName==null||userName.isEmpty()){
String error="Error retrieving username in scope "+scope+": "+userName;
logger.error(error);
throw new ServiceException(error);
}
token = pContext.getCurrentUserToken(httpServletRequest);
if(token==null||token.isEmpty()){
String error="Error retrieving token for "+userName+" in "+scope+": "+token;
logger.error(error);
throw new ServiceException(error);
}
String name = pContext.getCurrentUser(httpServletRequest)
.getFirstName();
String lastName = pContext.getCurrentUser(httpServletRequest)
.getLastName();
String fullName = pContext.getCurrentUser(httpServletRequest)
.getFullname();
String userAvatarURL = pContext.getCurrentUser(httpServletRequest) }
.getUserAvatarURL();
String email = pContext.getCurrentUser(httpServletRequest) if (scope == null || scope.isEmpty()) {
.getEmail(); String error = "Error retrieving scope: " + scope;
String groupId = String.valueOf(pContext logger.error(error);
.getCurrentGroupId(httpServletRequest)); throw new ServiceException(error);
String groupName = pContext.getCurrentGroupName(httpServletRequest); }
GCubeUser gCubeUser = pContext.getCurrentUser(httpServletRequest);
if (gCubeUser == null) {
String error = "Error retrieving gCubeUser in scope " + scope
+ ": " + gCubeUser;
logger.error(error);
throw new ServiceException(error);
}
userName = gCubeUser.getUsername();
if (userName == null || userName.isEmpty()) {
String error = "Error retrieving username in scope " + scope
+ ": " + userName;
logger.error(error);
throw new ServiceException(error);
}
token = pContext.getCurrentUserToken(scope, httpServletRequest);
if (token == null || token.isEmpty()) {
String error = "Error retrieving token for " + userName
+ " in " + scope + ": " + token;
logger.error(error);
throw new ServiceException(error);
}
String name = gCubeUser.getFirstName();
String lastName = gCubeUser.getLastName();
String fullName = gCubeUser.getFullname();
String userAvatarURL = gCubeUser.getUserAvatarURL();
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 ServiceException(error);
}
GCubeGroup group;
try {
group = new LiferayGroupManager().getGroup(gId);
} catch (Throwable e) {
String error = "Error retrieving group: " + groupName;
logger.error(error);
throw new ServiceException(error);
}
groupName = group.getGroupName();
} else {
groupId = String.valueOf(pContext
.getCurrentGroupId(httpServletRequest));
groupName = pContext.getCurrentGroupName(httpServletRequest);
}
sCredentials = new ServiceCredentials(userName, fullName, name, sCredentials = new ServiceCredentials(userName, fullName, name,
lastName, email, scope, groupId, groupName, userAvatarURL, token); lastName, email, scope, groupId, groupName, userAvatarURL,
token);
} }
logger.info("ServiceCredentials: " + sCredentials); logger.info("ServiceCredentials: " + sCredentials);
@ -110,10 +159,8 @@ public class SessionUtil {
return sCredentials; return sCredentials;
} }
public static SClient getSClient(ServiceCredentials serviceCredentials,
HttpSession session) throws Exception {
public static SClient getSClient(ServiceCredentials serviceCredentials, HttpSession session)
throws Exception {
if (serviceCredentials == null) { if (serviceCredentials == null) {
logger.error("ServiceCredentials is null!"); logger.error("ServiceCredentials is null!");