fix: support getAuthenticatedUser() in OPDS (#2041)
This commit is contained in:
@@ -1,6 +1,7 @@
|
||||
package org.booklore.config.security;
|
||||
|
||||
import lombok.AllArgsConstructor;
|
||||
import org.booklore.config.security.userdetails.OpdsUserDetails;
|
||||
import org.booklore.model.dto.BookLoreUser;
|
||||
import org.booklore.repository.ShelfRepository;
|
||||
import org.springframework.security.core.context.SecurityContextHolder;
|
||||
@@ -14,8 +15,14 @@ public class SecurityUtil {
|
||||
|
||||
private BookLoreUser getCurrentUser() {
|
||||
var authentication = SecurityContextHolder.getContext().getAuthentication();
|
||||
if (authentication != null && authentication.getPrincipal() instanceof BookLoreUser user) {
|
||||
return user;
|
||||
if (authentication != null) {
|
||||
if (authentication.getPrincipal() instanceof BookLoreUser user) {
|
||||
return user;
|
||||
}
|
||||
|
||||
if (authentication.getPrincipal() instanceof OpdsUserDetails opdsUser) {
|
||||
return opdsUser.getUser();
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
+5
-1
@@ -3,6 +3,7 @@ package org.booklore.config.security.service;
|
||||
import jakarta.annotation.PostConstruct;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.booklore.model.dto.AccessTokenDto;
|
||||
import org.booklore.model.dto.OpdsUser;
|
||||
import org.springframework.context.annotation.Lazy;
|
||||
import org.booklore.config.AppProperties;
|
||||
import org.booklore.config.security.JwtUtils;
|
||||
@@ -85,13 +86,16 @@ public class AuthenticationService {
|
||||
return null;
|
||||
}
|
||||
Object principal = authentication.getPrincipal();
|
||||
if (principal instanceof OpdsUserDetails opdsUser) {
|
||||
principal = opdsUser.getUser();
|
||||
}
|
||||
if (principal instanceof BookLoreUser user) {
|
||||
if (user.getId() != null && user.getId() != -1L) {
|
||||
defaultSettingInitializer.ensureDefaultSettings(user);
|
||||
}
|
||||
return user;
|
||||
}
|
||||
throw new IllegalStateException("Authenticated principal is not of type BookLoreUser");
|
||||
throw new IllegalStateException("Authenticated principal is not of expected type");
|
||||
}
|
||||
|
||||
public BookLoreUser getSystemUser() {
|
||||
|
||||
+9
-1
@@ -3,7 +3,10 @@ package org.booklore.config.security.service;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import org.booklore.config.security.userdetails.OpdsUserDetails;
|
||||
import org.booklore.mapper.OpdsUserV2Mapper;
|
||||
import org.booklore.mapper.custom.BookLoreUserTransformer;
|
||||
import org.booklore.model.dto.BookLoreUser;
|
||||
import org.booklore.model.dto.OpdsUserV2;
|
||||
import org.booklore.model.entity.BookLoreUserEntity;
|
||||
import org.booklore.model.entity.OpdsUserV2Entity;
|
||||
import org.booklore.repository.OpdsUserV2Repository;
|
||||
import org.springframework.security.core.userdetails.UserDetailsService;
|
||||
@@ -16,6 +19,7 @@ import org.springframework.transaction.annotation.Transactional;
|
||||
@RequiredArgsConstructor
|
||||
public class OpdsUserDetailsService implements UserDetailsService {
|
||||
|
||||
private final BookLoreUserTransformer bookLoreUserTransformer;
|
||||
private final OpdsUserV2Repository opdsUserV2Repository;
|
||||
private final OpdsUserV2Mapper opdsUserV2Mapper;
|
||||
|
||||
@@ -24,7 +28,11 @@ public class OpdsUserDetailsService implements UserDetailsService {
|
||||
public OpdsUserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
|
||||
OpdsUserV2Entity userV2 = opdsUserV2Repository.findByUsername(username)
|
||||
.orElseThrow(() -> new UsernameNotFoundException("Invalid credentials"));
|
||||
BookLoreUser appUser = bookLoreUserTransformer.toDTO(userV2.getUser());
|
||||
OpdsUserV2 mappedCredential = opdsUserV2Mapper.toDto(userV2);
|
||||
return new OpdsUserDetails(mappedCredential);
|
||||
return new OpdsUserDetails(
|
||||
appUser,
|
||||
mappedCredential
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -1,5 +1,6 @@
|
||||
package org.booklore.config.security.userdetails;
|
||||
|
||||
import org.booklore.model.dto.BookLoreUser;
|
||||
import org.booklore.model.dto.OpdsUserV2;
|
||||
import lombok.Getter;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
@@ -12,6 +13,7 @@ import java.util.List;
|
||||
@Getter
|
||||
@RequiredArgsConstructor
|
||||
public class OpdsUserDetails implements UserDetails {
|
||||
private final BookLoreUser user;
|
||||
|
||||
private final OpdsUserV2 opdsUserV2;
|
||||
|
||||
|
||||
@@ -67,7 +67,11 @@ class OpdsBookServiceTest {
|
||||
|
||||
private OpdsUserDetails legacyUserDetails() {
|
||||
OpdsUserV2 v2 = OpdsUserV2.builder().userId(999L).username("legacy").build();
|
||||
return new OpdsUserDetails(v2);
|
||||
BookLoreUser user = BookLoreUser.builder().username("legacy").id(999L).build();
|
||||
return new OpdsUserDetails(
|
||||
user,
|
||||
v2
|
||||
);
|
||||
}
|
||||
|
||||
private OpdsUserDetails v2UserDetails(Long userId, boolean isAdmin, Set<Long> libraryIds) {
|
||||
@@ -88,7 +92,10 @@ class OpdsBookServiceTest {
|
||||
}
|
||||
when(user.getAssignedLibraries()).thenReturn(libraries);
|
||||
|
||||
return new OpdsUserDetails(v2);
|
||||
return new OpdsUserDetails(
|
||||
user,
|
||||
v2
|
||||
);
|
||||
}
|
||||
|
||||
@Test
|
||||
|
||||
Reference in New Issue
Block a user