feat(users): store locale and theme preferences on the backend (#1571)
This commit is contained in:
@@ -4,6 +4,7 @@ import org.booklore.model.dto.request.ChangePasswordRequest;
|
||||
import org.booklore.model.dto.request.ChangeUserPasswordRequest;
|
||||
import org.booklore.model.dto.request.UpdateUserSettingRequest;
|
||||
import org.booklore.model.dto.BookLoreUser;
|
||||
import org.booklore.model.dto.request.UserProfileUpdateRequest;
|
||||
import org.booklore.model.dto.request.UserUpdateRequest;
|
||||
import org.booklore.service.user.UserService;
|
||||
import io.swagger.v3.oas.annotations.tags.Tag;
|
||||
@@ -53,7 +54,7 @@ public class UserController {
|
||||
return ResponseEntity.ok(userService.getBookLoreUsers());
|
||||
}
|
||||
|
||||
@Operation(summary = "Update user", description = "Update a user's profile by their ID. Requires admin.")
|
||||
@Operation(summary = "Update user", description = "Update a user's profile, permissions, and library assignments by their ID. Requires admin.")
|
||||
@ApiResponse(responseCode = "200", description = "User updated successfully")
|
||||
@PutMapping("/{id}")
|
||||
@PreAuthorize("@securityUtil.isAdmin()")
|
||||
@@ -64,6 +65,17 @@ public class UserController {
|
||||
return ResponseEntity.ok(updatedUser);
|
||||
}
|
||||
|
||||
@Operation(summary = "Update user profile", description = "Update a user's profile preferences by their ID. Allowed for admins and the user themself.")
|
||||
@ApiResponse(responseCode = "200", description = "User profile updated successfully")
|
||||
@PutMapping("/{id}/profile")
|
||||
@PreAuthorize("@securityUtil.isAdmin() or @securityUtil.isSelf(#id)")
|
||||
public ResponseEntity<BookLoreUser> updateUserProfile(
|
||||
@Parameter(description = "ID of the user") @PathVariable Long id,
|
||||
@Parameter(description = "User profile update request") @Valid @RequestBody UserProfileUpdateRequest updateRequest) {
|
||||
BookLoreUser updatedUser = userService.updateUserProfile(id, updateRequest);
|
||||
return ResponseEntity.ok(updatedUser);
|
||||
}
|
||||
|
||||
@Operation(summary = "Delete user", description = "Delete a user by their ID. Requires admin.")
|
||||
@ApiResponse(responseCode = "204", description = "User deleted successfully")
|
||||
@DeleteMapping("/{id}")
|
||||
|
||||
@@ -32,6 +32,10 @@ public class BookLoreUserTransformer {
|
||||
bookLoreUser.setUsername(userEntity.getUsername());
|
||||
bookLoreUser.setName(userEntity.getName());
|
||||
bookLoreUser.setEmail(userEntity.getEmail());
|
||||
bookLoreUser.setLocale(userEntity.getLocale());
|
||||
bookLoreUser.setTheme(userEntity.getTheme());
|
||||
bookLoreUser.setThemeAccent(userEntity.getThemeAccent());
|
||||
bookLoreUser.setThemeSyncEnabled(userEntity.isThemeSyncEnabled());
|
||||
bookLoreUser.setDefaultPassword(userEntity.isDefaultPassword());
|
||||
bookLoreUser.setPermissions(permissions);
|
||||
|
||||
@@ -92,4 +96,4 @@ public class BookLoreUserTransformer {
|
||||
bookLoreUser.setProvisioningMethod(userEntity.getProvisioningMethod());
|
||||
return bookLoreUser;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -21,6 +21,10 @@ public class BookLoreUser {
|
||||
private boolean isDefaultPassword;
|
||||
private String name;
|
||||
private String email;
|
||||
private String locale;
|
||||
private String theme;
|
||||
private String themeAccent;
|
||||
private boolean themeSyncEnabled;
|
||||
private ProvisioningMethod provisioningMethod;
|
||||
private List<Library> assignedLibraries;
|
||||
private UserPermissions permissions;
|
||||
|
||||
@@ -0,0 +1,13 @@
|
||||
package org.booklore.model.dto.request;
|
||||
|
||||
import lombok.Data;
|
||||
|
||||
@Data
|
||||
public class UserProfileUpdateRequest {
|
||||
private String name;
|
||||
private String email;
|
||||
private String locale;
|
||||
private String theme;
|
||||
private String themeAccent;
|
||||
private Boolean themeSyncEnabled;
|
||||
}
|
||||
@@ -39,6 +39,21 @@ public class BookLoreUserEntity {
|
||||
@Column(unique = true)
|
||||
private String email;
|
||||
|
||||
@Builder.Default
|
||||
@Column(nullable = false, length = 16)
|
||||
private String locale = "en";
|
||||
|
||||
@Builder.Default
|
||||
@Column(nullable = false, length = 32)
|
||||
private String theme = "grimmory";
|
||||
|
||||
@Column(name = "theme_accent", length = 32)
|
||||
private String themeAccent;
|
||||
|
||||
@Builder.Default
|
||||
@Column(name = "theme_sync_enabled", nullable = false)
|
||||
private boolean themeSyncEnabled = true;
|
||||
|
||||
@Column(name = "provisioning_method")
|
||||
@Enumerated(EnumType.STRING)
|
||||
private ProvisioningMethod provisioningMethod;
|
||||
@@ -104,4 +119,4 @@ public class BookLoreUserEntity {
|
||||
public final int hashCode() {
|
||||
return Hibernate.getClass(this).hashCode();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -8,6 +8,7 @@ import org.booklore.model.dto.BookLoreUser;
|
||||
import org.booklore.model.dto.request.ChangePasswordRequest;
|
||||
import org.booklore.model.dto.request.ChangeUserPasswordRequest;
|
||||
import org.booklore.model.dto.request.UpdateUserSettingRequest;
|
||||
import org.booklore.model.dto.request.UserProfileUpdateRequest;
|
||||
import org.booklore.model.dto.request.UserUpdateRequest;
|
||||
import org.booklore.model.dto.settings.UserSettingKey;
|
||||
import org.booklore.model.entity.BookLoreUserEntity;
|
||||
@@ -30,6 +31,18 @@ import java.util.Set;
|
||||
@Service
|
||||
@RequiredArgsConstructor
|
||||
public class UserService {
|
||||
private static final Set<String> SUPPORTED_LOCALES = Set.of(
|
||||
"en", "es", "it", "de", "fr", "nl", "pl", "pt", "ru", "hr",
|
||||
"sv", "zh", "ja", "hu", "sl", "sk", "uk", "id", "da"
|
||||
);
|
||||
private static final Set<String> SUPPORTED_THEMES = Set.of(
|
||||
"grimmory", "cobalt", "ember", "crimson", "rose", "forest",
|
||||
"meadow", "teal", "lagoon", "violet", "fuchsia", "slate", "custom"
|
||||
);
|
||||
private static final Set<String> SUPPORTED_THEME_ACCENTS = Set.of(
|
||||
"red", "orange", "amber", "yellow", "lime", "green", "emerald", "teal",
|
||||
"cyan", "sky", "blue", "indigo", "violet", "purple", "fuchsia", "pink", "rose"
|
||||
);
|
||||
|
||||
private final UserRepository userRepository;
|
||||
private final LibraryRepository libraryRepository;
|
||||
@@ -69,6 +82,39 @@ public class UserService {
|
||||
return bookLoreUserTransformer.toDTO(user);
|
||||
}
|
||||
|
||||
@Transactional
|
||||
public BookLoreUser updateUserProfile(Long id, UserProfileUpdateRequest updateRequest) {
|
||||
BookLoreUserEntity user = userRepository.findByIdWithDetails(id).orElseThrow(() -> ApiError.USER_NOT_FOUND.createException(id));
|
||||
BookLoreUser currentUser = getMyself();
|
||||
boolean isAdmin = currentUser.getPermissions().isAdmin();
|
||||
|
||||
if (!isAdmin && !currentUser.getId().equals(id)) {
|
||||
throw ApiError.GENERIC_UNAUTHORIZED.createException("You do not have permission to update this User");
|
||||
}
|
||||
|
||||
if (updateRequest.getName() != null) {
|
||||
user.setName(updateRequest.getName());
|
||||
}
|
||||
|
||||
if (updateRequest.getEmail() != null) {
|
||||
user.setEmail(updateRequest.getEmail());
|
||||
}
|
||||
|
||||
if (updateRequest.getLocale() != null) {
|
||||
validateLocale(updateRequest.getLocale());
|
||||
user.setLocale(updateRequest.getLocale());
|
||||
}
|
||||
|
||||
applyThemePreferences(user, updateRequest.getTheme(), updateRequest.getThemeAccent());
|
||||
if (updateRequest.getThemeSyncEnabled() != null) {
|
||||
user.setThemeSyncEnabled(updateRequest.getThemeSyncEnabled());
|
||||
}
|
||||
|
||||
userRepository.save(user);
|
||||
auditService.log(AuditAction.USER_UPDATED, "User", id, "Updated user profile: " + user.getUsername());
|
||||
return bookLoreUserTransformer.toDTO(user);
|
||||
}
|
||||
|
||||
@Transactional
|
||||
public void deleteUser(Long id) {
|
||||
BookLoreUserEntity userToDelete = userRepository.findById(id).orElseThrow(() -> ApiError.USER_NOT_FOUND.createException(id));
|
||||
@@ -181,4 +227,45 @@ public class UserService {
|
||||
private boolean meetsMinimumPasswordRequirements(String password) {
|
||||
return password != null && password.length() >= 8;
|
||||
}
|
||||
|
||||
private void validateLocale(String locale) {
|
||||
if (!SUPPORTED_LOCALES.contains(locale)) {
|
||||
throw ApiError.INVALID_INPUT.createException("Unsupported locale: " + locale);
|
||||
}
|
||||
}
|
||||
|
||||
private void validateTheme(String theme) {
|
||||
if (!SUPPORTED_THEMES.contains(theme)) {
|
||||
throw ApiError.INVALID_INPUT.createException("Unsupported theme: " + theme);
|
||||
}
|
||||
}
|
||||
|
||||
private void applyThemePreferences(BookLoreUserEntity user, String theme, String themeAccent) {
|
||||
String nextTheme = theme != null ? theme : user.getTheme();
|
||||
validateTheme(nextTheme);
|
||||
|
||||
if (!"custom".equals(nextTheme)) {
|
||||
if (themeAccent != null) {
|
||||
throw ApiError.INVALID_INPUT.createException("Theme accent can only be set when theme is custom.");
|
||||
}
|
||||
user.setTheme(nextTheme);
|
||||
user.setThemeAccent(null);
|
||||
return;
|
||||
}
|
||||
|
||||
String nextThemeAccent = themeAccent != null ? themeAccent : user.getThemeAccent();
|
||||
if (nextThemeAccent == null) {
|
||||
throw ApiError.INVALID_INPUT.createException("Theme accent is required when theme is custom.");
|
||||
}
|
||||
|
||||
validateThemeAccent(nextThemeAccent);
|
||||
user.setTheme(nextTheme);
|
||||
user.setThemeAccent(nextThemeAccent);
|
||||
}
|
||||
|
||||
private void validateThemeAccent(String themeAccent) {
|
||||
if (!SUPPORTED_THEME_ACCENTS.contains(themeAccent)) {
|
||||
throw ApiError.INVALID_INPUT.createException("Unsupported theme accent: " + themeAccent);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user