Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -152,18 +152,12 @@ private void authorizeClruaAction(Context context, List<ClarinLicenseResourceUse
if (CollectionUtils.isEmpty(clruaList)) {
return;
}
// Check if the user is the same as from the userRegistration
// Do not allow to get the userRegistration of another user
// Only the owner may read it. Records without a user registration come from anonymous
// downloads, so only an admin may read them.
EPerson currentUser = context.getCurrentUser();
ClarinLicenseResourceUserAllowance clrua = clruaList.get(0);

// Check if the userRegistration is not null
if (Objects.isNull(clrua.getUserRegistration())) {
return;
}

UUID userRegistrationEpersonUUID = clrua.getUserRegistration().getPersonID();
if (Objects.nonNull(currentUser) && currentUser.getID().equals(userRegistrationEpersonUUID)) {
ClarinUserRegistration userRegistration = clruaList.get(0).getUserRegistration();
if (Objects.nonNull(currentUser) && Objects.nonNull(userRegistration)
&& currentUser.getID().equals(userRegistration.getPersonID())) {
return;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -154,18 +154,12 @@ private void authorizeClarinUserMetadataAction(Context context, List<ClarinUserM
return;
}

// Check if the user is the same as from the userRegistration
// Do not allow to get the userRegistration of another user
// Only the owner may read it. Records without a user registration come from anonymous
// downloads, so only an admin may read them.
EPerson currentUser = context.getCurrentUser();
ClarinUserMetadata userMetadatum = userMetadata.get(0);

// Check if the userRegistration is not null
if (Objects.isNull(userMetadatum.getEperson())) {
return;
}

UUID userRegistrationEpersonUUID = userMetadatum.getEperson().getPersonID();
if (currentUser.getID().equals(userRegistrationEpersonUUID)) {
ClarinUserRegistration userRegistration = userMetadata.get(0).getEperson();
if (Objects.nonNull(currentUser) && Objects.nonNull(userRegistration)
&& currentUser.getID().equals(userRegistration.getPersonID())) {
return;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -140,14 +140,10 @@ private void authorizeClarinUserRegistrationAction(Context context, List<ClarinU
return;
}

// Check if the user is the same as from the userRegistration
// Do not allow to get the userRegistration of another user
// Only the owner may read it
EPerson currentUser = context.getCurrentUser();
ClarinUserRegistration clarinUserRegistration = userRegistrationList.get(0);
UUID userRegistrationEpersonUUID = clarinUserRegistration.getPersonID();
// An anonymous request has no current user. Deny it here instead of throwing a NullPointerException,
// so that the callers can translate the AuthorizeException into 401/403.
if (Objects.nonNull(currentUser) && currentUser.getID().equals(userRegistrationEpersonUUID)) {
if (Objects.nonNull(currentUser) && currentUser.getID().equals(clarinUserRegistration.getPersonID())) {
return;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,16 +68,21 @@ public Page<ClarinLicenseResourceUserAllowanceRest> findAll(Context context, Pag
}

@SearchRestMethod(name = "byBitstreamAndUser")
@PreAuthorize("hasAuthority('AUTHENTICATED')")
public Page<ClarinLicenseResourceUserAllowanceRest> 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> clarinLicenseResourceUserAllowance =
clarinLicenseResourceUserAllowanceService.findByEPersonIdAndBitstreamId(context, userUUID,
bitstreamUUID);
List<ClarinLicenseResourceUserAllowance> clarinLicenseResourceUserAllowance;
try {
clarinLicenseResourceUserAllowance = clarinLicenseResourceUserAllowanceService
.findByEPersonIdAndBitstreamId(context, userUUID, bitstreamUUID);
} catch (AuthorizeException e) {
throw new RESTAuthorizationException(e);
}

if (CollectionUtils.isEmpty(clarinLicenseResourceUserAllowance)) {
return null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,14 +64,20 @@ public Page<ClarinUserMetadataRest> findAll(Context context, Pageable pageable)
}

@SearchRestMethod(name = "byUserRegistrationAndBitstream")
@PreAuthorize("hasAuthority('AUTHENTICATED')")
public Page<ClarinUserMetadataRest> 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<ClarinUserMetadata> clarinUserMetadataList =
clarinUserMetadataService.findByUserRegistrationAndBitstream(context, userRegId, bitstreamUUID, true);
List<ClarinUserMetadata> clarinUserMetadataList;
try {
clarinUserMetadataList = clarinUserMetadataService
.findByUserRegistrationAndBitstream(context, userRegId, bitstreamUUID, true);
} catch (AuthorizeException e) {
throw new RESTAuthorizationException(e);
}

return converter.toRestPage(clarinUserMetadataList, pageable, utils.obtainProjection());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -80,14 +80,18 @@ protected ClarinUserRegistrationRest createAndReturn(Context context) throws SQL
}

@SearchRestMethod(name = "byEPerson")
@PreAuthorize("hasAuthority('AUTHENTICATED')")
public Page<ClarinUserRegistrationRest> findByEPerson(@Parameter(value = "userUUID", required = true) UUID
userUUID,
Pageable pageable)
throws SQLException, AuthorizeException {
Pageable pageable) throws SQLException {
Context context = obtainContext();

List<ClarinUserRegistration> clarinUserRegistrationList =
clarinUserRegistrationService.findByEPersonUUID(context, userUUID);
List<ClarinUserRegistration> clarinUserRegistrationList;
try {
clarinUserRegistrationList = clarinUserRegistrationService.findByEPersonUUID(context, userUUID);
} catch (AuthorizeException e) {
throw new RESTAuthorizationException(e);
}
if (CollectionUtils.isEmpty(clarinUserRegistrationList)) {
return null;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -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<Integer> 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("[email protected]")
.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;
}
}
Loading
Loading