refactor(komga): stop using opds service for book access checks (#1479)
This commit is contained in:
@@ -8,14 +8,17 @@ import lombok.extern.slf4j.Slf4j;
|
||||
import org.booklore.config.JacksonConfig;
|
||||
import org.booklore.config.security.service.AuthenticationService;
|
||||
import org.booklore.config.security.userdetails.OpdsUserDetails;
|
||||
import org.booklore.exception.ApiError;
|
||||
import org.booklore.mapper.komga.KomgaMapper;
|
||||
import org.booklore.model.dto.BookLoreUser;
|
||||
import org.booklore.model.dto.komga.KomgaBookDto;
|
||||
import org.booklore.model.dto.komga.KomgaLibraryDto;
|
||||
import org.booklore.model.dto.komga.KomgaPageableDto;
|
||||
import org.booklore.model.dto.komga.KomgaSeriesDto;
|
||||
import org.booklore.model.entity.BookEntity;
|
||||
import org.booklore.model.entity.BookLoreUserEntity;
|
||||
import org.booklore.service.book.BookService;
|
||||
import org.booklore.service.komga.KomgaService;
|
||||
import org.booklore.service.opds.OpdsBookService;
|
||||
import org.booklore.service.opds.OpdsUserV2Service;
|
||||
import org.springframework.beans.factory.annotation.Qualifier;
|
||||
import org.springframework.core.io.Resource;
|
||||
@@ -39,7 +42,6 @@ public class KomgaController {
|
||||
|
||||
private final KomgaService komgaService;
|
||||
private final BookService bookService;
|
||||
private final OpdsBookService opdsBookService;
|
||||
private final AuthenticationService authenticationService;
|
||||
private final OpdsUserV2Service opdsUserV2Service;
|
||||
private final KomgaMapper komgaMapper;
|
||||
@@ -58,6 +60,18 @@ public class KomgaController {
|
||||
}
|
||||
}
|
||||
|
||||
private void validateBookContentAccess(Long bookId) {
|
||||
BookLoreUser user = authenticationService.getAuthenticatedUser();
|
||||
|
||||
if (user == null) {
|
||||
throw ApiError.FORBIDDEN.createException("Authentication required");
|
||||
}
|
||||
|
||||
if (!komgaService.validateBookContentAccess(user, bookId)) {
|
||||
throw ApiError.BOOK_NOT_FOUND.createException(bookId);
|
||||
}
|
||||
}
|
||||
|
||||
// ==================== Libraries ====================
|
||||
|
||||
@Operation(summary = "List all libraries")
|
||||
@@ -152,7 +166,8 @@ public class KomgaController {
|
||||
@Parameter(description = "Book ID") @PathVariable Long bookId,
|
||||
@Parameter(description = "Page number") @PathVariable Integer pageNumber,
|
||||
@Parameter(description = "Convert image format (e.g., 'png')") @RequestParam(required = false) String convert) {
|
||||
opdsBookService.validateBookContentAccess(bookId, getOpdsUserId());
|
||||
validateBookContentAccess(bookId);
|
||||
|
||||
try {
|
||||
boolean convertToPng = "png".equalsIgnoreCase(convert);
|
||||
Resource pageImage = komgaService.getBookPageImage(bookId, pageNumber, convertToPng);
|
||||
@@ -172,7 +187,8 @@ public class KomgaController {
|
||||
@GetMapping("/v1/books/{bookId}/file")
|
||||
public ResponseEntity<Resource> downloadBook(
|
||||
@Parameter(description = "Book ID") @PathVariable Long bookId) {
|
||||
opdsBookService.validateBookContentAccess(bookId, getOpdsUserId());
|
||||
validateBookContentAccess(bookId);
|
||||
|
||||
return bookService.downloadBook(bookId);
|
||||
}
|
||||
|
||||
@@ -180,7 +196,8 @@ public class KomgaController {
|
||||
@GetMapping("/v1/books/{bookId}/thumbnail")
|
||||
public ResponseEntity<Resource> getBookThumbnail(
|
||||
@Parameter(description = "Book ID") @PathVariable Long bookId) {
|
||||
opdsBookService.validateBookContentAccess(bookId, getOpdsUserId());
|
||||
validateBookContentAccess(bookId);
|
||||
|
||||
Resource coverImage = bookService.getBookThumbnail(bookId);
|
||||
return ResponseEntity.ok()
|
||||
.header("Content-Type", "image/jpeg")
|
||||
@@ -216,11 +233,4 @@ public class KomgaController {
|
||||
@Parameter(description = "Return all collections without paging") @RequestParam(defaultValue = "false") boolean unpaged) {
|
||||
return writeJson(komgaService.getCollections(page, size, unpaged));
|
||||
}
|
||||
|
||||
private Long getOpdsUserId() {
|
||||
OpdsUserDetails details = authenticationService.getOpdsUser();
|
||||
return details != null && details.getOpdsUserV2() != null
|
||||
? details.getOpdsUserV2().getUserId()
|
||||
: null;
|
||||
}
|
||||
}
|
||||
@@ -2,7 +2,9 @@ package org.booklore.service.komga;
|
||||
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.booklore.exception.ApiError;
|
||||
import org.booklore.mapper.komga.KomgaMapper;
|
||||
import org.booklore.model.dto.BookLoreUser;
|
||||
import org.booklore.model.dto.MagicShelf;
|
||||
import org.booklore.model.dto.komga.*;
|
||||
import org.booklore.model.entity.BookEntity;
|
||||
@@ -15,6 +17,7 @@ import org.booklore.service.MagicShelfService;
|
||||
import org.booklore.service.appsettings.AppSettingService;
|
||||
import org.booklore.service.reader.CbxReaderService;
|
||||
import org.booklore.service.reader.PdfReaderService;
|
||||
import org.booklore.service.restriction.ContentRestrictionService;
|
||||
import org.springframework.core.io.ByteArrayResource;
|
||||
import org.springframework.core.io.Resource;
|
||||
import org.springframework.data.domain.Page;
|
||||
@@ -45,6 +48,40 @@ public class KomgaService {
|
||||
private final CbxReaderService cbxReaderService;
|
||||
private final PdfReaderService pdfReaderService;
|
||||
private final AppSettingService appSettingService;
|
||||
private final ContentRestrictionService contentRestrictionService;
|
||||
|
||||
|
||||
public boolean validateBookContentAccess(BookLoreUser user, Long bookId) {
|
||||
if (user == null) {
|
||||
return false;
|
||||
}
|
||||
|
||||
if (user.getPermissions() != null && user.getPermissions().isAdmin()) {
|
||||
return true;
|
||||
}
|
||||
|
||||
BookEntity book = bookRepository.findById(bookId)
|
||||
.orElse(null);
|
||||
|
||||
if (book == null) {
|
||||
return false;
|
||||
}
|
||||
|
||||
boolean hasLibraryAccess = user.getAssignedLibraries() != null && user.getAssignedLibraries().stream()
|
||||
.anyMatch(library -> library.getId().equals(book.getLibrary().getId()));
|
||||
|
||||
if (!hasLibraryAccess) {
|
||||
return false;
|
||||
}
|
||||
|
||||
List<BookEntity> filtered = contentRestrictionService.applyRestrictions(List.of(book), user.getId());
|
||||
if (filtered.isEmpty()) {
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
public List<KomgaLibraryDto> getAllLibraries() {
|
||||
return libraryRepository.findAll().stream()
|
||||
|
||||
Reference in New Issue
Block a user