diff --git a/dspace-api/src/main/java/org/dspace/content/clarin/ClarinLicenseResourceUserAllowanceServiceImpl.java b/dspace-api/src/main/java/org/dspace/content/clarin/ClarinLicenseResourceUserAllowanceServiceImpl.java index 4af119b0c8cc..2ecfbf5da979 100644 --- a/dspace-api/src/main/java/org/dspace/content/clarin/ClarinLicenseResourceUserAllowanceServiceImpl.java +++ b/dspace-api/src/main/java/org/dspace/content/clarin/ClarinLicenseResourceUserAllowanceServiceImpl.java @@ -152,18 +152,12 @@ private void authorizeClruaAction(Context context, List findAll(Context context, Pag } @SearchRestMethod(name = "byBitstreamAndUser") + @PreAuthorize("hasAuthority('AUTHENTICATED')") public Page findByValue( @Parameter(value = "bitstreamUUID", required = true) UUID bitstreamUUID, @Parameter(value = "userUUID", required = true) UUID userUUID, - Pageable pageable) throws SQLException, AuthorizeException { + Pageable pageable) throws SQLException { Context context = obtainContext(); - List clarinLicenseResourceUserAllowance = - clarinLicenseResourceUserAllowanceService.findByEPersonIdAndBitstreamId(context, userUUID, - bitstreamUUID); + List clarinLicenseResourceUserAllowance; + try { + clarinLicenseResourceUserAllowance = clarinLicenseResourceUserAllowanceService + .findByEPersonIdAndBitstreamId(context, userUUID, bitstreamUUID); + } catch (AuthorizeException e) { + throw new RESTAuthorizationException(e); + } if (CollectionUtils.isEmpty(clarinLicenseResourceUserAllowance)) { return null; diff --git a/dspace-server-webapp/src/main/java/org/dspace/app/rest/repository/ClarinUserMetadataRestRepository.java b/dspace-server-webapp/src/main/java/org/dspace/app/rest/repository/ClarinUserMetadataRestRepository.java index 4270bcda8843..ebdf303d332c 100644 --- a/dspace-server-webapp/src/main/java/org/dspace/app/rest/repository/ClarinUserMetadataRestRepository.java +++ b/dspace-server-webapp/src/main/java/org/dspace/app/rest/repository/ClarinUserMetadataRestRepository.java @@ -64,14 +64,20 @@ public Page findAll(Context context, Pageable pageable) } @SearchRestMethod(name = "byUserRegistrationAndBitstream") + @PreAuthorize("hasAuthority('AUTHENTICATED')") public Page findByUserRegistrationAndBitstream( @Parameter(value = "userRegUUID", required = true) Integer userRegId, @Parameter(value = "bitstreamUUID", required = true) UUID bitstreamUUID, - Pageable pageable) throws SQLException, AuthorizeException { + Pageable pageable) throws SQLException { Context context = obtainContext(); - List clarinUserMetadataList = - clarinUserMetadataService.findByUserRegistrationAndBitstream(context, userRegId, bitstreamUUID, true); + List clarinUserMetadataList; + try { + clarinUserMetadataList = clarinUserMetadataService + .findByUserRegistrationAndBitstream(context, userRegId, bitstreamUUID, true); + } catch (AuthorizeException e) { + throw new RESTAuthorizationException(e); + } return converter.toRestPage(clarinUserMetadataList, pageable, utils.obtainProjection()); } diff --git a/dspace-server-webapp/src/main/java/org/dspace/app/rest/repository/ClarinUserRegistrationRestRepository.java b/dspace-server-webapp/src/main/java/org/dspace/app/rest/repository/ClarinUserRegistrationRestRepository.java index 0d616855292d..28088e5d6efc 100644 --- a/dspace-server-webapp/src/main/java/org/dspace/app/rest/repository/ClarinUserRegistrationRestRepository.java +++ b/dspace-server-webapp/src/main/java/org/dspace/app/rest/repository/ClarinUserRegistrationRestRepository.java @@ -80,14 +80,18 @@ protected ClarinUserRegistrationRest createAndReturn(Context context) throws SQL } @SearchRestMethod(name = "byEPerson") + @PreAuthorize("hasAuthority('AUTHENTICATED')") public Page findByEPerson(@Parameter(value = "userUUID", required = true) UUID userUUID, - Pageable pageable) - throws SQLException, AuthorizeException { + Pageable pageable) throws SQLException { Context context = obtainContext(); - List clarinUserRegistrationList = - clarinUserRegistrationService.findByEPersonUUID(context, userUUID); + List clarinUserRegistrationList; + try { + clarinUserRegistrationList = clarinUserRegistrationService.findByEPersonUUID(context, userUUID); + } catch (AuthorizeException e) { + throw new RESTAuthorizationException(e); + } if (CollectionUtils.isEmpty(clarinUserRegistrationList)) { return null; } diff --git a/dspace-server-webapp/src/test/java/org/dspace/app/rest/ClarinLicenseResourceUserAllowanceRestRepositoryIT.java b/dspace-server-webapp/src/test/java/org/dspace/app/rest/ClarinLicenseResourceUserAllowanceRestRepositoryIT.java new file mode 100644 index 000000000000..7e557973aab0 --- /dev/null +++ b/dspace-server-webapp/src/test/java/org/dspace/app/rest/ClarinLicenseResourceUserAllowanceRestRepositoryIT.java @@ -0,0 +1,184 @@ +/** + * 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; + +import static org.hamcrest.Matchers.is; +import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get; +import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath; +import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status; + +import java.nio.charset.StandardCharsets; +import java.util.ArrayList; +import java.util.List; + +import org.apache.commons.io.IOUtils; +import org.dspace.app.rest.test.AbstractControllerIntegrationTest; +import org.dspace.builder.BitstreamBuilder; +import org.dspace.builder.ClarinLicenseBuilder; +import org.dspace.builder.ClarinLicenseResourceMappingBuilder; +import org.dspace.builder.ClarinLicenseResourceUserAllowanceBuilder; +import org.dspace.builder.ClarinUserRegistrationBuilder; +import org.dspace.builder.CollectionBuilder; +import org.dspace.builder.CommunityBuilder; +import org.dspace.builder.EPersonBuilder; +import org.dspace.builder.ItemBuilder; +import org.dspace.content.Bitstream; +import org.dspace.content.Collection; +import org.dspace.content.Community; +import org.dspace.content.Item; +import org.dspace.content.clarin.ClarinLicenseResourceMapping; +import org.dspace.content.clarin.ClarinLicenseResourceUserAllowance; +import org.dspace.content.clarin.ClarinUserRegistration; +import org.dspace.eperson.EPerson; +import org.junit.After; +import org.junit.Before; +import org.junit.Test; + +public class ClarinLicenseResourceUserAllowanceRestRepositoryIT extends AbstractControllerIntegrationTest { + + private Bitstream bitstream; + private ClarinLicenseResourceMapping mapping; + private ClarinUserRegistration clarinUserRegistration; + private EPerson otherEPerson; + private final List allowanceIds = new ArrayList<>(); + + @Before + public void setup() throws Exception { + context.turnOffAuthorisationSystem(); + Community community = CommunityBuilder.createCommunity(context) + .withName("Community") + .build(); + Collection collection = CollectionBuilder.createCollection(context, community) + .withName("Collection") + .build(); + Item item = ItemBuilder.createItem(context, collection) + .withTitle("Item") + .build(); + bitstream = BitstreamBuilder + .createBitstream(context, item, IOUtils.toInputStream("content", StandardCharsets.UTF_8)) + .withName("file.txt") + .build(); + mapping = ClarinLicenseResourceMappingBuilder.createClarinLicenseResourceMapping(context).build(); + mapping.setLicense(ClarinLicenseBuilder.createClarinLicense(context).build()); + mapping.setBitstream(bitstream); + clarinUserRegistration = ClarinUserRegistrationBuilder.createClarinUserRegistration(context) + .withEPersonID(eperson.getID()) + .build(); + otherEPerson = EPersonBuilder.createEPerson(context) + .withEmail("otherEperson@mail.com") + .withPassword(password) + .build(); + context.restoreAuthSystemState(); + } + + @After + public void deleteAllowances() throws Exception { + for (Integer id : allowanceIds) { + ClarinLicenseResourceUserAllowanceBuilder.deleteClarinLicenseResourceUserAllowance(id); + } + } + + @Test + public void findOneTest() throws Exception { + ClarinLicenseResourceUserAllowance allowance = createAllowance(clarinUserRegistration); + String url = "/api/core/clarinlruallowances/" + allowance.getID(); + + getClient().perform(get(url)) + .andExpect(status().isUnauthorized()); + + String userToken = getAuthToken(eperson.getEmail(), password); + getClient(userToken).perform(get(url)) + .andExpect(status().isOk()); + + String otherUserToken = getAuthToken(otherEPerson.getEmail(), password); + getClient(otherUserToken).perform(get(url)) + .andExpect(status().isForbidden()); + + String adminToken = getAuthToken(admin.getEmail(), password); + getClient(adminToken).perform(get(url)) + .andExpect(status().isOk()); + } + + @Test + public void findOneWithoutUserRegistrationTest() throws Exception { + // Allowances without a user registration come from an anonymous download, only admin can read them + ClarinLicenseResourceUserAllowance allowance = createAllowance(null); + String url = "/api/core/clarinlruallowances/" + allowance.getID(); + + getClient().perform(get(url)) + .andExpect(status().isUnauthorized()); + + String userToken = getAuthToken(eperson.getEmail(), password); + getClient(userToken).perform(get(url)) + .andExpect(status().isForbidden()); + + String adminToken = getAuthToken(admin.getEmail(), password); + getClient(adminToken).perform(get(url)) + .andExpect(status().isOk()); + } + + @Test + public void linksTest() throws Exception { + ClarinLicenseResourceUserAllowance allowance = createAllowance(clarinUserRegistration); + + String userToken = getAuthToken(eperson.getEmail(), password); + String otherUserToken = getAuthToken(otherEPerson.getEmail(), password); + for (String link : List.of("userRegistration", "resourceMapping")) { + String url = "/api/core/clarinlruallowances/" + allowance.getID() + "/" + link; + + getClient().perform(get(url)) + .andExpect(status().isUnauthorized()); + + getClient(otherUserToken).perform(get(url)) + .andExpect(status().isForbidden()); + + getClient(userToken).perform(get(url)) + .andExpect(status().isOk()); + } + } + + @Test + public void findByBitstreamAndUserTest() throws Exception { + createAllowance(clarinUserRegistration); + String search = "/api/core/clarinlruallowances/search/byBitstreamAndUser?bitstreamUUID=" + + bitstream.getID() + "&userUUID=" + eperson.getID(); + + getClient().perform(get(search)) + .andExpect(status().isUnauthorized()); + // a search that matches nothing is refused too + getClient().perform(get("/api/core/clarinlruallowances/search/byBitstreamAndUser?bitstreamUUID=" + + bitstream.getID() + "&userUUID=" + otherEPerson.getID())) + .andExpect(status().isUnauthorized()); + + String userToken = getAuthToken(eperson.getEmail(), password); + getClient(userToken).perform(get(search)) + .andExpect(status().isOk()) + .andExpect(jsonPath("$.page.totalElements", is(1))); + + String otherUserToken = getAuthToken(otherEPerson.getEmail(), password); + getClient(otherUserToken).perform(get(search)) + .andExpect(status().isForbidden()); + + String adminToken = getAuthToken(admin.getEmail(), password); + getClient(adminToken).perform(get(search)) + .andExpect(status().isOk()); + } + + private ClarinLicenseResourceUserAllowance createAllowance(ClarinUserRegistration owner) throws Exception { + context.turnOffAuthorisationSystem(); + ClarinLicenseResourceUserAllowance allowance = ClarinLicenseResourceUserAllowanceBuilder + .createClarinLicenseResourceUserAllowance(context) + .withUser(owner) + .withMapping(mapping) + .withToken("downloadToken") + .build(); + context.restoreAuthSystemState(); + allowanceIds.add(allowance.getID()); + return allowance; + } +} diff --git a/dspace-server-webapp/src/test/java/org/dspace/app/rest/ClarinUserMetadataRestRepositoryIT.java b/dspace-server-webapp/src/test/java/org/dspace/app/rest/ClarinUserMetadataRestRepositoryIT.java index 357a099d2b9c..462472bbf7b5 100644 --- a/dspace-server-webapp/src/test/java/org/dspace/app/rest/ClarinUserMetadataRestRepositoryIT.java +++ b/dspace-server-webapp/src/test/java/org/dspace/app/rest/ClarinUserMetadataRestRepositoryIT.java @@ -7,24 +7,50 @@ */ package org.dspace.app.rest; +import static org.hamcrest.Matchers.is; import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get; +import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath; import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status; +import java.nio.charset.StandardCharsets; +import java.util.UUID; + +import org.apache.commons.io.IOUtils; import org.dspace.app.rest.test.AbstractControllerIntegrationTest; +import org.dspace.builder.BitstreamBuilder; import org.dspace.builder.ClarinLicenseBuilder; +import org.dspace.builder.ClarinLicenseResourceMappingBuilder; +import org.dspace.builder.ClarinLicenseResourceUserAllowanceBuilder; import org.dspace.builder.ClarinUserMetadataBuilder; import org.dspace.builder.ClarinUserRegistrationBuilder; import org.dspace.builder.CollectionBuilder; import org.dspace.builder.CommunityBuilder; import org.dspace.builder.EPersonBuilder; +import org.dspace.builder.ItemBuilder; +import org.dspace.content.Bitstream; +import org.dspace.content.Collection; import org.dspace.content.Community; +import org.dspace.content.Item; +import org.dspace.content.clarin.ClarinLicenseResourceMapping; +import org.dspace.content.clarin.ClarinLicenseResourceUserAllowance; import org.dspace.content.clarin.ClarinUserMetadata; import org.dspace.content.clarin.ClarinUserRegistration; import org.dspace.eperson.EPerson; +import org.junit.After; import org.junit.Test; public class ClarinUserMetadataRestRepositoryIT extends AbstractControllerIntegrationTest { + private ClarinLicenseResourceUserAllowance allowance; + private ClarinLicenseResourceMapping mapping; + + @After + public void deleteAllowance() throws Exception { + if (allowance != null) { + ClarinLicenseResourceUserAllowanceBuilder.deleteClarinLicenseResourceUserAllowance(allowance.getID()); + } + } + @Test public void findAllTest() throws Exception { context.turnOffAuthorisationSystem(); @@ -121,4 +147,90 @@ public void findOneTest() throws Exception { otherClarinUserMetadata.getID())) .andExpect(status().isForbidden()); } + + @Test + public void findOneWithoutUserRegistrationTest() throws Exception { + context.turnOffAuthorisationSystem(); + ClarinUserMetadata clarinUserMetadata = ClarinUserMetadataBuilder.createClarinUserMetadata(context) + .build(); + context.restoreAuthSystemState(); + + // User metadata without a user registration come from an anonymous download, only admin can read them + getClient().perform(get("/api/core/clarinusermetadatas/" + clarinUserMetadata.getID())) + .andExpect(status().isUnauthorized()); + + String userToken = getAuthToken(eperson.getEmail(), password); + getClient(userToken).perform(get("/api/core/clarinusermetadatas/" + clarinUserMetadata.getID())) + .andExpect(status().isForbidden()); + + String adminToken = getAuthToken(admin.getEmail(), password); + getClient(adminToken).perform(get("/api/core/clarinusermetadatas/" + clarinUserMetadata.getID())) + .andExpect(status().isOk()); + } + + @Test + public void findByUserRegistrationAndBitstreamTest() throws Exception { + context.turnOffAuthorisationSystem(); + Bitstream bitstream = createBitstreamWithLicense(); + ClarinUserRegistration clarinUserRegistration = ClarinUserRegistrationBuilder + .createClarinUserRegistration(context) + .withEPersonID(eperson.getID()) + .build(); + allowance = ClarinLicenseResourceUserAllowanceBuilder.createClarinLicenseResourceUserAllowance(context) + .withUser(clarinUserRegistration) + .withMapping(mapping) + .build(); + ClarinUserMetadata clarinUserMetadata = ClarinUserMetadataBuilder.createClarinUserMetadata(context) + .withUserRegistration(clarinUserRegistration) + .build(); + clarinUserMetadata.setTransaction(allowance); + EPerson otherEPerson = EPersonBuilder.createEPerson(context) + .withEmail("otherEperson@mail.com") + .withPassword(password) + .build(); + context.restoreAuthSystemState(); + + String search = "/api/core/clarinusermetadatas/search/byUserRegistrationAndBitstream?userRegUUID=" + + clarinUserRegistration.getID() + "&bitstreamUUID=" + bitstream.getID(); + + getClient().perform(get(search)) + .andExpect(status().isUnauthorized()); + // a search that matches nothing is refused too + getClient().perform(get("/api/core/clarinusermetadatas/search/byUserRegistrationAndBitstream?userRegUUID=" + + clarinUserRegistration.getID() + "&bitstreamUUID=" + UUID.randomUUID())) + .andExpect(status().isUnauthorized()); + + String userToken = getAuthToken(eperson.getEmail(), password); + getClient(userToken).perform(get(search)) + .andExpect(status().isOk()) + .andExpect(jsonPath("$.page.totalElements", is(1))); + + String otherUserToken = getAuthToken(otherEPerson.getEmail(), password); + getClient(otherUserToken).perform(get(search)) + .andExpect(status().isForbidden()); + + String adminToken = getAuthToken(admin.getEmail(), password); + getClient(adminToken).perform(get(search)) + .andExpect(status().isOk()); + } + + private Bitstream createBitstreamWithLicense() throws Exception { + Community community = CommunityBuilder.createCommunity(context) + .withName("Community") + .build(); + Collection collection = CollectionBuilder.createCollection(context, community) + .withName("Collection") + .build(); + Item item = ItemBuilder.createItem(context, collection) + .withTitle("Item") + .build(); + Bitstream bitstream = BitstreamBuilder + .createBitstream(context, item, IOUtils.toInputStream("content", StandardCharsets.UTF_8)) + .withName("file.txt") + .build(); + mapping = ClarinLicenseResourceMappingBuilder.createClarinLicenseResourceMapping(context).build(); + mapping.setLicense(ClarinLicenseBuilder.createClarinLicense(context).build()); + mapping.setBitstream(bitstream); + return bitstream; + } } diff --git a/dspace-server-webapp/src/test/java/org/dspace/app/rest/ClarinUserRegistrationRestRepositoryIT.java b/dspace-server-webapp/src/test/java/org/dspace/app/rest/ClarinUserRegistrationRestRepositoryIT.java index ab4bbaeed0a1..b02cba17e079 100644 --- a/dspace-server-webapp/src/test/java/org/dspace/app/rest/ClarinUserRegistrationRestRepositoryIT.java +++ b/dspace-server-webapp/src/test/java/org/dspace/app/rest/ClarinUserRegistrationRestRepositoryIT.java @@ -7,9 +7,13 @@ */ package org.dspace.app.rest; +import static org.hamcrest.Matchers.is; import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get; +import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath; import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status; +import java.util.List; + import org.dspace.app.rest.test.AbstractControllerIntegrationTest; import org.dspace.builder.ClarinLicenseBuilder; import org.dspace.builder.ClarinUserMetadataBuilder; @@ -114,4 +118,68 @@ public void findOneTest() throws Exception { otherClarinUserRegistration.getID())) .andExpect(status().isForbidden()); } + + @Test + public void findByEPersonTest() throws Exception { + context.turnOffAuthorisationSystem(); + ClarinUserRegistrationBuilder.createClarinUserRegistration(context) + .withEPersonID(eperson.getID()) + .build(); + EPerson otherEPerson = EPersonBuilder.createEPerson(context) + .withEmail("otherEperson@mail.com") + .withPassword(password) + .build(); + context.restoreAuthSystemState(); + + String search = "/api/core/clarinuserregistrations/search/byEPerson?userUUID=" + eperson.getID(); + + getClient().perform(get(search)) + .andExpect(status().isUnauthorized()); + // a search that matches nothing is refused too + getClient().perform(get("/api/core/clarinuserregistrations/search/byEPerson?userUUID=" + + otherEPerson.getID())) + .andExpect(status().isUnauthorized()); + + String userToken = getAuthToken(eperson.getEmail(), password); + getClient(userToken).perform(get(search)) + .andExpect(status().isOk()) + .andExpect(jsonPath("$.page.totalElements", is(1))); + + String otherUserToken = getAuthToken(otherEPerson.getEmail(), password); + getClient(otherUserToken).perform(get(search)) + .andExpect(status().isForbidden()); + + String adminToken = getAuthToken(admin.getEmail(), password); + getClient(adminToken).perform(get(search)) + .andExpect(status().isOk()); + } + + @Test + public void linksTest() throws Exception { + context.turnOffAuthorisationSystem(); + ClarinUserRegistration clarinUserRegistration = ClarinUserRegistrationBuilder + .createClarinUserRegistration(context) + .withEPersonID(eperson.getID()) + .build(); + EPerson otherEPerson = EPersonBuilder.createEPerson(context) + .withEmail("otherEperson@mail.com") + .withPassword(password) + .build(); + context.restoreAuthSystemState(); + + String userToken = getAuthToken(eperson.getEmail(), password); + String otherUserToken = getAuthToken(otherEPerson.getEmail(), password); + for (String link : List.of("userMetadata", "clarinLicenses")) { + String url = "/api/core/clarinuserregistrations/" + clarinUserRegistration.getID() + "/" + link; + + getClient().perform(get(url)) + .andExpect(status().isUnauthorized()); + + getClient(otherUserToken).perform(get(url)) + .andExpect(status().isForbidden()); + + getClient(userToken).perform(get(url)) + .andExpect(status().isOk()); + } + } }