forked from 4Science/DSpace
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request DSpace#9768 from 4Science/DURACOM-222
Expose the item submitter over the REST endpoint `/api/core/items/<:uuid>/submitter`
- Loading branch information
Showing
4 changed files
with
191 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
61 changes: 61 additions & 0 deletions
61
...rver-webapp/src/main/java/org/dspace/app/rest/repository/ItemSubmitterLinkRepository.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
/** | ||
* The contents of this file are subject to the license and copyright | ||
* detailed in the LICENSE and NOTICE files at the root of the source | ||
* tree and available online at | ||
* | ||
* http://www.dspace.org/license/ | ||
*/ | ||
package org.dspace.app.rest.repository; | ||
|
||
import java.sql.SQLException; | ||
import java.util.UUID; | ||
import javax.annotation.Nullable; | ||
|
||
import jakarta.servlet.http.HttpServletRequest; | ||
import org.dspace.app.rest.model.EPersonRest; | ||
import org.dspace.app.rest.model.ItemRest; | ||
import org.dspace.app.rest.projection.Projection; | ||
import org.dspace.content.Item; | ||
import org.dspace.content.service.ItemService; | ||
import org.dspace.core.Context; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.data.domain.Pageable; | ||
import org.springframework.data.rest.webmvc.ResourceNotFoundException; | ||
import org.springframework.security.access.prepost.PreAuthorize; | ||
import org.springframework.stereotype.Component; | ||
|
||
/** | ||
* Link repository for "submitter" subresource of an item. | ||
*/ | ||
@Component(ItemRest.CATEGORY + "." + ItemRest.PLURAL_NAME + "." + ItemRest.SUBMITTER) | ||
public class ItemSubmitterLinkRepository extends AbstractDSpaceRestRepository | ||
implements LinkRestRepository { | ||
|
||
@Autowired | ||
ItemService itemService; | ||
|
||
/** | ||
* Retrieve the submitter for an item. | ||
* | ||
* @param request - The current request | ||
* @param id - The item ID for which to retrieve the submitter | ||
* @param optionalPageable - optional pageable object | ||
* @param projection - the current projection | ||
* @return the submitter for the item | ||
*/ | ||
@PreAuthorize("hasPermission(#id, 'ITEM', 'READ')") | ||
public EPersonRest getItemSubmitter(@Nullable HttpServletRequest request, UUID id, | ||
@Nullable Pageable optionalPageable, Projection projection) { | ||
try { | ||
Context context = obtainContext(); | ||
Item item = itemService.find(context, id); | ||
if (item == null) { | ||
throw new ResourceNotFoundException("No such item: " + id); | ||
} | ||
|
||
return converter.toRest(item.getSubmitter(), projection); | ||
} catch (SQLException e) { | ||
throw new RuntimeException(e); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4696,4 +4696,124 @@ public void findAccessStatusForItemTest() throws Exception { | |
.andExpect(jsonPath("$.status", notNullValue())); | ||
} | ||
|
||
@Test | ||
public void findSubmitterByAdminTest() throws Exception { | ||
context.turnOffAuthorisationSystem(); | ||
|
||
//** GIVEN ** | ||
//1. A community-collection structure with one parent community with sub-community and two collections. | ||
parentCommunity = CommunityBuilder.createCommunity(context) | ||
.withName("Parent Community") | ||
.build(); | ||
Community child1 = CommunityBuilder.createSubCommunity(context, parentCommunity) | ||
.withName("Sub Community") | ||
.build(); | ||
Collection col1 = CollectionBuilder.createCollection(context, child1).withName("Collection 1").build(); | ||
|
||
EPerson submitter = EPersonBuilder.createEPerson(context) | ||
.withEmail("[email protected]") | ||
.withPassword(password) | ||
.withCanLogin(true) | ||
.build(); | ||
|
||
context.setCurrentUser(submitter); | ||
|
||
//2. Three public items that are readable by Anonymous with different subjects | ||
Item publicItem = ItemBuilder.createItem(context, col1) | ||
.withTitle("Public item 1") | ||
.withIssueDate("2017-10-17") | ||
.withAuthor("Smith, Donald") | ||
.withSubject("ExtraEntry") | ||
.build(); | ||
|
||
context.restoreAuthSystemState(); | ||
|
||
String token = getAuthToken(admin.getEmail(), password); | ||
|
||
getClient(token).perform(get("/api/core/items/" + publicItem.getID()) | ||
.param("projection", "full")) | ||
.andExpect(status().isOk()) | ||
.andExpect(jsonPath("$", ItemMatcher.matchFullEmbeds())); | ||
|
||
getClient(token).perform(get("/api/core/items/" + publicItem.getID() + "/submitter")) | ||
.andExpect(status().isOk()) | ||
.andExpect(jsonPath("$.id", is(submitter.getID().toString()))) | ||
.andExpect(jsonPath("$.email", is(submitter.getEmail()))); | ||
} | ||
|
||
@Test | ||
public void findSubmitterWithoutReadAccessTest() throws Exception { | ||
context.turnOffAuthorisationSystem(); | ||
|
||
parentCommunity = CommunityBuilder.createCommunity(context) | ||
.withName("Parent Community") | ||
.build(); | ||
|
||
Collection col1 = CollectionBuilder.createCollection(context, parentCommunity).withName("Collection 1").build(); | ||
|
||
EPerson submitter = EPersonBuilder.createEPerson(context) | ||
.withEmail("[email protected]") | ||
.withPassword(password) | ||
.withCanLogin(true) | ||
.build(); | ||
|
||
context.setCurrentUser(submitter); | ||
|
||
Item publicItem = ItemBuilder.createItem(context, col1) | ||
.withTitle("Public item 1") | ||
.withIssueDate("2017-10-17") | ||
.withAuthor("Smith, Donald") | ||
.withSubject("ExtraEntry") | ||
.build(); | ||
|
||
context.restoreAuthSystemState(); | ||
|
||
String token = getAuthToken(eperson.getEmail(), password); | ||
|
||
getClient(token).perform(get("/api/core/items/" + publicItem.getID()) | ||
.param("projection", "full")) | ||
.andExpect(status().isOk()) | ||
.andExpect(jsonPath("$", ItemMatcher.matchFullEmbeds())); | ||
|
||
// find submitter by user has no read access | ||
getClient(token).perform(get("/api/core/items/" + publicItem.getID() + "/submitter")) | ||
.andExpect(status().isNoContent()); | ||
} | ||
|
||
@Test | ||
public void findSubmitterByAnonymousTest() throws Exception { | ||
context.turnOffAuthorisationSystem(); | ||
|
||
parentCommunity = CommunityBuilder.createCommunity(context) | ||
.withName("Parent Community") | ||
.build(); | ||
|
||
Collection col1 = CollectionBuilder.createCollection(context, parentCommunity).withName("Collection 1").build(); | ||
|
||
EPerson submitter = EPersonBuilder.createEPerson(context) | ||
.withEmail("[email protected]") | ||
.withPassword(password) | ||
.withCanLogin(true) | ||
.build(); | ||
|
||
context.setCurrentUser(submitter); | ||
|
||
Item publicItem = ItemBuilder.createItem(context, col1) | ||
.withTitle("Public item 1") | ||
.withIssueDate("2017-10-17") | ||
.withAuthor("Smith, Donald") | ||
.withSubject("ExtraEntry") | ||
.build(); | ||
|
||
context.restoreAuthSystemState(); | ||
|
||
getClient().perform(get("/api/core/items/" + publicItem.getID()) | ||
.param("projection", "full")) | ||
.andExpect(status().isOk()) | ||
.andExpect(jsonPath("$", ItemMatcher.matchFullEmbeds())); | ||
|
||
getClient().perform(get("/api/core/items/" + publicItem.getID() + "/submitter")) | ||
.andExpect(status().isNoContent()); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters