diff --git a/src/main/java/org/gcube/datacatalogue/ckanutillibrary/CKanUtilsImpl.java b/src/main/java/org/gcube/datacatalogue/ckanutillibrary/CKanUtilsImpl.java index 670d3c1..dd26e09 100644 --- a/src/main/java/org/gcube/datacatalogue/ckanutillibrary/CKanUtilsImpl.java +++ b/src/main/java/org/gcube/datacatalogue/ckanutillibrary/CKanUtilsImpl.java @@ -784,43 +784,48 @@ public class CKanUtilsImpl implements CKanUtils{ // else organizationNameToCheck = organizationName.toLowerCase(); - boolean alreadyPresent = isRoleAlreadySet(ckanUsername, organizationNameToCheck, correspondentRoleToCheck); + try{ + boolean alreadyPresent = isRoleAlreadySet(ckanUsername, organizationNameToCheck, correspondentRoleToCheck); - if(alreadyPresent) - return true; // just return - else{ + if(alreadyPresent) + return true; // just return + else{ - // we need to use the apis to make it - String path = "/api/3/action/organization_member_create"; + // we need to use the apis to make it + String path = "/api/3/action/organization_member_create"; - // Request parameters to be replaced - String parameter = "{" - + "\"id\":\"ORGANIZATION_ID_NAME\"," - + "\"username\":\"USERNAME_ID_NAME\"," - + "\"role\":\"ROLE\"" - + "}"; + // Request parameters to be replaced + String parameter = "{" + + "\"id\":\"ORGANIZATION_ID_NAME\"," + + "\"username\":\"USERNAME_ID_NAME\"," + + "\"role\":\"ROLE\"" + + "}"; - // replace those values - parameter = parameter.replace("ORGANIZATION_ID_NAME", organizationNameToCheck); - parameter = parameter.replace("USERNAME_ID_NAME", ckanUsername); - parameter = parameter.replace("ROLE", correspondentRoleToCheck.toString().toLowerCase()); + // replace those values + parameter = parameter.replace("ORGANIZATION_ID_NAME", organizationNameToCheck); + parameter = parameter.replace("USERNAME_ID_NAME", ckanUsername); + parameter = parameter.replace("ROLE", correspondentRoleToCheck.toString().toLowerCase()); - logger.debug("API request for organization membership is going to be " + parameter); + logger.debug("API request for organization membership is going to be " + parameter); - try(CloseableHttpClient httpClient = HttpClientBuilder.create().build();) { - HttpPost request = new HttpPost(CKAN_CATALOGUE_URL + path); - request.addHeader("Authorization", CKAN_TOKEN_SYS); // sys token - StringEntity params = new StringEntity(parameter); - request.setEntity(params); - HttpResponse response = httpClient.execute(request); - logger.debug("Response code is " + response.getStatusLine().getStatusCode() + " and response message is " + response.getStatusLine().getReasonPhrase()); + try(CloseableHttpClient httpClient = HttpClientBuilder.create().build();) { + HttpPost request = new HttpPost(CKAN_CATALOGUE_URL + path); + request.addHeader("Authorization", CKAN_TOKEN_SYS); // sys token + StringEntity params = new StringEntity(parameter); + request.setEntity(params); + HttpResponse response = httpClient.execute(request); + logger.debug("Response code is " + response.getStatusLine().getStatusCode() + " and response message is " + response.getStatusLine().getReasonPhrase()); - return (response.getStatusLine().getStatusCode() == HttpStatus.SC_OK); + return (response.getStatusLine().getStatusCode() == HttpStatus.SC_OK); - }catch (Exception ex) { - logger.error("Error while trying to change the role for this user ", ex); + }catch (Exception ex) { + logger.error("Error while trying to change the role for this user ", ex); + } } + }catch (Exception ex) { + logger.error("Unable to check if this role was already set, please check your parameters! ", ex); } + return false; } @@ -833,20 +838,14 @@ public class CKanUtilsImpl implements CKanUtils{ */ protected boolean isRoleAlreadySet(String ckanUsername, String organizationName, - RolesIntoOrganization correspondentRoleToCheck) { + RolesIntoOrganization correspondentRoleToCheck) throws Exception{ - try{ + // get the users (if you try ckanOrganization.getUsers() it returns null.. maybe a bug TODO) + List users = client.getOrganization(organizationName).getUsers(); - // get the users (if you try ckanOrganization.getUsers() it returns null.. maybe a bug TODO) - List users = client.getOrganization(organizationName).getUsers(); - - for (CkanUser ckanUser : users) { - if(ckanUser.getName().equals(ckanUsername) && ckanUser.getCapacity().equals(correspondentRoleToCheck.toString().toLowerCase())) - return true; - } - - }catch(Exception e){ - logger.error("Unable to check if this role was already set", e); + for (CkanUser ckanUser : users) { + if(ckanUser.getName().equals(ckanUsername) && ckanUser.getCapacity().equals(correspondentRoleToCheck.toString().toLowerCase())) + return true; } return false; diff --git a/src/test/java/org/gcube/datacatalogue/ckanutillibrary/TestCKanLib.java b/src/test/java/org/gcube/datacatalogue/ckanutillibrary/TestCKanLib.java index 3315993..2df57c0 100644 --- a/src/test/java/org/gcube/datacatalogue/ckanutillibrary/TestCKanLib.java +++ b/src/test/java/org/gcube/datacatalogue/ckanutillibrary/TestCKanLib.java @@ -23,7 +23,7 @@ public class TestCKanLib { private static final org.slf4j.Logger logger = LoggerFactory.getLogger(TestCKanLib.class); private CkanUtilsFactory factory; - private String scope = "/gcube/devNext/NextNext"; + private String scope = "/gcube"; private String testUser = "costantino_perciante"; String subjectId = "aa_father4"; String objectId = "bb_son4"; @@ -178,12 +178,12 @@ public class TestCKanLib { ScopeProvider.instance.set("/gcube"); String url = "https://dev4.d4science.org/group/devvre/ckan"; - String scopeToUse = new ApplicationProfileScopePerUrlReader().getScopePerUrl(url); + String scopeToUse = ApplicationProfileScopePerUrlReader.getScopePerUrl(url); logger.debug("Retrieved scope is " + scopeToUse); ScopeProvider.instance.reset(); // the following sysout should print null String url2 = "https://dev4.d4science.org/group/devvre/ckan"; - String scopeToUse2 = new ApplicationProfileScopePerUrlReader().getScopePerUrl(url2); + String scopeToUse2 = ApplicationProfileScopePerUrlReader.getScopePerUrl(url2); logger.debug("Retrieved scope is " + scopeToUse2); } @@ -246,4 +246,13 @@ public class TestCKanLib { // } + + //@Test + public void testInvalidOrgRole() throws Exception{ + + CKanUtilsImpl instance = factory.getUtilsPerScope(scope); + boolean res = instance.checkRole("costantino_perciante", "gcube", RolesIntoOrganization.EDITOR); + logger.debug(""+res); + + } }