fix(magic-shelf): remove OPDS permission gate (#2144)

This commit is contained in:
Alex
2026-07-27 18:31:49 +01:00
committed by GitHub
parent 111670ef49
commit f410ecb5ef
4 changed files with 7 additions and 26 deletions
@@ -31,4 +31,10 @@ public class OpdsUserDetails implements UserDetails {
public Collection<? extends GrantedAuthority> getAuthorities() { public Collection<? extends GrantedAuthority> getAuthorities() {
return List.of(); return List.of();
} }
@Override
public boolean isEnabled() {
BookLoreUser.UserPermissions permissions = user.getPermissions();
return permissions != null && (permissions.isAdmin() || permissions.isCanAccessOpds());
}
} }
@@ -127,14 +127,9 @@ public class MagicShelfBookService {
BookLoreUserEntity entity = userRepository.findByIdWithDetails(userId) BookLoreUserEntity entity = userRepository.findByIdWithDetails(userId)
.orElseThrow(() -> ApiError.USER_NOT_FOUND.createException(userId)); .orElseThrow(() -> ApiError.USER_NOT_FOUND.createException(userId));
if (entity.getPermissions() == null ||
(!entity.getPermissions().isPermissionAccessOpds() && !entity.getPermissions().isPermissionAdmin())) {
throw ApiError.FORBIDDEN.createException("You are not allowed to access this resource");
}
boolean isOwner = shelf.getUserId().equals(userId); boolean isOwner = shelf.getUserId().equals(userId);
boolean isPublic = shelf.isPublic(); boolean isPublic = shelf.isPublic();
boolean isAdmin = entity.getPermissions().isPermissionAdmin(); boolean isAdmin = entity.getPermissions() != null && entity.getPermissions().isPermissionAdmin();
if (!isOwner && !isPublic && !isAdmin) { if (!isOwner && !isPublic && !isAdmin) {
throw ApiError.FORBIDDEN.createException("You are not allowed to access this magic shelf"); throw ApiError.FORBIDDEN.createException("You are not allowed to access this magic shelf");
@@ -75,11 +75,6 @@ public class OpdsBookService {
BookLoreUserEntity entity = userRepository.findByIdWithDetails(userId) BookLoreUserEntity entity = userRepository.findByIdWithDetails(userId)
.orElseThrow(() -> ApiError.USER_NOT_FOUND.createException(userId)); .orElseThrow(() -> ApiError.USER_NOT_FOUND.createException(userId));
if (entity.getPermissions() == null ||
(!entity.getPermissions().isPermissionAccessOpds() && !entity.getPermissions().isPermissionAdmin())) {
throw ApiError.FORBIDDEN.createException("You are not allowed to access this resource");
}
BookLoreUser user = bookLoreUserTransformer.toDTO(entity); BookLoreUser user = bookLoreUserTransformer.toDTO(entity);
boolean isAdmin = user.getPermissions().isAdmin(); boolean isAdmin = user.getPermissions().isAdmin();
Set<Long> userLibraryIds = getAccessibleLibraries(userId).stream().map(Library::getId).collect(Collectors.toSet()); Set<Long> userLibraryIds = getAccessibleLibraries(userId).stream().map(Library::getId).collect(Collectors.toSet());
@@ -340,21 +340,6 @@ class OpdsBookServiceTest {
assertThat(result).isEmpty(); assertThat(result).isEmpty();
} }
@Test
void getBooksPageForV2User_throwsForbidden_whenNoPermission() {
OpdsUserV2 v2 = OpdsUserV2.builder().userId(1L).build();
BookLoreUserEntity entity = mock(BookLoreUserEntity.class);
var permissionsEntity = mock(UserPermissionsEntity.class);
when(permissionsEntity.isPermissionAccessOpds()).thenReturn(false);
when(permissionsEntity.isPermissionAdmin()).thenReturn(false);
when(entity.getPermissions()).thenReturn(permissionsEntity);
when(userRepository.findByIdWithDetails(1L)).thenReturn(Optional.of(entity));
assertThatThrownBy(() ->
opdsBookService.getBooksPage(1L, null, null, null, 0, 10)
).hasMessageContaining("You are not allowed to access this resource");
}
@Test @Test
void getBooksPage_withSingleShelfId_returnsShelfBooks() { void getBooksPage_withSingleShelfId_returnsShelfBooks() {
OpdsUserDetails details = v2UserDetails(1L, false, Set.of(1L)); OpdsUserDetails details = v2UserDetails(1L, false, Set.of(1L));