Improving authorship code

This commit is contained in:
Luca Frosini 2022-10-03 13:52:51 +02:00
parent 150f533fc7
commit 1060843069
1 changed files with 21 additions and 8 deletions

View File

@ -230,14 +230,20 @@ public class CKANPackage extends CKAN implements Moderated {
* *
*/ */
public ObjectNode checkAuthor(ObjectNode objectNode, String authorName, String authorEmail) { public ObjectNode checkAuthor(ObjectNode objectNode, String authorName, String authorEmail) {
if(!updateOperation) { // this prevent to change the original author in case of update if(!updateOperation) {
objectNode.put(AUTHOR_KEY, authorName); objectNode.put(AUTHOR_KEY, authorName);
objectNode.put(AUTHOR_EMAIL_KEY, authorEmail); objectNode.put(AUTHOR_EMAIL_KEY, authorEmail);
}else { }else { // this prevent to change the original author in case of update
Role role = ckanUser.getRole(); Role role = ckanUser.getRole();
if(role.ordinal() < Role.ADMIN.ordinal()) { if(role.ordinal() < Role.ADMIN.ordinal()) {
objectNode.remove(AUTHOR_KEY); if(result.get(AUTHOR_KEY).asText().compareTo(authorName)!=0) {
objectNode.remove(AUTHOR_EMAIL_KEY); throw new BadRequestException("Only Catalogue-Admins or above can change the authorship (i.e. " + AUTHOR_KEY + " field) of an item.");
}
if(result.get(AUTHOR_EMAIL_KEY).asText().compareTo(authorEmail)!=0) {
throw new BadRequestException("Only Catalogue-Admins or above can change the authorship (i.e. " + AUTHOR_EMAIL_KEY + " field) of an item.");
}
} }
} }
return objectNode; return objectNode;
@ -263,7 +269,14 @@ public class CKANPackage extends CKAN implements Moderated {
// We need to enforce the itemID to properly manage resource persistence // We need to enforce the itemID to properly manage resource persistence
if(objectNode.has(ID_KEY)) { if(objectNode.has(ID_KEY)) {
itemID = objectNode.get(ID_KEY).asText(); String id = objectNode.get(ID_KEY).asText();
if(itemID==null) {
itemID = id;
}else {
if(id.compareTo(itemID)!=0) {
throw new BadRequestException("Item ID comntained in the request body does not match with the id of the item in Ckan.");
}
}
} }
// To include private item in search result (e.g. listing) a private package must be searchable // To include private item in search result (e.g. listing) a private package must be searchable
@ -335,7 +348,7 @@ public class CKANPackage extends CKAN implements Moderated {
return objectNode; return objectNode;
} }
protected JsonNode validateJson(String json) { protected JsonNode validateJso(String json) {
try { try {
// check base information (and set them if needed) // check base information (and set them if needed)
ObjectNode objectNode = checkBaseInformation(json); ObjectNode objectNode = checkBaseInformation(json);
@ -859,8 +872,6 @@ public class CKANPackage extends CKAN implements Moderated {
try { try {
this.updateOperation = true; this.updateOperation = true;
JsonNode jsonNode = validateJson(json);
/* /*
* Going to read the item from CKAN just to check the item status. * Going to read the item from CKAN just to check the item status.
* I need to reset the result first because the current contains * I need to reset the result first because the current contains
@ -869,6 +880,8 @@ public class CKANPackage extends CKAN implements Moderated {
this.result = null; this.result = null;
readItem(); readItem();
JsonNode jsonNode = validateJson(json);
jsonNode = checkModerationUpdate(jsonNode); jsonNode = checkModerationUpdate(jsonNode);
Map<String,CKANResource> originalResources = new HashMap<>(); Map<String,CKANResource> originalResources = new HashMap<>();