[CST-5680] Fixed bitstream content download error using special groups

This commit is contained in:
Luca Giamminonni
2022-05-25 12:46:38 +02:00
parent f062abdcb2
commit da14dca46c
4 changed files with 73 additions and 9 deletions

View File

@@ -657,6 +657,15 @@ public class Context implements AutoCloseable {
return myGroups;
}
/**
* Get a set of all of the special groups uuids that current user is a member of.
*
* @return list of special groups uuids
*/
public Set<UUID> getSpecialGroupUuids() {
return CollectionUtils.isEmpty(specialGroups) ? Set.of() : specialGroups;
}
/**
* Temporary change the user bound to the context, empty the special groups that
* are retained to allow subsequent restore

View File

@@ -153,8 +153,9 @@ public class BitstreamRestController {
}
org.dspace.app.rest.utils.BitstreamResource bitstreamResource =
new org.dspace.app.rest.utils.BitstreamResource(
name, uuid, currentUser != null ? currentUser.getID() : null, citationEnabledForBitstream);
new org.dspace.app.rest.utils.BitstreamResource(name, uuid,
currentUser != null ? currentUser.getID() : null,
context.getSpecialGroupUuids(), citationEnabledForBitstream);
//We have all the data we need, close the connection to the database so that it doesn't stay open during
//download/streaming

View File

@@ -11,6 +11,7 @@ import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.sql.SQLException;
import java.util.Set;
import java.util.UUID;
import org.apache.commons.io.IOUtils;
@@ -40,6 +41,7 @@ public class BitstreamResource extends AbstractResource {
private UUID currentUserUUID;
private boolean shouldGenerateCoverPage;
private byte[] file;
private Set<UUID> currentSpecialGroups;
private BitstreamService bitstreamService = ContentServiceFactory.getInstance().getBitstreamService();
private EPersonService ePersonService = EPersonServiceFactory.getInstance().getEPersonService();
@@ -47,11 +49,12 @@ public class BitstreamResource extends AbstractResource {
new DSpace().getServiceManager()
.getServicesByType(CitationDocumentService.class).get(0);
public BitstreamResource(String name, UUID uuid, UUID currentUserUUID,
public BitstreamResource(String name, UUID uuid, UUID currentUserUUID, Set<UUID> currentSpecialGroups,
boolean shouldGenerateCoverPage) {
this.name = name;
this.uuid = uuid;
this.currentUserUUID = currentUserUUID;
this.currentSpecialGroups = currentSpecialGroups;
this.shouldGenerateCoverPage = shouldGenerateCoverPage;
}
@@ -84,9 +87,8 @@ public class BitstreamResource extends AbstractResource {
@Override
public InputStream getInputStream() throws IOException {
try (Context context = new Context()) {
EPerson currentUser = ePersonService.find(context, currentUserUUID);
context.setCurrentUser(currentUser);
try (Context context = initializeContext()) {
Bitstream bitstream = bitstreamService.find(context, uuid);
InputStream out;
@@ -110,9 +112,7 @@ public class BitstreamResource extends AbstractResource {
@Override
public long contentLength() throws IOException {
try (Context context = new Context()) {
EPerson currentUser = ePersonService.find(context, currentUserUUID);
context.setCurrentUser(currentUser);
try (Context context = initializeContext()) {
Bitstream bitstream = bitstreamService.find(context, uuid);
if (shouldGenerateCoverPage) {
return getCoverpageByteArray(context, bitstream).length;
@@ -123,4 +123,12 @@ public class BitstreamResource extends AbstractResource {
throw new IOException(e);
}
}
private Context initializeContext() throws SQLException {
Context context = new Context();
EPerson currentUser = ePersonService.find(context, currentUserUUID);
context.setCurrentUser(currentUser);
currentSpecialGroups.forEach(context::setSpecialGroup);
return context;
}
}

View File

@@ -684,6 +684,52 @@ public class BitstreamRestControllerIT extends AbstractControllerIntegrationTest
checkNumberOfStatsRecords(bitstream, 1);
}
@Test
public void restrictedSpecialGroupBitstreamTest() throws Exception {
context.turnOffAuthorisationSystem();
parentCommunity = CommunityBuilder.createCommunity(context)
.withName("Parent Community")
.build();
Collection col1 = CollectionBuilder.createCollection(context, parentCommunity)
.withName("Collection 1")
.build();
Group restrictedGroup = GroupBuilder.createGroup(context)
.withName("Restricted Group")
.build();
String bitstreamContent = "Private!";
try (InputStream is = IOUtils.toInputStream(bitstreamContent, CharEncoding.UTF_8)) {
Item item = ItemBuilder.createItem(context, col1)
.withTitle("item 1")
.withIssueDate("2013-01-17")
.withAuthor("Doe, John")
.build();
bitstream = BitstreamBuilder
.createBitstream(context, item, is)
.withName("Test Embargoed Bitstream")
.withDescription("This bitstream is embargoed")
.withMimeType("text/plain")
.withReaderGroup(restrictedGroup)
.build();
}
context.restoreAuthSystemState();
configurationService.setProperty("authentication-password.login.specialgroup", "Restricted Group");
String authToken = getAuthToken(eperson.getEmail(), password);
getClient(authToken).perform(get("/api/core/bitstreams/" + bitstream.getID() + "/content"))
.andExpect(status().isOk());
checkNumberOfStatsRecords(bitstream, 1);
}
@Test
public void restrictedGroupBitstreamAccessGrantByAdminsTest() throws Exception {
context.turnOffAuthorisationSystem();