[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; 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 * Temporary change the user bound to the context, empty the special groups that
* are retained to allow subsequent restore * are retained to allow subsequent restore

View File

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

View File

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