From 6a8561080f57e9cf89cca924025f9afd50a67629 Mon Sep 17 00:00:00 2001 From: adityachandelgit <> Date: Sat, 17 May 2025 01:21:57 -0600 Subject: [PATCH] Enable editing existing admin users, creating new admins, and deleting other admin users --- .../security/AuthenticationService.java | 6 ++- .../booklore/exception/ApiError.java | 1 + .../booklore/model/dto/UserCreateRequest.java | 1 + .../service/user/UserProvisioningService.java | 29 ++++++++++++--- .../booklore/service/user/UserService.java | 20 ++++------ booklore-ui/src/app/app.routes.ts | 3 +- .../src/app/auth-interceptor.service.ts | 2 +- .../layout-topbar/app.topbar.component.html | 5 +-- .../create-user-dialog.component.html | 5 +++ .../create-user-dialog.component.ts | 3 +- .../user-management.component.html | 37 +++++++++++-------- .../user-management.component.ts | 17 +++++++++ 12 files changed, 88 insertions(+), 41 deletions(-) diff --git a/booklore-api/src/main/java/com/adityachandel/booklore/config/security/AuthenticationService.java b/booklore-api/src/main/java/com/adityachandel/booklore/config/security/AuthenticationService.java index c05c83b2c..0a8dcb7f8 100644 --- a/booklore-api/src/main/java/com/adityachandel/booklore/config/security/AuthenticationService.java +++ b/booklore-api/src/main/java/com/adityachandel/booklore/config/security/AuthenticationService.java @@ -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> loginUser(UserLoginRequest loginRequest) { diff --git a/booklore-api/src/main/java/com/adityachandel/booklore/exception/ApiError.java b/booklore-api/src/main/java/com/adityachandel/booklore/exception/ApiError.java index 799beb7ce..2ed100f1d 100644 --- a/booklore-api/src/main/java/com/adityachandel/booklore/exception/ApiError.java +++ b/booklore-api/src/main/java/com/adityachandel/booklore/exception/ApiError.java @@ -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; diff --git a/booklore-api/src/main/java/com/adityachandel/booklore/model/dto/UserCreateRequest.java b/booklore-api/src/main/java/com/adityachandel/booklore/model/dto/UserCreateRequest.java index da4b391df..2ee5f9afc 100644 --- a/booklore-api/src/main/java/com/adityachandel/booklore/model/dto/UserCreateRequest.java +++ b/booklore-api/src/main/java/com/adityachandel/booklore/model/dto/UserCreateRequest.java @@ -15,6 +15,7 @@ public class UserCreateRequest { private boolean permissionDownload; private boolean permissionEditMetadata; private boolean permissionEmailBook; + private boolean permissionAdmin; private Set selectedLibraries; } \ No newline at end of file diff --git a/booklore-api/src/main/java/com/adityachandel/booklore/service/user/UserProvisioningService.java b/booklore-api/src/main/java/com/adityachandel/booklore/service/user/UserProvisioningService.java index bc4068e76..003cf7574 100644 --- a/booklore-api/src/main/java/com/adityachandel/booklore/service/user/UserProvisioningService.java +++ b/booklore-api/src/main/java/com/adityachandel/booklore/service/user/UserProvisioningService.java @@ -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(); + } } \ No newline at end of file diff --git a/booklore-api/src/main/java/com/adityachandel/booklore/service/user/UserService.java b/booklore-api/src/main/java/com/adityachandel/booklore/service/user/UserService.java index 0f92116d0..7184ab7ff 100644 --- a/booklore-api/src/main/java/com/adityachandel/booklore/service/user/UserService.java +++ b/booklore-api/src/main/java/com/adityachandel/booklore/service/user/UserService.java @@ -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) { diff --git a/booklore-ui/src/app/app.routes.ts b/booklore-ui/src/app/app.routes.ts index 72396c97b..9b7218da4 100644 --- a/booklore-ui/src/app/app.routes.ts +++ b/booklore-ui/src/app/app.routes.ts @@ -59,8 +59,7 @@ export const routes: Routes = [ }, { path: 'change-password', - component: ChangePasswordComponent, - canActivate: [SetupGuard] + component: ChangePasswordComponent }, { path: '**', diff --git a/booklore-ui/src/app/auth-interceptor.service.ts b/booklore-ui/src/app/auth-interceptor.service.ts index abb5162f0..7308c423a 100644 --- a/booklore-ui/src/app/auth-interceptor.service.ts +++ b/booklore-ui/src/app/auth-interceptor.service.ts @@ -20,7 +20,7 @@ export const AuthInterceptorService: HttpInterceptorFn = (req, next: HttpHandler return next(authReq).pipe( catchError((error: HttpErrorResponse) => { - if (error.status === 401 || error.status === 403) { + if (error.status === 401) { return handle401Error(authService, authReq, next, router, !!internalToken); } return throwError(() => error); diff --git a/booklore-ui/src/app/layout/component/layout-topbar/app.topbar.component.html b/booklore-ui/src/app/layout/component/layout-topbar/app.topbar.component.html index 441b5650b..19c687df6 100644 --- a/booklore-ui/src/app/layout/component/layout-topbar/app.topbar.component.html +++ b/booklore-ui/src/app/layout/component/layout-topbar/app.topbar.component.html @@ -22,13 +22,13 @@ - diff --git a/booklore-ui/src/app/settings/user-management/create-user-dialog/create-user-dialog.component.html b/booklore-ui/src/app/settings/user-management/create-user-dialog/create-user-dialog.component.html index 736906a8b..7d3a1a226 100644 --- a/booklore-ui/src/app/settings/user-management/create-user-dialog/create-user-dialog.component.html +++ b/booklore-ui/src/app/settings/user-management/create-user-dialog/create-user-dialog.component.html @@ -70,6 +70,11 @@ + +
+ + +
diff --git a/booklore-ui/src/app/settings/user-management/create-user-dialog/create-user-dialog.component.ts b/booklore-ui/src/app/settings/user-management/create-user-dialog/create-user-dialog.component.ts index cb7f4da1a..bddc99649 100644 --- a/booklore-ui/src/app/settings/user-management/create-user-dialog/create-user-dialog.component.ts +++ b/booklore-ui/src/app/settings/user-management/create-user-dialog/create-user-dialog.component.ts @@ -48,7 +48,8 @@ export class CreateUserDialogComponent implements OnInit { permissionUpload: [false], permissionDownload: [false], permissionEditMetadata: [false], - permissionEmailBook: [false] + permissionEmailBook: [false], + permissionAdmin: [false], }); } diff --git a/booklore-ui/src/app/settings/user-management/user-management.component.html b/booklore-ui/src/app/settings/user-management/user-management.component.html index 37d5dec5d..0ca82d8f8 100644 --- a/booklore-ui/src/app/settings/user-management/user-management.component.html +++ b/booklore-ui/src/app/settings/user-management/user-management.component.html @@ -10,18 +10,19 @@ - Provisioning + Type Username Full Name Email Assigned Libraries - Can Upload - Can Download - Can Edit Metadata - Can Email Books + Is Admin + Can Upload + Can Download + Can Edit Metadata + Can Email Books Edit - Change Password - Delete + Change Password + Delete @@ -53,6 +54,10 @@ {{ user.libraryNames }} + + + + @@ -70,26 +75,26 @@ - + - + - + - - + + diff --git a/booklore-ui/src/app/settings/user-management/user-management.component.ts b/booklore-ui/src/app/settings/user-management/user-management.component.ts index 2ac9e5109..0f6e408f7 100644 --- a/booklore-ui/src/app/settings/user-management/user-management.component.ts +++ b/booklore-ui/src/app/settings/user-management/user-management.component.ts @@ -40,6 +40,7 @@ export class UserManagementComponent implements OnInit { private messageService = inject(MessageService); users: User[] = []; + currentUser: User | undefined; editingLibraryIds: number[] = []; allLibraries: Library[] = []; @@ -51,6 +52,7 @@ export class UserManagementComponent implements OnInit { ngOnInit() { this.loadUsers(); + this.loadMyself(); this.libraryService.getAllLibrariesFromAPI().subscribe({ next: (libraries) => { this.allLibraries = libraries; @@ -65,6 +67,21 @@ export class UserManagementComponent implements OnInit { }); } + loadMyself() { + this.userService.getMyself().subscribe({ + next: (user) => { + this.currentUser = user; + }, + error: () => { + this.messageService.add({ + severity: 'error', + summary: 'Error', + detail: 'Failed to fetch current user', + }); + } + }) + } + loadUsers() { this.userService.getUsers().subscribe({ next: (data) => {