The list of organizations in which a sysadmin can publish has every ckan organization

git-svn-id: http://svn.d4science-ii.research-infrastructures.eu/gcube/trunk/portlets/widgets/ckan-metadata-publisher-widget@129156 82a268e6-3cf1-43bd-a215-b396298e98cf
This commit is contained in:
Costantino Perciante 2016-06-18 08:13:37 +00:00
parent 96147b6e65
commit 445d2d364a
1 changed files with 28 additions and 20 deletions

View File

@ -51,14 +51,14 @@ public class CKANPublisherServicesImpl extends RemoteServiceServlet implements C
// Logger
private static final org.slf4j.Logger logger = LoggerFactory.getLogger(CKANPublisherServicesImpl.class);
public static final String TEST_SCOPE = "/gcube/devsec/devVRE";
public static final String CKAN_TOKEN_KEY = "ckanToken";
// library util instance
private CKanUtilsImpl instance;
@Override
public void init(){
// discover the library
@ -106,17 +106,17 @@ public class CKANPublisherServicesImpl extends RemoteServiceServlet implements C
ASLSession session = getASLSession();
String username = session.getUsername();
logger.debug("User in session is " + username);
String token = null;
if(this.getThreadLocalRequest().getSession().getAttribute(CKAN_TOKEN_KEY) != null)
token = (String)this.getThreadLocalRequest().getSession().getAttribute(CKAN_TOKEN_KEY);
else{
token = instance.getApiKeyFromUsername(username);
this.getThreadLocalRequest().getSession().setAttribute(CKAN_TOKEN_KEY, token);
logger.debug("Ckan token has been set for user " + username);
}
logger.debug("Found ckan token " + token + " for user " + username);
return token;
@ -124,29 +124,37 @@ public class CKANPublisherServicesImpl extends RemoteServiceServlet implements C
/**
* Retrieve the list of organizations in which the user can publish (roles ADMIN, EDITOR)
* If he is a SYSADMIN, he can publish everywhere
* @param username
* @return the list of organizations
*/
private List<String> getUserOrganizationsList(String username) {
logger.debug("Request for user " + username + " organizations list");
// We need to retrieve orgs in which the user has the roles ADMIN, EDITOR
List<RolesIntoOrganization> rolesToMatch = new ArrayList<RolesIntoOrganization>();
rolesToMatch.add(RolesIntoOrganization.EDITOR);
rolesToMatch.add(RolesIntoOrganization.ADMIN);
List<String> orgsName = new ArrayList<String>();
Map<String, List<RolesIntoOrganization>> orgsAndRoles = instance.getGroupsAndRolesByUser(username, rolesToMatch);
Iterator<Entry<String, List<RolesIntoOrganization>>> iterator = orgsAndRoles.entrySet().iterator();
// get the names
while (iterator.hasNext()) {
Map.Entry<String, java.util.List<RolesIntoOrganization>> entry = (Map.Entry<String, java.util.List<RolesIntoOrganization>>) iterator
.next();
orgsName.add(entry.getKey());
if(instance.isSysAdmin(username, getUserCKanTokenFromSession())){
logger.debug("The user " + username + " is a sysadmin. He can publish everywhere");
orgsName = instance.getOrganizationsNames();
}else{
// We need to retrieve orgs in which the user has the roles ADMIN, EDITOR
List<RolesIntoOrganization> rolesToMatch = new ArrayList<RolesIntoOrganization>();
rolesToMatch.add(RolesIntoOrganization.EDITOR);
rolesToMatch.add(RolesIntoOrganization.ADMIN);
Map<String, List<RolesIntoOrganization>> orgsAndRoles = instance.getGroupsAndRolesByUser(username, rolesToMatch);
Iterator<Entry<String, List<RolesIntoOrganization>>> iterator = orgsAndRoles.entrySet().iterator();
// get the names
while (iterator.hasNext()) {
Map.Entry<String, java.util.List<RolesIntoOrganization>> entry = (Map.Entry<String, java.util.List<RolesIntoOrganization>>) iterator
.next();
orgsName.add(entry.getKey());
}
}
return orgsName;
}