2016-06-13 09:48:29 +02:00
package org.gcube.portlets.gcubeckan.gcubeckandatacatalog.server ;
import static org.gcube.common.authorization.client.Constants.authorizationService ;
import java.util.ArrayList ;
import java.util.List ;
2016-11-29 17:20:50 +01:00
import java.util.Map ;
2017-01-27 11:27:40 +01:00
import java.util.Map.Entry ;
import java.util.Set ;
2018-02-22 16:08:22 +01:00
import java.util.concurrent.ConcurrentHashMap ;
2016-06-13 09:48:29 +02:00
import javax.servlet.http.HttpSession ;
2016-06-20 15:22:21 +02:00
import org.apache.commons.codec.binary.Base64 ;
2016-11-17 14:17:29 +01:00
import org.gcube.common.authorization.client.exceptions.ObjectNotFound ;
2016-09-14 16:42:46 +02:00
import org.gcube.common.authorization.library.provider.UserInfo ;
2019-06-10 10:31:43 +02:00
import org.gcube.common.portal.PortalContext ;
2018-02-22 16:08:22 +01:00
import org.gcube.common.scope.impl.ScopeBean ;
import org.gcube.common.scope.impl.ScopeBean.Type ;
2017-01-25 18:30:53 +01:00
import org.gcube.datacatalogue.ckanutillibrary.server.DataCatalogue ;
import org.gcube.datacatalogue.ckanutillibrary.server.DataCatalogueFactory ;
2018-02-23 16:19:07 +01:00
import org.gcube.datacatalogue.ckanutillibrary.server.utils.CatalogueUtilMethods ;
2018-02-27 14:32:40 +01:00
import org.gcube.datacatalogue.ckanutillibrary.server.utils.SessionCatalogueAttributes ;
2017-01-25 18:30:53 +01:00
import org.gcube.datacatalogue.ckanutillibrary.shared.RolesCkanGroupOrOrg ;
2016-06-13 09:48:29 +02:00
import org.gcube.portlets.gcubeckan.gcubeckandatacatalog.client.GcubeCkanDataCatalogService ;
2018-02-22 16:08:22 +01:00
import org.gcube.portlets.gcubeckan.gcubeckandatacatalog.server.thread.UpdateItemCatalogueResource ;
2016-11-21 10:12:24 +01:00
import org.gcube.portlets.gcubeckan.gcubeckandatacatalog.shared.BeanUserInOrgGroupRole ;
2016-06-23 12:49:45 +02:00
import org.gcube.portlets.gcubeckan.gcubeckandatacatalog.shared.CkanConnectorAccessPoint ;
2017-01-25 18:30:53 +01:00
import org.gcube.portlets.widgets.ckandatapublisherwidget.shared.OrganizationBean ;
2016-09-05 18:18:57 +02:00
import org.gcube.vomanagement.usermanagement.GroupManager ;
import org.gcube.vomanagement.usermanagement.impl.LiferayGroupManager ;
2016-06-13 09:48:29 +02:00
import com.google.gwt.user.server.rpc.RemoteServiceServlet ;
2016-12-26 23:12:33 +01:00
import com.liferay.portal.kernel.log.Log ;
import com.liferay.portal.kernel.log.LogFactoryUtil ;
2016-06-27 11:41:05 +02:00
2016-11-17 14:31:01 +01:00
import eu.trentorise.opendata.jackan.model.CkanGroup ;
2016-06-29 16:50:18 +02:00
import eu.trentorise.opendata.jackan.model.CkanOrganization ;
2016-06-13 09:48:29 +02:00
/ * *
* The server side implementation of the RPC service .
*
* @author Francesco Mangiacrapa francesco . mangiacrapa @isti.cnr.it
2016-09-05 18:18:57 +02:00
* @author Costantino Perciante costantino . perciante @isti.cnr.it
2016-06-13 09:48:29 +02:00
* Jun 10 , 2016
* /
@SuppressWarnings ( " serial " )
public class GcubeCkanDataCatalogServiceImpl extends RemoteServiceServlet implements GcubeCkanDataCatalogService {
2016-06-27 16:58:01 +02:00
public static final String UTF_8 = " UTF-8 " ;
2016-06-13 09:48:29 +02:00
private static final String PORT_HTTP = " :80 " ;
private static final String PORT_HTTPS = " :443 " ;
private static final String HTTPS = " https " ;
private static final String HTTP = " http " ;
public static String CKANCONNECTORCONTEXT = " CkanConnectorContext " ;
2016-06-23 12:49:45 +02:00
public static String CKANCONNECTORLOGOUT = " CkanConnectorLogout " ;
2016-12-26 23:12:33 +01:00
private static final Log logger = LogFactoryUtil . getLog ( GcubeCkanDataCatalogServiceImpl . class ) ;
2017-11-28 12:53:16 +01:00
private static final String VIEW_PER_ORGANIZATION_LIFERAY_CUSTOM_FIELD = " ViewPerVRECatalogue " ;
2018-02-22 16:08:22 +01:00
private static final Map < String , Boolean > checksItemCatalogueResourceUpdated = new ConcurrentHashMap < String , Boolean > ( ) ;
2016-06-13 09:48:29 +02:00
2016-06-27 11:10:47 +02:00
/ * *
2016-07-20 19:14:46 +02:00
* Instanciate the ckan util library .
2016-06-27 11:10:47 +02:00
* Since it needs the scope , we need to check if it is null or not
2019-12-05 14:26:23 +01:00
*
2016-07-20 19:14:46 +02:00
* @param discoverScope if you want to the discover the utils library in this specified scope
2019-12-05 14:26:23 +01:00
* @return the catalogue
2016-06-20 15:22:21 +02:00
* /
2016-09-28 16:44:27 +02:00
public DataCatalogue getCatalogue ( String discoverScope ) {
2017-01-25 18:30:53 +01:00
String currentScope = SessionUtil . getCurrentContext ( getThreadLocalRequest ( ) , false ) ;
2016-09-28 16:44:27 +02:00
DataCatalogue instance = null ;
2016-07-04 16:24:53 +02:00
try {
2016-11-04 13:01:35 +01:00
String scopeInWhichDiscover = discoverScope ! = null & & ! discoverScope . isEmpty ( ) ? discoverScope : currentScope ;
2016-09-05 18:18:57 +02:00
logger . debug ( " Discovering ckan utils library into scope " + scopeInWhichDiscover ) ;
2016-09-28 16:44:27 +02:00
instance = DataCatalogueFactory . getFactory ( ) . getUtilsPerScope ( scopeInWhichDiscover ) ;
2016-07-04 16:24:53 +02:00
} catch ( Exception e ) {
2019-08-01 14:19:50 +02:00
logger . error ( " Unable to retrieve ckan utils. Error was: " , e ) ;
2016-06-18 10:25:17 +02:00
}
2016-06-27 11:10:47 +02:00
return instance ;
}
2016-06-18 14:01:58 +02:00
2016-06-13 09:48:29 +02:00
/ * ( non - Javadoc )
2016-06-20 15:22:21 +02:00
* @see org . gcube . portlets . gcubeckan . gcubeckandatacatalog . client . GcubeCkanDataCatalogService # getCKanConnector ( java . lang . String , java . lang . String )
2016-06-13 09:48:29 +02:00
* /
@Override
2020-07-03 17:15:11 +02:00
public CkanConnectorAccessPoint getCKanConnector ( String browserLocationURL , String pathInfoParameter , String queryStringParameters ) throws Exception {
logger . info ( " getCKanConnector [browserLocationURL: " + browserLocationURL + " ] " ) ;
2017-01-25 18:30:53 +01:00
logger . info ( " getCKanConnector [pathInfo: " + pathInfoParameter + " , query: " + queryStringParameters + " ] " ) ;
2016-06-13 09:48:29 +02:00
try {
2016-06-20 16:05:58 +02:00
2016-11-25 10:37:56 +01:00
// just get the current scope and set it into ScopeProvider...
SessionUtil . getCurrentContext ( getThreadLocalRequest ( ) , true ) ;
2016-09-05 18:18:57 +02:00
// retrieve scope per current portlet url
2016-11-25 10:37:56 +01:00
String scopePerCurrentUrl = SessionUtil . getScopeFromClientUrl ( getThreadLocalRequest ( ) ) ;
2016-09-05 18:18:57 +02:00
2016-06-20 16:05:58 +02:00
if ( queryStringParameters ! = null & & Base64 . isBase64 ( queryStringParameters . getBytes ( ) ) ) {
byte [ ] valueDecoded = Base64 . decodeBase64 ( queryStringParameters . getBytes ( ) ) ;
queryStringParameters = new String ( valueDecoded ) ;
logger . info ( " queryStringParameters detected like Base64 and decoded like: " + queryStringParameters ) ;
}
2018-08-30 17:35:45 +02:00
if ( SessionUtil . isIntoPortal ( ) ) {
ScopeBean scopeBean = new ScopeBean ( scopePerCurrentUrl ) ;
if ( scopeBean . is ( Type . VRE ) & & ! checksItemCatalogueResourceUpdated . containsKey ( scopePerCurrentUrl ) ) {
new UpdateItemCatalogueResource ( scopePerCurrentUrl , SessionUtil . getCurrentClientUrl ( getThreadLocalRequest ( ) ) . split ( " \\ ? " ) [ 0 ] ) . start ( ) ;
checksItemCatalogueResourceUpdated . put ( scopePerCurrentUrl , true ) ;
}
2018-02-22 16:08:22 +01:00
}
2017-12-01 18:06:25 +01:00
if ( pathInfoParameter = = null | | pathInfoParameter . isEmpty ( ) ) {
2020-07-03 17:15:11 +02:00
//pathInfoParameter is null or empty, in this case we are pointing to the Catalogue Home
pathInfoParameter = isViewPerVREEnabled ( browserLocationURL ) ;
2017-11-28 12:53:16 +01:00
}
2017-12-01 18:06:25 +01:00
2020-07-03 17:15:11 +02:00
CkanConnectorAccessPoint ckAP = getCkanConnectorAccessPoint ( browserLocationURL , pathInfoParameter , queryStringParameters , scopePerCurrentUrl ) ;
2016-09-05 18:18:57 +02:00
SessionUtil . saveCkanAccessPoint ( this . getThreadLocalRequest ( ) . getSession ( ) , scopePerCurrentUrl , ckAP ) ;
2019-12-05 14:26:23 +01:00
logger . info ( " Built the URI to CKAN (connector in case of user logged): " + ckAP . buildURI ( ) ) ;
2016-06-23 14:57:20 +02:00
logger . debug ( " returning ckanConnectorUri: " + ckAP ) ;
2016-06-23 12:49:45 +02:00
return ckAP ;
2016-06-13 09:48:29 +02:00
} catch ( Exception e ) {
2018-02-06 18:33:00 +01:00
String message = " Sorry an error occurred while contacting gCube Ckan Data Catalogue " ;
2016-06-13 09:48:29 +02:00
logger . error ( message , e ) ;
throw new Exception ( message ) ;
}
}
2016-06-20 15:22:21 +02:00
/ * *
2016-06-23 14:57:20 +02:00
* Gets the ckan connector access point .
2016-06-20 15:22:21 +02:00
*
2020-07-03 17:15:11 +02:00
* @param browserLocationURL the browser location URL
2016-06-23 14:57:20 +02:00
* @param pathInfoParameter the path info parameter
2016-06-20 15:22:21 +02:00
* @param queryStringParameters the query string parameters
2019-12-05 14:26:23 +01:00
* @param scopePerCurrentUrl the scope per current url
2016-06-23 14:57:20 +02:00
* @return the ckan connector access point
2019-12-05 14:26:23 +01:00
* @throws Exception the exception
2016-06-20 15:22:21 +02:00
* /
2020-07-03 17:15:11 +02:00
private CkanConnectorAccessPoint getCkanConnectorAccessPoint ( String browserLocationURL , String pathInfoParameter , String queryStringParameters , String scopePerCurrentUrl ) throws Exception {
2016-06-30 17:22:37 +02:00
2019-12-05 14:26:23 +01:00
if ( outsideLoginPortal ( ) ) {
2016-09-28 16:44:27 +02:00
CkanConnectorAccessPoint ckan = new CkanConnectorAccessPoint ( getCatalogue ( scopePerCurrentUrl ) . getCatalogueUrl ( ) , " " ) ;
2019-12-05 14:26:23 +01:00
ckan . setOutsideLoginOnPortal ( true ) ;
2017-10-24 19:21:29 +02:00
ckan . addPathInfo ( pathInfoParameter ) ;
ckan . addQueryString ( queryStringParameters ) ;
2016-06-29 18:35:01 +02:00
return ckan ;
}
2016-06-20 15:22:21 +02:00
2016-06-23 14:57:20 +02:00
//CKAN BASE URL
2016-07-18 12:39:45 +02:00
GcoreEndpointReader ckanEndPoint = null ;
try {
2016-09-05 18:18:57 +02:00
ckanEndPoint = SessionUtil . getCkanEndPoint ( this . getThreadLocalRequest ( ) . getSession ( ) , scopePerCurrentUrl ) ;
2016-07-18 12:39:45 +02:00
} catch ( Exception e ) {
logger . error ( " CkanConnectorAccessPoint error: " + e . getMessage ( ) ) ;
throw new Exception ( " Sorry, an error occurred during contacting d4Science Data Catalogue, try again later " ) ;
}
2016-06-23 14:57:20 +02:00
String ckanConnectorBaseUrl = ckanEndPoint . getCkanResourceEntyName ( ) ;
ckanConnectorBaseUrl = ckanConnectorBaseUrl . startsWith ( HTTP ) & & ! ckanConnectorBaseUrl . startsWith ( HTTPS ) ? ckanConnectorBaseUrl . replaceFirst ( HTTP , HTTPS ) : ckanConnectorBaseUrl ;
ckanConnectorBaseUrl = ckanConnectorBaseUrl . contains ( PORT_HTTP ) ? ckanConnectorBaseUrl . replace ( PORT_HTTP , PORT_HTTPS ) : ckanConnectorBaseUrl ;
logger . debug ( " Base URL is: " + ckanConnectorBaseUrl ) ;
2016-06-29 11:25:46 +02:00
2016-06-23 14:57:20 +02:00
//GET CONTEXT
String ckanContext = getServletContext ( ) . getInitParameter ( CKANCONNECTORCONTEXT ) ;
logger . debug ( CKANCONNECTORCONTEXT + " is: " + ckanContext ) ;
ckanContext = ckanContext ! = null ? ckanContext : " " ;
2016-06-23 12:49:45 +02:00
2016-07-01 16:51:51 +02:00
CkanConnectorAccessPoint ckan = new CkanConnectorAccessPoint ( ckanConnectorBaseUrl , ckanContext ) ;
2016-06-29 11:25:46 +02:00
pathInfoParameter = CkanConnectorAccessPoint . checkURLPathSeparator ( pathInfoParameter , true , false ) ;
2016-06-23 14:57:20 +02:00
logger . debug ( " External Path Info parameter: " + pathInfoParameter ) ;
2016-06-23 12:49:45 +02:00
2016-06-23 14:57:20 +02:00
//ADD PATH INFO
2016-06-29 11:25:46 +02:00
ckan . addPathInfo ( pathInfoParameter ) ;
2016-06-23 14:57:20 +02:00
logger . debug ( " CKanConnector pathInfo: " + ckan . getPathInfoParameter ( ) ) ;
ckan . addQueryString ( queryStringParameters ) ;
2016-06-23 12:49:45 +02:00
2016-06-23 14:57:20 +02:00
//GET TOKEN
2016-06-23 12:49:45 +02:00
String gcubeTokenValue = null ;
2016-11-25 10:37:56 +01:00
gcubeTokenValue = getGcubeSecurityToken ( scopePerCurrentUrl ) ;
2016-06-20 16:05:58 +02:00
2016-09-14 16:42:46 +02:00
// set the token into the CkanConnectorAccessPoint
2016-06-23 12:49:45 +02:00
ckan . addGubeToken ( gcubeTokenValue ) ;
2016-06-21 16:12:27 +02:00
//ADDING LIST OF VRE TO WHICH USER BELONGS
2016-06-23 12:49:45 +02:00
if ( ! SessionUtil . isIntoPortal ( ) ) {
return ckan ;
}
2017-01-27 11:27:40 +01:00
// retrieve the list of VREs to whom the user belongs (actually one vre at most is sent)
2017-01-26 18:47:46 +01:00
Map < String , String > roleForVre = UserUtil . getVreRoleForUser (
2018-02-09 15:39:52 +01:00
SessionUtil . getCurrentUser (
getThreadLocalRequest ( ) ) . getEmail ( ) ,
scopePerCurrentUrl ,
2018-08-30 17:35:45 +02:00
getCatalogue ( scopePerCurrentUrl ) ,
2020-07-03 17:15:11 +02:00
isViewPerVREEnabled ( browserLocationURL ) ! = null ,
2018-02-09 15:39:52 +01:00
getThreadLocalRequest ( ) . getSession ( ) ) ;
2017-01-25 18:30:53 +01:00
ckan . addListOfVREs ( roleForVre ) ;
2016-06-23 12:49:45 +02:00
return ckan ;
2016-06-20 15:22:21 +02:00
}
2016-06-13 09:48:29 +02:00
/ * *
2019-12-05 14:26:23 +01:00
* Gets the gcube security token for the user in current session and for a given scope .
2016-06-13 09:48:29 +02:00
*
2019-12-05 14:26:23 +01:00
* @param context the context
2016-06-13 09:48:29 +02:00
* @return the gcube security token
2019-12-05 14:26:23 +01:00
* @throws Exception the exception
2016-06-13 09:48:29 +02:00
* /
2016-11-17 14:17:29 +01:00
protected String getGcubeSecurityToken ( String context ) throws Exception {
2016-06-13 09:48:29 +02:00
2016-09-14 16:42:46 +02:00
// ask it directly to the auth service
2016-11-25 10:37:56 +01:00
String username = SessionUtil . getCurrentUser ( getThreadLocalRequest ( ) ) . getUsername ( ) ;
2016-11-17 14:17:29 +01:00
String token = null ;
try {
logger . debug ( " Checking if token for user " + username + " in context " + context + " already exists... " ) ;
2019-06-10 10:31:43 +02:00
//ADDED by Francesco Mangiacrapa
if ( SessionUtil . isIntoPortal ( ) ) {
token = authorizationService ( ) . resolveTokenByUserAndContext ( username , context ) ;
logger . debug ( " It exists! " ) ;
} else {
logger . debug ( " Out of portal, I'm reading the user token from PortalContext " ) ;
token = PortalContext . getConfiguration ( ) . getCurrentUserToken ( username , context ) ;
}
2016-11-17 14:17:29 +01:00
} catch ( ObjectNotFound e ) {
logger . info ( " Creating token for user " + username + " and context " + context ) ;
token = authorizationService ( ) . generateUserToken ( new UserInfo ( username , new ArrayList < String > ( ) ) , context ) ;
logger . debug ( " received token: " + token . substring ( 0 , 5 ) + " *********************** " ) ;
}
2016-09-14 16:42:46 +02:00
return token ;
2016-06-13 09:48:29 +02:00
}
/ * ( non - Javadoc )
* @see org . gcube . portlets . gcubeckan . gcubeckandatacatalog . client . GcubeCkanDataCatalogService # getMyRole ( )
* /
@Override
2017-01-25 18:30:53 +01:00
public RolesCkanGroupOrOrg getMyRole ( ) {
2016-06-14 17:42:59 +02:00
2016-06-30 17:22:37 +02:00
// base role as default value
2017-01-25 18:30:53 +01:00
RolesCkanGroupOrOrg toReturn = RolesCkanGroupOrOrg . MEMBER ;
2016-06-18 10:25:17 +02:00
2016-06-30 17:22:37 +02:00
if ( ! SessionUtil . isIntoPortal ( ) ) {
2017-01-25 18:30:53 +01:00
logger . warn ( " OUT FROM PORTAL DETECTED RETURNING ROLE: " + RolesCkanGroupOrOrg . ADMIN ) ;
toReturn = RolesCkanGroupOrOrg . ADMIN ;
2016-06-30 17:22:37 +02:00
} else {
2016-06-14 17:42:59 +02:00
2016-06-30 17:22:37 +02:00
HttpSession httpSession = this . getThreadLocalRequest ( ) . getSession ( ) ;
2016-11-25 10:37:56 +01:00
String username = SessionUtil . getCurrentUser ( getThreadLocalRequest ( ) ) . getUsername ( ) ;
2016-07-04 12:44:58 +02:00
2016-11-25 10:37:56 +01:00
// get the scope
String scopePerCurrentUrl = SessionUtil . getScopeFromClientUrl ( getThreadLocalRequest ( ) ) ;
2016-09-05 18:18:57 +02:00
2016-11-25 10:37:56 +01:00
// get key per scope
2018-02-23 16:19:07 +01:00
String keyPerScopeRole = CatalogueUtilMethods . concatenateSessionKeyScope ( SessionCatalogueAttributes . CKAN_HIGHEST_ROLE , scopePerCurrentUrl ) ;
String keyPerScopeOrganizations = CatalogueUtilMethods . concatenateSessionKeyScope ( SessionCatalogueAttributes . CKAN_ORGANIZATIONS_PUBLISH_KEY , scopePerCurrentUrl ) ;
String keyPerScopeGroups = CatalogueUtilMethods . concatenateSessionKeyScope ( SessionCatalogueAttributes . CKAN_GROUPS_MEMBER , scopePerCurrentUrl ) ;
2016-11-04 13:01:35 +01:00
2016-11-25 10:37:56 +01:00
// check into session
if ( httpSession . getAttribute ( keyPerScopeRole ) ! = null ) {
2016-06-14 17:42:59 +02:00
2017-01-25 18:30:53 +01:00
toReturn = ( RolesCkanGroupOrOrg ) httpSession . getAttribute ( keyPerScopeRole ) ;
2016-11-25 10:37:56 +01:00
logger . info ( " Found user role into session " + toReturn + " and it is going to be returned for user " + username ) ;
2016-06-18 13:58:31 +02:00
2016-11-25 10:37:56 +01:00
} else {
2016-06-18 13:58:31 +02:00
2016-11-25 10:37:56 +01:00
try {
GroupManager gm = new LiferayGroupManager ( ) ;
String groupName = gm . getGroup ( gm . getGroupIdFromInfrastructureScope ( scopePerCurrentUrl ) ) . getGroupName ( ) ;
2016-09-05 18:18:57 +02:00
2016-11-25 10:37:56 +01:00
// we build up also a list that keeps track of the scopes (orgs) in which the user has at least role EDITOR
List < OrganizationBean > orgsInWhichAtLeastEditorRole = new ArrayList < OrganizationBean > ( ) ;
toReturn = UserUtil . getHighestRole ( scopePerCurrentUrl , username , groupName , this , orgsInWhichAtLeastEditorRole ) ;
2016-11-04 13:01:35 +01:00
2016-11-25 10:37:56 +01:00
// put role in session
httpSession . setAttribute ( keyPerScopeRole , toReturn ) ;
logger . info ( " Set role " + toReturn + " into session for user " + username ) ;
2016-09-05 18:18:57 +02:00
2016-11-25 10:37:56 +01:00
// if he is an admin/editor preload:
// 1) organizations in which he can publish (the widget will find these info in session)
// 2) the list of groups
2017-01-25 18:30:53 +01:00
if ( ! toReturn . equals ( RolesCkanGroupOrOrg . MEMBER ) ) {
2016-11-25 10:37:56 +01:00
httpSession . setAttribute ( keyPerScopeOrganizations , orgsInWhichAtLeastEditorRole ) ;
httpSession . setAttribute ( keyPerScopeGroups , fetchUserGroups ( scopePerCurrentUrl , username ) ) ;
2016-06-30 17:22:37 +02:00
}
2016-11-25 10:37:56 +01:00
} catch ( Exception e ) {
logger . error ( " Error while retreving roles... returning " + toReturn , e ) ;
2016-06-30 17:22:37 +02:00
}
}
2016-06-14 17:42:59 +02:00
}
2016-06-30 17:22:37 +02:00
2017-02-01 18:11:54 +01:00
logger . debug ( " Returning highest role " + toReturn ) ;
2016-11-25 10:37:56 +01:00
2016-07-20 19:14:46 +02:00
// return the role
2016-06-30 17:22:37 +02:00
return toReturn ;
2016-06-13 09:48:29 +02:00
}
2016-06-13 12:54:15 +02:00
2016-11-17 14:31:01 +01:00
/ * *
2019-12-05 14:26:23 +01:00
* Fetch the list of ckan groups for which the user is member .
*
* @param context the context
* @param username the username
* @return the list
2016-11-17 14:31:01 +01:00
* /
2017-04-27 16:45:15 +02:00
private List < OrganizationBean > fetchUserGroups ( String context , String username ) {
2016-11-17 14:31:01 +01:00
2017-04-27 16:45:15 +02:00
List < OrganizationBean > toReturn = null ;
2016-11-17 14:31:01 +01:00
logger . info ( " Preloading user's groups " ) ;
try {
DataCatalogue catalogue = getCatalogue ( context ) ;
String apiKey = catalogue . getApiKeyFromUsername ( username ) ;
2017-04-27 16:45:15 +02:00
toReturn = new ArrayList < OrganizationBean > ( ) ;
2017-01-27 11:27:40 +01:00
Map < String , Map < CkanGroup , RolesCkanGroupOrOrg > > mapRoleGroup = catalogue . getUserRoleByGroup ( username , apiKey ) ;
Set < Entry < String , Map < CkanGroup , RolesCkanGroupOrOrg > > > set = mapRoleGroup . entrySet ( ) ;
for ( Entry < String , Map < CkanGroup , RolesCkanGroupOrOrg > > entry : set ) {
Set < Entry < CkanGroup , RolesCkanGroupOrOrg > > subSet = entry . getValue ( ) . entrySet ( ) ;
for ( Entry < CkanGroup , RolesCkanGroupOrOrg > subEntry : subSet ) {
2017-04-27 16:45:15 +02:00
toReturn . add ( new OrganizationBean ( subEntry . getKey ( ) . getTitle ( ) , subEntry . getKey ( ) . getName ( ) , false ) ) ;
2017-01-27 11:27:40 +01:00
}
2016-11-17 14:31:01 +01:00
}
2017-01-27 11:27:40 +01:00
2016-11-17 14:31:01 +01:00
logger . debug ( " List of groups to return is " + toReturn ) ;
} catch ( Exception e ) {
logger . error ( " Failed to preload user's groups " ) ;
}
return toReturn ;
}
2017-02-15 18:49:48 +01:00
// @Override
// public String logoutURIFromCkan() {
// String username = SessionUtil.getCurrentUser(getThreadLocalRequest()).getUsername();
//
// // get the scope from session
// String scopePerCurrentUrl = SessionUtil.getScopeFromClientUrl(getThreadLocalRequest());
//
// CkanConnectorAccessPoint ckanAP = SessionUtil.getCkanAccessPoint(this.getThreadLocalRequest().getSession(), scopePerCurrentUrl);
// logger.info("Logout from CKAN for: "+username +" by token: "+ckanAP.getGcubeTokenValue());
//
// String ckanConnectorLogut = getServletContext().getInitParameter(CKANCONNECTORLOGOUT);
// logger.debug(CKANCONNECTORLOGOUT + " is: "+ckanConnectorLogut);
//
// CkanConnectorAccessPoint ckan = new CkanConnectorAccessPoint(ckanAP.getBaseUrl(), ckanConnectorLogut);
// ckan.addGubeToken(ckanAP.getGcubeTokenValue());
// return ckan.buildURI();
// }
2016-06-28 12:45:24 +02:00
2016-06-23 12:49:45 +02:00
/ * ( non - Javadoc )
* @see org . gcube . portlets . gcubeckan . gcubeckandatacatalog . client . GcubeCkanDataCatalogService # logoutFromCkan ( )
* /
@Override
2016-11-04 13:01:35 +01:00
public String logoutFromCkanURL ( ) {
2017-02-15 18:49:48 +01:00
2017-02-03 18:24:11 +01:00
// check if the portlet is accessed from outside the portal
2019-12-05 14:26:23 +01:00
if ( outsideLoginPortal ( ) )
2017-02-03 18:24:11 +01:00
return null ;
2016-11-04 13:01:35 +01:00
2016-06-23 12:49:45 +02:00
HttpSession httpSession = this . getThreadLocalRequest ( ) . getSession ( ) ;
2016-11-25 10:37:56 +01:00
String username = SessionUtil . getCurrentUser ( getThreadLocalRequest ( ) ) . getUsername ( ) ;
2016-11-29 17:20:50 +01:00
2016-11-25 10:37:56 +01:00
if ( username = = null ) {
logger . warn ( " User is null " ) ;
return null ;
}
2016-11-29 17:20:50 +01:00
2016-11-04 13:01:35 +01:00
logger . info ( " Getting CKAN Logout URL... " ) ;
2016-11-25 10:37:56 +01:00
String scopePerCurrentUrl = SessionUtil . getScopeFromClientUrl ( getThreadLocalRequest ( ) ) ;
2016-11-04 13:01:35 +01:00
CkanConnectorAccessPoint ckanAP = SessionUtil . getCkanAccessPoint ( httpSession , scopePerCurrentUrl ) ;
// String token = getGcubeSecurityToken();
logger . info ( " Logout from CKAN for: " + username ) ;
logger . info ( " by token: " + ckanAP . getGcubeTokenValue ( ) + " , the scope is: " + scopePerCurrentUrl ) ;
String ckanConnectorLogout = getServletContext ( ) . getInitParameter ( CKANCONNECTORLOGOUT ) ;
logger . debug ( GcubeCkanDataCatalogServiceImpl . CKANCONNECTORLOGOUT + " is: " + ckanConnectorLogout ) ;
CkanConnectorAccessPoint ckan = new CkanConnectorAccessPoint ( ckanAP . getBaseUrl ( ) , ckanConnectorLogout ) ;
ckan . addGubeToken ( ckanAP . getGcubeTokenValue ( ) ) ;
String deleteURI = ckan . buildURI ( ) ;
logger . debug ( GcubeCkanDataCatalogServiceImpl . CKANCONNECTORLOGOUT + " returning: " + deleteURI ) ;
return deleteURI ;
2016-06-27 11:31:13 +02:00
}
2016-06-27 12:06:28 +02:00
2019-12-05 14:26:23 +01:00
/ * ( non - Javadoc )
* @see org . gcube . portlets . gcubeckan . gcubeckandatacatalog . client . GcubeCkanDataCatalogService # getCkanOrganizationsNamesAndUrlsForUser ( )
* /
2016-06-29 16:50:18 +02:00
@Override
2016-11-21 10:12:24 +01:00
public List < BeanUserInOrgGroupRole > getCkanOrganizationsNamesAndUrlsForUser ( ) {
2016-07-22 11:16:50 +02:00
2016-11-21 10:12:24 +01:00
List < BeanUserInOrgGroupRole > toReturn = new ArrayList < BeanUserInOrgGroupRole > ( ) ;
2016-06-29 16:50:18 +02:00
if ( ! SessionUtil . isIntoPortal ( ) ) {
logger . warn ( " You are not into the portal " ) ;
2017-01-25 18:30:53 +01:00
BeanUserInOrgGroupRole org = new BeanUserInOrgGroupRole ( " testVRE " , " /organization/devvre " , RolesCkanGroupOrOrg . ADMIN ) ;
2016-09-27 18:38:33 +02:00
toReturn . add ( org ) ;
2016-06-29 16:50:18 +02:00
2016-06-30 17:22:37 +02:00
} else {
2016-06-29 16:50:18 +02:00
2016-06-30 17:22:37 +02:00
HttpSession httpSession = this . getThreadLocalRequest ( ) . getSession ( ) ;
2016-11-25 10:37:56 +01:00
String username = SessionUtil . getCurrentUser ( getThreadLocalRequest ( ) ) . getUsername ( ) ;
2016-06-30 17:22:37 +02:00
2016-09-05 18:18:57 +02:00
// retrieve scope per current portlet url
2016-11-25 10:37:56 +01:00
String scopePerCurrentUrl = SessionUtil . getScopeFromClientUrl ( getThreadLocalRequest ( ) ) ;
2018-02-23 16:19:07 +01:00
String keyPerScope = CatalogueUtilMethods . concatenateSessionKeyScope ( SessionCatalogueAttributes . CKAN_ORGS_USER_KEY , scopePerCurrentUrl ) ;
2016-07-04 12:44:58 +02:00
2016-11-25 10:37:56 +01:00
// check if the aslsession already has such information
if ( httpSession . getAttribute ( keyPerScope ) ! = null ) {
toReturn = ( List < BeanUserInOrgGroupRole > ) httpSession . getAttribute ( keyPerScope ) ;
logger . debug ( " List of organizations was into the session " + toReturn ) ;
} else {
logger . debug ( " Organizations list wasn't into session, retrieving them " ) ;
DataCatalogue catalogue = getCatalogue ( scopePerCurrentUrl ) ;
String apiKey = catalogue . getApiKeyFromUsername ( username ) ;
2017-01-27 11:27:40 +01:00
Map < String , Map < CkanOrganization , RolesCkanGroupOrOrg > > mapRoleGroup = catalogue . getUserRoleByOrganization ( username , apiKey ) ;
Set < Entry < String , Map < CkanOrganization , RolesCkanGroupOrOrg > > > set = mapRoleGroup . entrySet ( ) ;
for ( Entry < String , Map < CkanOrganization , RolesCkanGroupOrOrg > > entry : set ) {
Set < Entry < CkanOrganization , RolesCkanGroupOrOrg > > subSet = entry . getValue ( ) . entrySet ( ) ;
for ( Entry < CkanOrganization , RolesCkanGroupOrOrg > subEntry : subSet ) {
BeanUserInOrgGroupRole org = new BeanUserInOrgGroupRole ( subEntry . getKey ( ) . getTitle ( ) , " /organization/ " + subEntry . getKey ( ) . getName ( ) , subEntry . getValue ( ) ) ;
toReturn . add ( org ) ;
}
2016-06-30 17:22:37 +02:00
}
2016-11-25 10:37:56 +01:00
logger . debug ( " List of organizations to return for user " + username + " is " + toReturn ) ;
httpSession . setAttribute ( keyPerScope , toReturn ) ;
2016-06-29 16:50:18 +02:00
}
}
return toReturn ;
}
2016-11-25 10:37:56 +01:00
2016-11-21 10:12:24 +01:00
2019-12-05 14:26:23 +01:00
/ * ( non - Javadoc )
* @see org . gcube . portlets . gcubeckan . gcubeckandatacatalog . client . GcubeCkanDataCatalogService # getCkanGroupsNamesAndUrlsForUser ( )
* /
2016-11-21 10:12:24 +01:00
@Override
public List < BeanUserInOrgGroupRole > getCkanGroupsNamesAndUrlsForUser ( ) {
List < BeanUserInOrgGroupRole > toReturn = new ArrayList < BeanUserInOrgGroupRole > ( ) ;
if ( ! SessionUtil . isIntoPortal ( ) ) {
logger . warn ( " You are not into the portal " ) ;
2017-01-25 18:30:53 +01:00
BeanUserInOrgGroupRole org = new BeanUserInOrgGroupRole ( " testGroup " , " /group/testgroup " , RolesCkanGroupOrOrg . MEMBER ) ;
2016-11-21 10:12:24 +01:00
toReturn . add ( org ) ;
} else {
2016-11-25 10:37:56 +01:00
HttpSession httpSession = getThreadLocalRequest ( ) . getSession ( ) ;
String username = SessionUtil . getCurrentUser ( getThreadLocalRequest ( ) ) . getUsername ( ) ;
2016-11-21 10:12:24 +01:00
// retrieve scope per current portlet url
2016-11-25 10:37:56 +01:00
String scopePerCurrentUrl = SessionUtil . getScopeFromClientUrl ( getThreadLocalRequest ( ) ) ;
2018-02-23 16:19:07 +01:00
String keyPerScope = CatalogueUtilMethods . concatenateSessionKeyScope ( SessionCatalogueAttributes . CKAN_GROUPS_USER_KEY , scopePerCurrentUrl ) ;
2016-11-21 10:12:24 +01:00
2016-11-25 10:37:56 +01:00
// check if the aslsession already has such information
if ( httpSession . getAttribute ( keyPerScope ) ! = null ) {
toReturn = ( List < BeanUserInOrgGroupRole > ) httpSession . getAttribute ( keyPerScope ) ;
logger . debug ( " List of groups was into the session " + toReturn ) ;
} else {
logger . debug ( " Groups list wasn't into session, retrieving them " ) ;
DataCatalogue catalogue = getCatalogue ( scopePerCurrentUrl ) ;
String apiKey = catalogue . getApiKeyFromUsername ( username ) ;
2017-01-27 11:27:40 +01:00
Map < String , Map < CkanGroup , RolesCkanGroupOrOrg > > mapRoleGroup = catalogue . getUserRoleByGroup ( username , apiKey ) ;
Set < Entry < String , Map < CkanGroup , RolesCkanGroupOrOrg > > > set = mapRoleGroup . entrySet ( ) ;
for ( Entry < String , Map < CkanGroup , RolesCkanGroupOrOrg > > entry : set ) {
Set < Entry < CkanGroup , RolesCkanGroupOrOrg > > subSet = entry . getValue ( ) . entrySet ( ) ;
for ( Entry < CkanGroup , RolesCkanGroupOrOrg > subEntry : subSet ) {
BeanUserInOrgGroupRole org = new BeanUserInOrgGroupRole ( subEntry . getKey ( ) . getTitle ( ) , " /group/ " + subEntry . getKey ( ) . getName ( ) , subEntry . getValue ( ) ) ;
toReturn . add ( org ) ;
}
2016-11-21 10:12:24 +01:00
}
2016-12-13 17:46:40 +01:00
logger . debug ( " List of groups to return for user " + username + " is " + toReturn ) ;
2016-11-25 10:37:56 +01:00
httpSession . setAttribute ( keyPerScope , toReturn ) ;
2016-11-21 10:12:24 +01:00
}
2016-11-25 10:37:56 +01:00
2016-11-21 10:12:24 +01:00
}
return toReturn ;
}
2016-06-29 16:50:18 +02:00
2019-12-05 14:26:23 +01:00
/ *
* Returns true either you are outside gateway ( in eclipse mode )
* or you are on the gateway and the user is not logged ( i . e . public VRE catalogue )
*
* /
2016-06-29 18:35:01 +02:00
@Override
2019-12-05 14:26:23 +01:00
public boolean outsideLoginPortal ( ) {
2016-06-29 18:35:01 +02:00
2016-06-30 17:22:37 +02:00
if ( ! SessionUtil . isIntoPortal ( ) ) {
2016-09-14 16:42:46 +02:00
logger . warn ( " You are in DEV mode " ) ;
2016-06-30 17:22:37 +02:00
return false ;
} else {
2016-11-25 10:37:56 +01:00
String username = null ;
try {
username = SessionUtil . getCurrentUser ( getThreadLocalRequest ( ) ) . getUsername ( ) ;
} catch ( Exception e ) {
logger . warn ( " Maybe there is no user " ) ;
}
return username = = null | | username . isEmpty ( ) ;
2016-06-30 17:22:37 +02:00
}
2016-06-29 18:35:01 +02:00
}
2016-11-29 17:20:50 +01:00
2019-12-05 14:26:23 +01:00
/ * ( non - Javadoc )
* @see org . gcube . portlets . gcubeckan . gcubeckandatacatalog . client . GcubeCkanDataCatalogService # isManageProductEnabled ( )
* /
2016-11-29 17:20:50 +01:00
@Override
public boolean isManageProductEnabled ( ) {
2017-02-16 18:36:09 +01:00
logger . info ( " Checking if the manage product button needs to be shown or not for the current context " ) ;
2016-11-29 17:20:50 +01:00
String scopePerCurrentUrl = SessionUtil . getScopeFromClientUrl ( getThreadLocalRequest ( ) ) ;
DataCatalogue catalogue = getCatalogue ( scopePerCurrentUrl ) ;
if ( catalogue = = null ) {
logger . warn ( " There is no catalogue instance here..., returning false " ) ;
return false ;
}
2016-12-26 22:17:09 +01:00
else {
2016-12-26 23:12:33 +01:00
try {
2017-02-16 18:36:09 +01:00
boolean toReturn = catalogue . isManageProductEnabled ( ) ;
2017-01-02 11:00:17 +01:00
logger . info ( " Will manage product be enabled for this user? " + Boolean . toString ( toReturn ) ) ;
return toReturn ;
2016-12-26 23:12:33 +01:00
} catch ( Exception e ) {
logger . error ( " Unable to determine if the manage product needs to be shown or not " , e ) ;
return false ;
2016-12-26 22:17:09 +01:00
}
}
2016-11-29 17:20:50 +01:00
}
2017-11-28 12:53:16 +01:00
/ * *
2019-12-05 14:26:23 +01:00
* Ask to liferay .
*
2020-07-03 17:15:11 +02:00
* @param browserLocationURL the browser location URL
2019-12-05 14:26:23 +01:00
* @return the string
2017-11-28 12:53:16 +01:00
* /
2017-11-28 14:41:53 +01:00
@Override
2020-07-03 17:15:11 +02:00
public String isViewPerVREEnabled ( String browserLocationURL ) {
2017-11-28 12:53:16 +01:00
2017-12-01 18:06:25 +01:00
String toReturn = null ;
2019-12-05 14:26:23 +01:00
String scopePerCurrentUrl = SessionUtil . getScopeFromClientUrl ( getThreadLocalRequest ( ) ) ; // the view per vre can be managed independently for each context SessionUtil.getScopeFromClientUrl(getThreadLocalRequest());
//String scopePerCurrentUrl = PortalContext.getConfiguration().getCurrentScope(getThreadLocalRequest());
logger . debug ( " isViewPerVREEnabled called, read context: " + scopePerCurrentUrl ) ;
2017-12-01 18:06:25 +01:00
boolean enabled = false ;
2017-11-28 12:53:16 +01:00
try {
2019-12-05 14:26:23 +01:00
if ( ! SessionUtil . isIntoPortal ( ) ) {
2020-01-21 17:12:29 +01:00
enabled = false ;
logger . warn ( " Enabling View per VRE in DEV mode? " + enabled ) ;
if ( enabled )
return " /organization_vre/ " + scopePerCurrentUrl . substring ( scopePerCurrentUrl . lastIndexOf ( " / " ) + 1 , scopePerCurrentUrl . length ( ) ) . toLowerCase ( ) ; //is the VRE name
return null ;
2019-12-05 14:26:23 +01:00
}
2017-11-28 12:53:16 +01:00
GroupManager gm = new LiferayGroupManager ( ) ;
enabled = ( boolean ) gm . readCustomAttr ( gm . getGroupIdFromInfrastructureScope ( scopePerCurrentUrl ) , VIEW_PER_ORGANIZATION_LIFERAY_CUSTOM_FIELD ) ;
2020-07-03 17:15:11 +02:00
2017-12-01 18:06:25 +01:00
if ( enabled ) {
String groupName = gm . getGroup ( gm . getGroupIdFromInfrastructureScope ( scopePerCurrentUrl ) ) . getGroupName ( ) . toLowerCase ( ) ;
2020-07-03 17:15:11 +02:00
logger . info ( " VRE name (toLowerCase) read by LF GroupManager is: " + groupName ) ;
String secureVREName = " / " + groupName + " / " ;
if ( browserLocationURL . toLowerCase ( ) . contains ( secureVREName ) ) {
//this should mean the caller (the location URL) is from VRE environment
logger . info ( " The browserLocationURL: " + browserLocationURL + " contains the string /VRE name/ ' " + secureVREName + " '. The Catalogue Portlet should be at VRE level, reading configuration isViewPerVREEnabled " ) ;
toReturn = " /organization_vre/ " + groupName ;
} else {
//this should mean the caller is out from VRE environment,so should be a public or gateway catalogue
logger . info ( " The browserLocationURL: " + browserLocationURL + " DOES NOT contain the string /VRE name/ ' " + secureVREName + " '. The Catalogue Portlet should NOT be at VRE level. Ignoring configuration isViewPerVREEnabled " ) ;
}
2017-12-01 18:06:25 +01:00
}
logger . debug ( " Read value for " + VIEW_PER_ORGANIZATION_LIFERAY_CUSTOM_FIELD + " is " + enabled + " and path is " + toReturn ) ;
2020-07-03 17:15:11 +02:00
2017-11-28 12:53:16 +01:00
} catch ( Exception e ) {
logger . error ( " Failed to parse custom field value " , e ) ;
}
2017-12-01 18:06:25 +01:00
return toReturn ;
2017-11-28 12:53:16 +01:00
}
2016-06-13 09:48:29 +02:00
}