diff --git a/.settings/org.eclipse.wst.common.component b/.settings/org.eclipse.wst.common.component index 6cfd3bb..da1fa8a 100644 --- a/.settings/org.eclipse.wst.common.component +++ b/.settings/org.eclipse.wst.common.component @@ -4,9 +4,6 @@ - - uses - diff --git a/distro/changelog.xml b/distro/changelog.xml index 85f4b5d..55e595a 100644 --- a/distro/changelog.xml +++ b/distro/changelog.xml @@ -1,4 +1,8 @@ + + Item purge method enhanced + Minor fixes while checking user's permissions diff --git a/pom.xml b/pom.xml index 3bba055..b2b6099 100644 --- a/pom.xml +++ b/pom.xml @@ -11,7 +11,7 @@ org.gcube.data-catalogue catalogue-ws war - 1.1.0-SNAPSHOT + 1.1.1-SNAPSHOT catalogue-ws diff --git a/src/main/java/org/gcube/datacatalogue/catalogue/ws/Item.java b/src/main/java/org/gcube/datacatalogue/catalogue/ws/Item.java index 166fec0..a9ca3f5 100644 --- a/src/main/java/org/gcube/datacatalogue/catalogue/ws/Item.java +++ b/src/main/java/org/gcube/datacatalogue/catalogue/ws/Item.java @@ -22,12 +22,15 @@ import org.gcube.datacatalogue.catalogue.utils.Delegator; import org.gcube.datacatalogue.catalogue.utils.PackageCreatePostActions; import org.gcube.datacatalogue.catalogue.utils.Validator; import org.gcube.datacatalogue.ckanutillibrary.server.DataCatalogue; +import org.gcube.datacatalogue.ckanutillibrary.shared.RolesCkanGroupOrOrg; import org.json.simple.JSONArray; import org.json.simple.JSONObject; import org.json.simple.parser.JSONParser; import org.json.simple.parser.ParseException; import org.slf4j.LoggerFactory; +import eu.trentorise.opendata.jackan.model.CkanDataset; + @Path(Constants.ITEMS) /** * Items service endpoint. @@ -139,11 +142,64 @@ public class Item { @Produces(MediaType.APPLICATION_JSON) public String purge(String json, @Context UriInfo uriInfo){ - // see http://docs.ckan.org/en/latest/api/#ckan.logic.action.get.dataset_purge + // see http://docs.ckan.org/en/latest/api/#ckan.logic.action.delete.dataset_purge Caller caller = AuthorizationProvider.instance.get(); String context = ScopeProvider.instance.get(); - return Delegator.delegatePost(caller, context, Constants.ITEM_PURGE, json, uriInfo); + String username = caller.getClient().getId(); + + // we need to extend this method wrt ckan: admins can purge the organization data, while editors just their own + DataCatalogue utils = CatalogueUtils.getCatalogue(); + // if sysadmin, just invoke ckan + if(utils.isSysAdmin(username)){ + logger.debug("User " + caller.getClient().getId() + " seems a sysadmin"); + return Delegator.delegatePost(caller, context, Constants.ITEM_PURGE, json, uriInfo); + } + else{ + try { + + String datasetId = null; + String ownerId = null; + String organization = null; + + JSONParser parser = new JSONParser(); + JSONObject obj = (JSONObject)parser.parse(json); + + datasetId = (String)obj.get("id"); + if(datasetId == null || datasetId.isEmpty()) + throw new Exception("'id' field is missing!"); + + String userApiKey = utils.getApiKeyFromUsername(username); + CkanDataset item = utils.getDataset(datasetId, userApiKey); + ownerId = item.getCreatorUserId(); + organization = item.getOrganization().getName(); + + // check user role here + RolesCkanGroupOrOrg roleInOrganization = RolesCkanGroupOrOrg.convertFromCapacity(utils.getRoleOfUserInOrganization(username, organization, userApiKey)); + + boolean purged = false; + if(roleInOrganization.equals(RolesCkanGroupOrOrg.MEMBER)){ + throw new Exception("You have not enough priviliges to delete item with id " + datasetId); + }else if(roleInOrganization.equals(RolesCkanGroupOrOrg.ADMIN)){ + purged = utils.deleteProduct(datasetId, userApiKey, true); + }else{ + // we have an editor here; just check she owns the dataset + String userIdCkan = utils.getUserFromApiKey(userApiKey).getId(); + if(ownerId.equals(userIdCkan)) + purged = utils.deleteProduct(datasetId, userApiKey, true); + else + throw new Exception("Editors can only remove their own items!"); + } + return CatalogueUtils.createJSONObjectMin(purged, null).toJSONString(); + + } catch (Exception e) { + logger.error("Something went wrong... ", e); + if(e instanceof ParseException) + return CatalogueUtils.createJSONOnFailure("Failed to parse incoming json!"); + else + return CatalogueUtils.createJSONOnFailure(e.toString()); + } + } } // TODO PROFILE VALIDATION MUST BE PERFORMED HERE AS WELL diff --git a/src/main/webapp/WEB-INF/gcube-app.xml b/src/main/webapp/WEB-INF/gcube-app.xml index bfd1b1a..dd37e64 100644 --- a/src/main/webapp/WEB-INF/gcube-app.xml +++ b/src/main/webapp/WEB-INF/gcube-app.xml @@ -1,7 +1,7 @@ Catalogue-WS Data-Catalogue - 1.1.0-SNAPSHOT + 1.1.1-SNAPSHOT Catalogue Service /rest/