added method getGroupsByUser

git-svn-id: https://svn.d4science.research-infrastructures.eu/gcube/trunk/data-catalogue/ckan-util-library@134405 82a268e6-3cf1-43bd-a215-b396298e98cf
This commit is contained in:
Costantino Perciante 2016-11-21 08:50:43 +00:00
parent 5a110d07df
commit 3d4d22ae96
3 changed files with 49 additions and 1 deletions

View File

@ -40,6 +40,13 @@ public interface DataCatalogue {
* @return a list of organizations
*/
List<CkanOrganization> getOrganizationsByUser(String username);
/**
* Returns the list of groups to whom the user belongs (with any role)
* @param username
* @return a list of groups
*/
List<CkanGroup> getGroupsByUser(String username);
/**
* Returns the list of organizations' names to whom the user belongs (with any role)

View File

@ -296,6 +296,46 @@ public class DataCatalogueImpl implements DataCatalogue{
}
return toReturn;
}
@Override
public List<CkanGroup> getGroupsByUser(String username) {
logger.debug("Requested groups for user " + username);
// checks
checkNotNull(username);
// in order to avoid errors, the username is always converted
String ckanUsername = UtilMethods.fromUsernameToCKanUsername(username);
// list to return
List<CkanGroup> toReturn = new ArrayList<CkanGroup>();
try{
// get the list of all organizations
List<CkanGroup> groups = client.getGroupList();
// iterate over them
for (CkanGroup ckanGroup : groups) {
List<CkanUser> users = client.getGroup(ckanGroup.getName()).getUsers();
// check if the current user is among them
for (CkanUser ckanUser : users) {
if(ckanUser.getName().equals(ckanUsername)){
logger.debug("User " + ckanUsername + " is into " + ckanGroup.getName());
toReturn.add(ckanGroup);
break;
}
}
}
}catch(Exception e){
logger.error("Unable to get user's organizations", e);
}
return toReturn;
}
@Override

View File

@ -7,7 +7,7 @@ package org.gcube.datacatalogue.ckanutillibrary.utils;
public class SessionCatalogueAttributes {
// CKAN KEYS (PLEASE NOTE THAT MOST OF THESE INFO ARE SAVED INTO SESSION PER SCOPE)
public static final String CKAN_ORGS_USER_KEY = "ckanOrgs"; // organizations to whom he belongs
public static final String CKAN_ORGS_USER_KEY = "ckanOrgs"; // organizations to whom he belongs (shown into the portlet)
public static final String CKAN_HIGHEST_ROLE = "ckanHighestRole"; // editor, member, admin
public static final String CKAN_ORGANIZATIONS_PUBLISH_KEY = "ckanOrganizationsPublish"; // here he can publish (admin/editor role)
public final static String SCOPE_CLIENT_PORTLET_URL = "currentClientUrlPortletScope"; // scope in which we need to discover
@ -15,4 +15,5 @@ public class SessionCatalogueAttributes {
public static final String CKAN_PROFILES_KEY = "ckanProfiles"; // product profiles
public static final String CKAN_PUBLISH_WORKSPACE = "ckanCatalogueInWorkspace";
public static final String CKAN_GROUPS_MEMBER = "ckanGroupsMember";
public static final String CKAN_GROUPS_USER_KEY = "ckanGroups"; // to show the list of groups in the portlet
}