From d5f652f8fa5466d45a13433eee0897a9c24c64c8 Mon Sep 17 00:00:00 2001 From: Alex <25013571+alexhb1@users.noreply.github.com> Date: Sat, 13 Jun 2026 17:49:06 +0200 Subject: [PATCH] fix(ui): use signals for OIDC settings (#1737) --- .../authentication-settings.component.html | 120 +++++++++--------- .../authentication-settings.component.spec.ts | 51 +++----- .../authentication-settings.component.ts | 79 ++++++------ 3 files changed, 122 insertions(+), 128 deletions(-) diff --git a/frontend/src/app/core/security/oauth2-management/authentication-settings.component.html b/frontend/src/app/core/security/oauth2-management/authentication-settings.component.html index ce20da9e8..e31cf5f6c 100644 --- a/frontend/src/app/core/security/oauth2-management/authentication-settings.component.html +++ b/frontend/src/app/core/security/oauth2-management/authentication-settings.component.html @@ -537,7 +537,8 @@ - @if (groupMappings.length > 0) { - + @if (groupMappings().length > 0) { + {{ t('groupMapping.colGroupClaim') }} @@ -602,72 +603,77 @@ -
-
- - -
-
- - -
-
-
-
- {{ t('groupMapping.adminLabel') }} -
- + @if (groupMappingDraft(); as draft) { +
+
+ +
-
-
-

{{ t('groupMapping.permissionsLabel') }}

-
-
- - -
- - @for (perm of editingGroupMappingPerms; track perm) { -
- - +
+ + +
+
+
+
+ {{ t('groupMapping.adminLabel') }}
- } - + +
+
+
+

{{ t('groupMapping.permissionsLabel') }}

+
+
+ + +
+ + @for (perm of availablePermissions; track perm.value) { +
+ + +
+ } +
+
+
+
+

{{ t('groupMapping.librariesLabel') }}

+ +
-
-

{{ t('groupMapping.librariesLabel') }}

- - -
-
+ } + [disabled]="!groupMappingDraft()?.oidcGroupClaim"> diff --git a/frontend/src/app/core/security/oauth2-management/authentication-settings.component.spec.ts b/frontend/src/app/core/security/oauth2-management/authentication-settings.component.spec.ts index 71b64203e..180bbaf79 100644 --- a/frontend/src/app/core/security/oauth2-management/authentication-settings.component.spec.ts +++ b/frontend/src/app/core/security/oauth2-management/authentication-settings.component.spec.ts @@ -167,7 +167,7 @@ describe('AuthenticationSettingsComponent', () => { expect(component.availablePermissions.find(p => p.value === 'permissionDeleteBook')?.selected).toBe(false); expect(component.editingLibraryIds).toEqual([1, 2]); expect(component.sessionDurationHours).toBe(12); - expect(component.groupSyncMode).toBe('ON_LOGIN'); + expect(component.groupSyncMode()).toBe('ON_LOGIN'); expect(component.oidcForceOnlyMode).toBe(true); expect(component.oidcProvider.providerName).toBe('Example IdP'); expect(component.oidcProvider.claimMapping).toEqual({ @@ -177,7 +177,7 @@ describe('AuthenticationSettingsComponent', () => { groups: 'memberOf', }); expect(component.mobileRedirectUris).toEqual(['grimmory://oauth2-callback']); - expect(component.groupMappings).toEqual(mappings); + expect(component.groupMappings()).toEqual(mappings); expect(groupMappingService.getAll).toHaveBeenCalledOnce(); }); @@ -191,31 +191,23 @@ describe('AuthenticationSettingsComponent', () => { }); it('opens a new group mapping dialog with blank state', () => { - component.editingGroupMapping = { + component.groupMappingDraft.set({ oidcGroupClaim: 'existing', isAdmin: true, permissions: ['permissionUpload'], libraryIds: [1], description: 'Existing', - }; - component.editingGroupMappingPerms = component.availablePermissions.map(permission => ({ - ...permission, - selected: true, - })); - component.editingGroupMappingLibraryIds = [1]; + }); component.openNewGroupMapping(); - expect(component.showGroupMappingDialog).toBe(true); - expect(component.editingGroupMapping).toEqual({ + expect(component.groupMappingDraft()).toEqual({ oidcGroupClaim: '', isAdmin: false, permissions: [], libraryIds: [], description: '', }); - expect(component.editingGroupMappingPerms.every(permission => !permission.selected)).toBe(true); - expect(component.editingGroupMappingLibraryIds).toEqual([]); }); it('opens an existing group mapping dialog with cloned state and selected permissions', () => { @@ -230,12 +222,11 @@ describe('AuthenticationSettingsComponent', () => { component.openEditGroupMapping(mapping); - expect(component.showGroupMappingDialog).toBe(true); - expect(component.editingGroupMapping).toEqual(mapping); - expect(component.editingGroupMapping).not.toBe(mapping); - expect(component.editingGroupMappingPerms.find(permission => permission.value === 'permissionUpload')?.selected).toBe(true); - expect(component.editingGroupMappingPerms.find(permission => permission.value === 'permissionDeleteBook')?.selected).toBe(false); - expect(component.editingGroupMappingLibraryIds).toEqual([1, 2]); + const draft = component.groupMappingDraft(); + expect(draft).toEqual({...mapping, permissions: ['permissionUpload']}); + expect(draft).not.toBe(mapping); + expect(draft?.permissions).not.toBe(mapping.permissions); + expect(draft?.libraryIds).not.toBe(mapping.libraryIds); }); it('toggles OIDC enablement and reports persistence failures', () => { @@ -371,7 +362,7 @@ describe('AuthenticationSettingsComponent', () => { it('saves the group sync mode and reports failures', () => { appSettingsService.saveSettings.mockReturnValueOnce(of(void 0)); - component.groupSyncMode = 'ON_LOGIN_ADDITIVE'; + component.groupSyncMode.set('ON_LOGIN_ADDITIVE'); component.saveGroupSyncMode(); @@ -428,12 +419,13 @@ describe('AuthenticationSettingsComponent', () => { groupMappingService.update.mockReturnValue(of(createdMapping)); groupMappingService.delete.mockReturnValue(of(void 0)); groupMappingService.getAll.mockReturnValue(of([createdMapping])); - component.editingGroupMapping = {oidcGroupClaim: 'readers', isAdmin: false, permissions: [], libraryIds: [], description: ''}; - component.editingGroupMappingPerms = component.availablePermissions.map(permission => ({ - ...permission, - selected: permission.value === 'permissionUpload', - })); - component.editingGroupMappingLibraryIds = [1]; + component.groupMappingDraft.set({ + oidcGroupClaim: 'readers', + isAdmin: false, + permissions: ['permissionUpload'], + libraryIds: [1], + description: '', + }); component.saveGroupMapping(); @@ -445,12 +437,7 @@ describe('AuthenticationSettingsComponent', () => { description: '', }); - component.editingGroupMapping = createdMapping; - component.editingGroupMappingPerms = component.availablePermissions.map(permission => ({ - ...permission, - selected: false, - })); - component.editingGroupMappingLibraryIds = [2]; + component.groupMappingDraft.set({...createdMapping, permissions: [], libraryIds: [2]}); component.saveGroupMapping(); expect(groupMappingService.update).toHaveBeenCalledWith(9, { diff --git a/frontend/src/app/core/security/oauth2-management/authentication-settings.component.ts b/frontend/src/app/core/security/oauth2-management/authentication-settings.component.ts index 1be459fb7..9a50e2432 100644 --- a/frontend/src/app/core/security/oauth2-management/authentication-settings.component.ts +++ b/frontend/src/app/core/security/oauth2-management/authentication-settings.component.ts @@ -1,6 +1,6 @@ import {HttpErrorResponse} from '@angular/common/http'; -import {Component, effect, inject} from '@angular/core'; -import {FormsModule, ReactiveFormsModule} from '@angular/forms'; +import {Component, effect, inject, signal} from '@angular/core'; +import {FormsModule} from '@angular/forms'; import {InputText} from 'primeng/inputtext'; import {Button} from 'primeng/button'; import {Checkbox} from 'primeng/checkbox'; @@ -33,7 +33,6 @@ import {Chip} from 'primeng/chip'; Button, MultiSelect, InputNumber, - ReactiveFormsModule, ExternalDocLinkComponent, TranslocoDirective, TranslocoPipe, @@ -105,17 +104,14 @@ export class AuthenticationSettingsComponent { showTestDetails = false; // Group mapping - groupSyncMode: string = 'DISABLED'; + readonly groupSyncMode = signal('DISABLED'); groupSyncModeOptions = [ {label: 'Disabled', value: 'DISABLED'}, {label: 'On Login (Replace)', value: 'ON_LOGIN'}, {label: 'On Login (Additive)', value: 'ON_LOGIN_ADDITIVE'} ]; - groupMappings: OidcGroupMapping[] = []; - showGroupMappingDialog = false; - editingGroupMapping: OidcGroupMapping = this.emptyGroupMapping(); - editingGroupMappingPerms: {label: string; value: string; selected: boolean; translationKey: string}[] = []; - editingGroupMappingLibraryIds: number[] = []; + readonly groupMappings = signal([]); + readonly groupMappingDraft = signal(null); oidcProvider: OidcProviderDetails = { providerName: '', @@ -163,7 +159,7 @@ export class AuthenticationSettingsComponent { }; this.sessionDurationHours = settings.oidcSessionDurationHours ?? null; - this.groupSyncMode = settings.oidcGroupSyncMode ?? 'DISABLED'; + this.groupSyncMode.set(settings.oidcGroupSyncMode ?? 'DISABLED'); this.oidcForceOnlyMode = settings.oidcForceOnlyMode ?? false; this.mobileRedirectUris = settings.oidcRedirectUris?.length ? [...settings.oidcRedirectUris] @@ -340,14 +336,14 @@ export class AuthenticationSettingsComponent { // Group mapping methods loadGroupMappings(): void { - this.groupMappingService.getAll().subscribe(mappings => this.groupMappings = mappings); + this.groupMappingService.getAll().subscribe(mappings => this.groupMappings.set(mappings)); } saveGroupSyncMode(): void { const payload = [ { key: AppSettingKey.OIDC_GROUP_SYNC_MODE, - newValue: this.groupSyncMode + newValue: this.groupSyncMode() } ]; this.appSettingsService.saveSettings(payload).subscribe({ @@ -365,34 +361,48 @@ export class AuthenticationSettingsComponent { } openNewGroupMapping(): void { - this.editingGroupMapping = this.emptyGroupMapping(); - this.initGroupMappingPerms([]); - this.editingGroupMappingLibraryIds = []; - this.showGroupMappingDialog = true; + this.groupMappingDraft.set(this.emptyGroupMapping()); } openEditGroupMapping(mapping: OidcGroupMapping): void { - this.editingGroupMapping = {...mapping}; - this.initGroupMappingPerms(mapping.permissions); - this.editingGroupMappingLibraryIds = [...mapping.libraryIds]; - this.showGroupMappingDialog = true; + this.groupMappingDraft.set({ + ...mapping, + permissions: mapping.permissions.filter(permission => permission !== 'permissionRead'), + libraryIds: [...mapping.libraryIds], + }); } - private initGroupMappingPerms(selectedPerms: string[]): void { - this.editingGroupMappingPerms = this.availablePermissions.map(p => ({ - ...p, - selected: selectedPerms.includes(p.value) - })); + updateGroupMappingDraft(patch: Partial): void { + this.groupMappingDraft.update(draft => draft ? {...draft, ...patch} : draft); + } + + setGroupMappingAdmin(isAdmin: boolean): void { + this.updateGroupMappingDraft(isAdmin + ? {isAdmin, permissions: this.availablePermissions.map(p => p.value)} + : {isAdmin}); + } + + setGroupMappingPermission(permission: string, selected: boolean): void { + this.groupMappingDraft.update(draft => { + if (!draft) return draft; + + const permissions = selected + ? [...draft.permissions, permission] + : draft.permissions.filter(value => value !== permission); + return {...draft, permissions: [...new Set(permissions)]}; + }); } saveGroupMapping(): void { + const draft = this.groupMappingDraft(); + if (!draft) return; + const mapping: OidcGroupMapping = { - ...this.editingGroupMapping, + ...draft, permissions: [ 'permissionRead', - ...this.editingGroupMappingPerms.filter(p => p.selected).map(p => p.value) - ], - libraryIds: this.editingGroupMappingLibraryIds + ...draft.permissions + ] }; const obs = mapping.id @@ -401,7 +411,7 @@ export class AuthenticationSettingsComponent { obs.subscribe({ next: () => { - this.showGroupMappingDialog = false; + this.groupMappingDraft.set(null); this.loadGroupMappings(); this.messageService.add({ severity: 'success', @@ -484,15 +494,6 @@ export class AuthenticationSettingsComponent { }); } - onAdminCheckboxChange(): void { - if (this.editingGroupMapping.isAdmin) { - this.editingGroupMappingPerms = this.availablePermissions.map(p => ({ - ...p, - selected: true, - })); - } - } - private emptyGroupMapping(): OidcGroupMapping { return {oidcGroupClaim: '', isAdmin: false, permissions: [], libraryIds: [], description: ''}; }