Skip to content

Commit 85d6cba

Browse files
committed
Fix Dashboard Alert and view Files
1 parent 21bb46f commit 85d6cba

File tree

3 files changed

+17
-2
lines changed

3 files changed

+17
-2
lines changed

src/main/java/de/jadenk/springcloud/controller/DashboardController.java

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -139,11 +139,13 @@ public String dashboard(@RequestParam(value = "error", required = false) String
139139
}
140140
}
141141

142-
// Fehlermeldungen beim Upload
142+
// Fehlermeldungen
143143
if ("uploadError".equals(error)) {
144144
model.addAttribute("error", "There was an Error while Uploading. Try again later.");
145145
} else if ("uploadInProgress".equals(error)) {
146146
model.addAttribute("error", "There is an current Upload in Progress.");
147+
} else if ("NoAccess".equals(error)) {
148+
model.addAttribute("error", "You aren't Allowed to see this.");
147149
} else if (error != null) {
148150
model.addAttribute("error", "An Error occurred.");
149151
}
@@ -287,9 +289,18 @@ public ResponseEntity<ByteArrayResource> downloadFile(@PathVariable Long id) {
287289
*/
288290
@GetMapping("/file/{fileId}")
289291
public ResponseEntity<Resource> getFile(@PathVariable Long fileId) {
292+
UserDetails currentUserDetails = (UserDetails) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
293+
User currentUser = userRepository.findByUsername(currentUserDetails.getUsername()).orElseThrow();
294+
290295
UploadedFile file = uploadedFileRepository.findById(fileId)
291296
.orElseThrow(() -> new ResourceNotFoundException("File not found"));
292297

298+
// Berechtigungsprüfung
299+
if (!file.getFileOwner().getId().equals(currentUser.getId())
300+
&& !fileAuthorizationService.isUserAuthorized(fileId, currentUser.getId())) {
301+
throw new ResourceNotFoundException("File not found"); // oder AccessDeniedException
302+
}
303+
293304
return ResponseEntity.ok()
294305
.contentType(MediaType.parseMediaType(file.getFileType()))
295306
.body(new ByteArrayResource(file.getFileData()));

src/main/java/de/jadenk/springcloud/exception/GlobalExceptionHandler.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,4 +17,8 @@ public void handleCustomRuntimeException(CustomRuntimeException ex) {
1717
webhookService.triggerWebhookEvent(WebhookEvent.ERROR_THROWN, ex.getMessage(), 0L);
1818
}
1919

20+
@ExceptionHandler(ResourceNotFoundException.class)
21+
public String handleResourceNotFound() {
22+
return "redirect:/dashboard?error=NoAccess";
23+
}
2024
}

src/main/resources/templates/dashboard.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
<!-- <script th:src="@{/js/dashboard.js}"></script>-->
1111
</head>
1212
<body class="bg-[var(--color-bg)] text-[var(--color-text)] font-sans min-h-screen">
13-
13+
<div th:replace="~{fragments/alert}"></div>
1414
<nav class="sticky top-0 z-50 w-full flex justify-between items-center flex-wrap
1515
bg-gradient-to-r from-[var(--color-bg)] to-[var(--color-bg-alt)]
1616
px-8 py-3 shadow-lg border-b-4 border-[var(--color-primary)] font-sans">

0 commit comments

Comments
 (0)