diff --git a/booklore-api/src/main/java/org/booklore/repository/AuthorRepository.java b/booklore-api/src/main/java/org/booklore/repository/AuthorRepository.java index cedc9c5b4..6ff2e1066 100644 --- a/booklore-api/src/main/java/org/booklore/repository/AuthorRepository.java +++ b/booklore-api/src/main/java/org/booklore/repository/AuthorRepository.java @@ -8,6 +8,7 @@ import org.springframework.stereotype.Repository; import java.util.List; import java.util.Optional; +import java.util.Set; @Repository public interface AuthorRepository extends JpaRepository { @@ -23,4 +24,10 @@ public interface AuthorRepository extends JpaRepository { @Query("SELECT a, COUNT(bm) FROM AuthorEntity a LEFT JOIN a.bookMetadataEntityList bm GROUP BY a ORDER BY a.name") List findAllWithBookCount(); + + @Query("SELECT a, COUNT(DISTINCT bm) FROM AuthorEntity a LEFT JOIN a.bookMetadataEntityList bm JOIN bm.book b WHERE b.library.id IN :libraryIds GROUP BY a ORDER BY a.name") + List findAllWithBookCountByLibraryIds(@Param("libraryIds") Set libraryIds); + + @Query("SELECT COUNT(b) > 0 FROM AuthorEntity a JOIN a.bookMetadataEntityList bm JOIN bm.book b WHERE a.id = :authorId AND b.library.id IN :libraryIds") + boolean existsByIdAndLibraryIds(@Param("authorId") Long authorId, @Param("libraryIds") Set libraryIds); } diff --git a/booklore-api/src/main/java/org/booklore/service/AuthorMetadataService.java b/booklore-api/src/main/java/org/booklore/service/AuthorMetadataService.java index 56b159b40..8ef79df2f 100644 --- a/booklore-api/src/main/java/org/booklore/service/AuthorMetadataService.java +++ b/booklore-api/src/main/java/org/booklore/service/AuthorMetadataService.java @@ -3,10 +3,13 @@ package org.booklore.service; import lombok.AllArgsConstructor; import lombok.extern.slf4j.Slf4j; import org.booklore.exception.ApiError; +import org.booklore.config.security.service.AuthenticationService; import org.booklore.model.dto.AuthorDetails; import org.booklore.model.dto.AuthorSearchResult; import org.booklore.model.dto.AuthorSummary; +import org.booklore.model.dto.BookLoreUser; import org.booklore.model.dto.CoverImage; +import org.booklore.model.dto.Library; import org.booklore.model.dto.request.AuthorMatchRequest; import org.booklore.model.dto.request.AuthorUpdateRequest; import org.booklore.model.entity.AuthorEntity; @@ -32,6 +35,8 @@ import java.nio.file.Paths; import java.util.ArrayList; import java.util.List; import java.util.Map; +import java.util.Set; +import java.util.stream.Collectors; @Slf4j @Service @@ -43,9 +48,19 @@ public class AuthorMetadataService { private final AuditService auditService; private final FileService fileService; private final DuckDuckGoCoverService duckDuckGoCoverService; + private final AuthenticationService authenticationService; public List getAllAuthors() { - List results = authorRepository.findAllWithBookCount(); + BookLoreUser user = authenticationService.getAuthenticatedUser(); + List results; + if (user.getPermissions().isAdmin()) { + results = authorRepository.findAllWithBookCount(); + } else { + Set libraryIds = user.getAssignedLibraries().stream() + .map(Library::getId) + .collect(Collectors.toSet()); + results = authorRepository.findAllWithBookCountByLibraryIds(libraryIds); + } List summaries = new ArrayList<>(); for (Object[] row : results) { AuthorEntity author = (AuthorEntity) row[0]; @@ -244,12 +259,14 @@ public class AuthorMetadataService { public AuthorDetails getAuthorByName(String name) { AuthorEntity author = authorRepository.findByNameIgnoreCase(name) .orElseThrow(() -> ApiError.AUTHOR_NOT_FOUND.createException(name)); + verifyAuthorAccess(author.getId()); return toAuthorDetails(author); } public AuthorDetails getAuthorDetails(Long authorId) { AuthorEntity author = authorRepository.findById(authorId) .orElseThrow(() -> ApiError.AUTHOR_NOT_FOUND.createException(authorId)); + verifyAuthorAccess(authorId); return toAuthorDetails(author); } @@ -277,6 +294,19 @@ public class AuthorMetadataService { return null; } + private void verifyAuthorAccess(Long authorId) { + BookLoreUser user = authenticationService.getAuthenticatedUser(); + if (user.getPermissions().isAdmin()) { + return; + } + Set libraryIds = user.getAssignedLibraries().stream() + .map(Library::getId) + .collect(Collectors.toSet()); + if (libraryIds.isEmpty() || !authorRepository.existsByIdAndLibraryIds(authorId, libraryIds)) { + throw ApiError.AUTHOR_NOT_FOUND.createException(authorId); + } + } + private void applyMetadataResult(AuthorEntity author, AuthorSearchResult result) { if (!author.isDescriptionLocked()) { author.setDescription(result.getDescription()); diff --git a/booklore-api/src/test/java/org/booklore/service/AuthorMetadataServiceTest.java b/booklore-api/src/test/java/org/booklore/service/AuthorMetadataServiceTest.java index 0942118f8..0c4a0043d 100644 --- a/booklore-api/src/test/java/org/booklore/service/AuthorMetadataServiceTest.java +++ b/booklore-api/src/test/java/org/booklore/service/AuthorMetadataServiceTest.java @@ -1,8 +1,10 @@ package org.booklore.service; +import org.booklore.config.security.service.AuthenticationService; import org.booklore.exception.APIException; import org.booklore.model.dto.AuthorDetails; import org.booklore.model.dto.AuthorSearchResult; +import org.booklore.model.dto.BookLoreUser; import org.booklore.model.dto.request.AuthorMatchRequest; import org.booklore.model.entity.AuthorEntity; import org.booklore.model.enums.AuditAction; @@ -28,6 +30,7 @@ import static org.assertj.core.api.Assertions.assertThatThrownBy; import static org.mockito.ArgumentMatchers.any; import static org.mockito.ArgumentMatchers.eq; import static org.mockito.Mockito.*; +import static org.mockito.Mockito.lenient; @ExtendWith(MockitoExtension.class) class AuthorMetadataServiceTest { @@ -37,6 +40,7 @@ class AuthorMetadataServiceTest { @Mock private AuditService auditService; @Mock private FileService fileService; @Mock private DuckDuckGoCoverService duckDuckGoCoverService; + @Mock private AuthenticationService authenticationService; private AuthorMetadataService service; @@ -45,7 +49,12 @@ class AuthorMetadataServiceTest { Map authorParserMap = Map.of( AuthorMetadataSource.AUDNEXUS, authorParser ); - service = new AuthorMetadataService(authorRepository, authorParserMap, auditService, fileService, duckDuckGoCoverService); + service = new AuthorMetadataService(authorRepository, authorParserMap, auditService, fileService, duckDuckGoCoverService, authenticationService); + + BookLoreUser.UserPermissions adminPermissions = new BookLoreUser.UserPermissions(); + adminPermissions.setAdmin(true); + BookLoreUser adminUser = BookLoreUser.builder().id(1L).permissions(adminPermissions).build(); + lenient().when(authenticationService.getAuthenticatedUser()).thenReturn(adminUser); } @Test