Enable editing existing admin users, creating new admins, and deleting other admin users
This commit is contained in:
committed by
Aditya Chandel
parent
099eecbde5
commit
6a8561080f
+5
-1
@@ -33,7 +33,11 @@ public class AuthenticationService {
|
||||
|
||||
public BookLoreUser getAuthenticatedUser() {
|
||||
Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
|
||||
return (BookLoreUser) authentication.getPrincipal();
|
||||
Object principal = authentication.getPrincipal();
|
||||
if (principal instanceof BookLoreUser) {
|
||||
return (BookLoreUser) principal;
|
||||
}
|
||||
throw new IllegalStateException("Authenticated principal is not of type BookLoreUser");
|
||||
}
|
||||
|
||||
public ResponseEntity<Map<String, String>> loginUser(UserLoginRequest loginRequest) {
|
||||
|
||||
@@ -41,6 +41,7 @@ public enum ApiError {
|
||||
PASSWORD_SAME_AS_CURRENT(HttpStatus.BAD_REQUEST, "New password cannot be the same as the current password"),
|
||||
INVALID_CREDENTIALS(HttpStatus.BAD_REQUEST, "Invalid credentials"),
|
||||
REMOTE_AUTH_DISABLED(HttpStatus.NON_AUTHORITATIVE_INFORMATION, "Remote login is disabled"),
|
||||
SELF_DELETION_NOT_ALLOWED(HttpStatus.FORBIDDEN, "You cannot delete your own account"),
|
||||
INVALID_INPUT(HttpStatus.BAD_REQUEST, "%s");
|
||||
|
||||
private final HttpStatus status;
|
||||
|
||||
@@ -15,6 +15,7 @@ public class UserCreateRequest {
|
||||
private boolean permissionDownload;
|
||||
private boolean permissionEditMetadata;
|
||||
private boolean permissionEmailBook;
|
||||
private boolean permissionAdmin;
|
||||
|
||||
private Set<Long> selectedLibraries;
|
||||
}
|
||||
+24
-5
@@ -6,6 +6,8 @@ import com.adityachandel.booklore.model.dto.BookLoreUser;
|
||||
import com.adityachandel.booklore.model.dto.UserCreateRequest;
|
||||
import com.adityachandel.booklore.model.dto.request.InitialUserRequest;
|
||||
import com.adityachandel.booklore.model.dto.settings.OidcAutoProvisionDetails;
|
||||
import com.adityachandel.booklore.model.dto.settings.SidebarSortOption;
|
||||
import com.adityachandel.booklore.model.dto.settings.UserSettingKey;
|
||||
import com.adityachandel.booklore.model.entity.*;
|
||||
import com.adityachandel.booklore.model.enums.ProvisioningMethod;
|
||||
import com.adityachandel.booklore.repository.LibraryRepository;
|
||||
@@ -81,6 +83,7 @@ public class UserProvisioningService {
|
||||
permissions.setPermissionDownload(request.isPermissionDownload());
|
||||
permissions.setPermissionEditMetadata(request.isPermissionEditMetadata());
|
||||
permissions.setPermissionEmailBook(request.isPermissionEmailBook());
|
||||
permissions.setPermissionAdmin(request.isPermissionAdmin());
|
||||
user.setPermissions(permissions);
|
||||
|
||||
if (request.getSelectedLibraries() != null && !request.getSelectedLibraries().isEmpty()) {
|
||||
@@ -173,19 +176,21 @@ public class UserProvisioningService {
|
||||
shelfRepository.save(shelfEntity);
|
||||
}
|
||||
|
||||
addUserSetting(user, "bookPreferences.perBookSetting", buildDefaultPerBookSetting());
|
||||
addUserSetting(user, "bookPreferences.pdfReaderSetting", buildDefaultPdfReaderSetting());
|
||||
addUserSetting(user, "bookPreferences.epubReaderSetting", buildDefaultEpubReaderSetting());
|
||||
addUserSetting(user, UserSettingKey.PER_BOOK_SETTING, buildDefaultPerBookSetting());
|
||||
addUserSetting(user, UserSettingKey.PDF_READER_SETTING, buildDefaultPdfReaderSetting());
|
||||
addUserSetting(user, UserSettingKey.EPUB_READER_SETTING, buildDefaultEpubReaderSetting());
|
||||
addUserSetting(user, UserSettingKey.SIDEBAR_LIBRARY_SORTING, buildDefaultSidebarLibrarySorting());
|
||||
addUserSetting(user, UserSettingKey.SIDEBAR_SHELF_SORTING, buildDefaultSidebarShelfSorting());
|
||||
|
||||
return user;
|
||||
}
|
||||
|
||||
private void addUserSetting(BookLoreUserEntity user, String key, Object value) {
|
||||
private void addUserSetting(BookLoreUserEntity user, UserSettingKey key, Object value) {
|
||||
try {
|
||||
String json = objectMapper.writeValueAsString(value);
|
||||
UserSettingEntity setting = UserSettingEntity.builder()
|
||||
.user(user)
|
||||
.settingKey(key)
|
||||
.settingKey(key.toString())
|
||||
.settingValue(json)
|
||||
.build();
|
||||
user.getSettings().add(setting);
|
||||
@@ -215,4 +220,18 @@ public class UserProvisioningService {
|
||||
.fontSize(150)
|
||||
.build();
|
||||
}
|
||||
|
||||
private Object buildDefaultSidebarLibrarySorting() {
|
||||
return SidebarSortOption.builder()
|
||||
.field("id")
|
||||
.order("asc")
|
||||
.build();
|
||||
}
|
||||
|
||||
private Object buildDefaultSidebarShelfSorting() {
|
||||
return SidebarSortOption.builder()
|
||||
.field("id")
|
||||
.order("asc")
|
||||
.build();
|
||||
}
|
||||
}
|
||||
+8
-12
@@ -67,20 +67,16 @@ public class UserService {
|
||||
}
|
||||
|
||||
public void deleteUser(Long id) {
|
||||
BookLoreUserEntity user = userRepository.findById(id).orElseThrow(() -> ApiError.USER_NOT_FOUND.createException(id));
|
||||
|
||||
if (user.getPermissions().isPermissionAdmin()) {
|
||||
throw ApiError.CANNOT_DELETE_ADMIN.createException();
|
||||
BookLoreUserEntity userToDelete = userRepository.findById(id).orElseThrow(() -> ApiError.USER_NOT_FOUND.createException(id));
|
||||
BookLoreUser currentUser = authenticationService.getAuthenticatedUser();
|
||||
boolean isAdmin = currentUser.getPermissions().isAdmin();
|
||||
if (!isAdmin) {
|
||||
throw ApiError.UNAUTHORIZED.createException("You do not have permission to delete this User");
|
||||
}
|
||||
|
||||
Object principal = SecurityContextHolder.getContext().getAuthentication().getPrincipal();
|
||||
if (principal instanceof UserDetails userDetails) {
|
||||
if (userDetails.getAuthorities().stream().noneMatch(auth -> auth.getAuthority().equals("ROLE_ADMIN"))) {
|
||||
throw ApiError.UNAUTHORIZED.createException();
|
||||
}
|
||||
if (currentUser.getId().equals(userToDelete.getId())) {
|
||||
throw ApiError.SELF_DELETION_NOT_ALLOWED.createException();
|
||||
}
|
||||
|
||||
userRepository.delete(user);
|
||||
userRepository.delete(userToDelete);
|
||||
}
|
||||
|
||||
public BookLoreUser getBookLoreUser(Long id) {
|
||||
|
||||
Reference in New Issue
Block a user