Skip to content

Commit 7c24cfd

Browse files
committed
authorization by spring
1 parent 1185123 commit 7c24cfd

2 files changed

Lines changed: 5 additions & 9 deletions

File tree

dspace-server-webapp/src/main/java/org/dspace/app/rest/MetadataBitstreamController.java

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -26,12 +26,12 @@
2626
import org.dspace.content.DSpaceObject;
2727
import org.dspace.content.Item;
2828
import org.dspace.content.service.BitstreamService;
29-
import org.dspace.core.Constants;
3029
import org.dspace.core.Context;
3130
import org.dspace.handle.service.HandleService;
3231
import org.springframework.beans.factory.annotation.Autowired;
3332
import org.springframework.http.HttpHeaders;
3433
import org.springframework.security.access.AccessDeniedException;
34+
import org.springframework.security.access.prepost.PreAuthorize;
3535
import org.springframework.web.bind.annotation.GetMapping;
3636
import org.springframework.web.bind.annotation.PathVariable;
3737
import org.springframework.web.bind.annotation.RequestMapping;
@@ -77,6 +77,7 @@ public class MetadataBitstreamController {
7777
* @throws UnprocessableEntityException if the handle does not resolve to a valid Item
7878
* or if the bitstream with the specified name is not found
7979
*/
80+
@PreAuthorize("hasPermission(#handleId, 'ITEM', 'READ')")
8081
@GetMapping("/handle/{prefix}/{suffix}/{name:.+}")
8182
public void downloadBitstreamByName(
8283
@PathVariable String prefix,
@@ -101,11 +102,6 @@ public void downloadBitstreamByName(
101102

102103
Item item = (Item) dso;
103104

104-
// Check READ permission on the actual Item object
105-
if (!authorizeService.authorizeActionBoolean(context, item, Constants.READ)) {
106-
throw new AuthorizeException("User does not have permission to read Item: " + item.getHandle());
107-
}
108-
109105
Bitstream targetBitstream = findBitstreamByName(item, name);
110106

111107
if (Objects.isNull(targetBitstream)) {
@@ -118,8 +114,8 @@ public void downloadBitstreamByName(
118114
.map(fmt -> fmt.getMIMEType())
119115
.orElse("application/octet-stream");
120116

121-
// Set content type without charset to match test expectations
122-
response.setHeader(HttpHeaders.CONTENT_TYPE, mime);
117+
// Set content type (tests use startsWith to tolerate charset if appended)
118+
response.setContentType(mime);
123119

124120
org.springframework.http.ContentDisposition cd =
125121
org.springframework.http.ContentDisposition.attachment()

dspace-server-webapp/src/test/java/org/dspace/app/rest/MetadataBitstreamControllerIT.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -140,7 +140,7 @@ public void downloadBitstreamWithSpecialCharactersTest() throws Exception {
140140
context.turnOffAuthorisationSystem();
141141

142142
String specialContent = "Content of file with special characters in name";
143-
String specialFileName = "test-file-with-spaces.pdf";
143+
String specialFileName = "test file with spaces & special chars (2024).pdf";
144144

145145
try (InputStream is = IOUtils.toInputStream(specialContent, CharEncoding.UTF_8)) {
146146
BitstreamBuilder.createBitstream(context, publicItem, is)

0 commit comments

Comments
 (0)