diff --git a/service/src/main/inse/auth/service.py b/service/src/main/inse/auth/service.py index 3c24086e..0af15a4b 100644 --- a/service/src/main/inse/auth/service.py +++ b/service/src/main/inse/auth/service.py @@ -1071,15 +1071,31 @@ def change_password( account = _get_active_account(session, access_token.effective_account_id) if not _verify_password(current_password, account.password_hash): + logger.warning( + "Password change failed: current password is incorrect for account %s", + account.id, + ) raise HTTPException( status_code=status.HTTP_403_FORBIDDEN, detail="Current password is incorrect.", ) + if current_password == new_password: + logger.warning( + "Password change failed: new password is the same as the current password for account %s", + account.id, + ) + raise HTTPException( + status_code=status.HTTP_400_BAD_REQUEST, + detail="New password cannot be the same as the current password.", + ) + account.password_hash = _hash_password(new_password) account.epoch += 1 session.add(account) session.commit() + logger.info("Password changed successfully for account %s", account.id) + return {"status": "ok"} diff --git a/service/src/main/inse/keywords/logic.py b/service/src/main/inse/keywords/logic.py index 904b1e31..d6ae4c97 100644 --- a/service/src/main/inse/keywords/logic.py +++ b/service/src/main/inse/keywords/logic.py @@ -365,7 +365,7 @@ async def extract_keywords_stream_enriched( KeywordsWsEvent( type="status", step="enriching", - message=f"Bearbeite keyword'{keyword_text}'", + message=f"Extracting topic for keyword '{keyword_text}'", ).model_dump() ) task = asyncio.create_task(get_openalex_topic(level, idx, confirmed[level][idx].llm_keyword)) diff --git a/webapp/src/app/modules/account/account.component.html b/webapp/src/app/modules/account/account.component.html index 7590acb3..da753236 100644 --- a/webapp/src/app/modules/account/account.component.html +++ b/webapp/src/app/modules/account/account.component.html @@ -16,5 +16,58 @@ - + + + Passwort ändern + + + Altes Passwort + + @if (oldPasswordErrorMessage()) { + {{ oldPasswordErrorMessage() }} + } + + + + + Neues Passwort + + @if (newPasswordErrorMessage()) { + {{ newPasswordErrorMessage() }} + } + + + + Neues Passwort wiederholen + + @if (newPasswordRepeatErrorMessage()) { + {{ newPasswordRepeatErrorMessage() }} + } + + + + Passwörter anzeigen + + Passwort ändern + + + + + diff --git a/webapp/src/app/modules/account/account.component.scss b/webapp/src/app/modules/account/account.component.scss index 25c46861..ff03e82e 100644 --- a/webapp/src/app/modules/account/account.component.scss +++ b/webapp/src/app/modules/account/account.component.scss @@ -42,3 +42,25 @@ font-weight: 600; color: var(--sg-text-muted); } + +.settings-tab { + padding: 1rem; + max-width: 50%; + display: flex; + flex-direction: column; + gap: 1rem; +} + +.change-password-form { + display: flex; + flex-direction: column; + gap: 1.5rem; +} + +.new-password-fields { + display: flex; + gap: 1rem; + > mat-form-field { + width: 100%; + } +} diff --git a/webapp/src/app/modules/account/account.component.ts b/webapp/src/app/modules/account/account.component.ts index 7a953dee..8b3eec70 100644 --- a/webapp/src/app/modules/account/account.component.ts +++ b/webapp/src/app/modules/account/account.component.ts @@ -1,21 +1,171 @@ -import { Component, effect, input } from '@angular/core'; +import { + Component, + computed, + effect, + inject, + input, + signal, +} from '@angular/core'; import { AccountProfile, UserRole } from '../../api'; import { RolePipe } from '../../shared/pipes/role.pipe'; import { MatChipsModule } from '@angular/material/chips'; import { MatTabsModule } from '@angular/material/tabs'; import { MatDividerModule } from '@angular/material/divider'; +import { MatInputModule } from '@angular/material/input'; +import { FormsModule, ReactiveFormsModule } from '@angular/forms'; +import { + form, + FormField, + maxLength, + minLength, + required, + validate, + FormRoot, +} from '@angular/forms/signals'; +import { MatFormFieldModule } from '@angular/material/form-field'; +import { MatIconModule } from '@angular/material/icon'; +import { MatButtonModule } from '@angular/material/button'; +import { MatSlideToggleModule } from '@angular/material/slide-toggle'; +import { CredentialService } from '../../shared/services/credential.service'; +import { MatSnackBar } from '@angular/material/snack-bar'; + +interface ChangePasswordForm { + oldPassword: string; + newPassword: string; + newPasswordRepeat: string; + showPasswords: boolean; +} @Component({ selector: 'app-account', - imports: [RolePipe, MatChipsModule, MatTabsModule, MatDividerModule], + imports: [ + RolePipe, + MatChipsModule, + MatTabsModule, + MatDividerModule, + MatInputModule, + FormsModule, + ReactiveFormsModule, + MatFormFieldModule, + MatDividerModule, + MatIconModule, + MatButtonModule, + MatSlideToggleModule, + FormField, + FormRoot, + ], templateUrl: './account.component.html', styleUrl: './account.component.scss', }) export class AccountComponent { readonly accountProfile = input.required(); + private readonly _credentialService = inject(CredentialService); + private readonly _snackBar = inject(MatSnackBar); + roles: UserRole[] = []; + changePasswordModel = signal({ + oldPassword: '', + newPassword: '', + newPasswordRepeat: '', + showPasswords: false, + }); + + oldPasswordErrorMessage = computed(() => { + const errors = this.changePasswordForm.oldPassword().errors(); + return errors.length > 0 && errors[0].message ? errors[0].message : null; + }); + + newPasswordErrorMessage = computed(() => { + const errors = this.changePasswordForm.newPassword().errors(); + return errors.length > 0 && errors[0].message ? errors[0].message : null; + }); + + newPasswordRepeatErrorMessage = computed(() => { + const errors = this.changePasswordForm.newPasswordRepeat().errors(); + return errors.length > 0 && errors[0].message ? errors[0].message : null; + }); + + changePasswordForm = form( + this.changePasswordModel, + (schemaPath) => { + required(schemaPath.oldPassword, { + message: 'Altes Passwort ist erforderlich.', + }); + required(schemaPath.newPassword, { + message: 'Neues Passwort ist erforderlich.', + }); + required(schemaPath.newPasswordRepeat, { + message: 'Neues Passwort wiederholen ist erforderlich.', + }); + + minLength(schemaPath.newPassword, 8, { + message: 'Neues Passwort muss mindestens 8 Zeichen lang sein.', + }); + maxLength(schemaPath.newPassword, 64, { + message: 'Neues Passwort darf maximal 64 Zeichen lang sein.', + }); + + validate(schemaPath.newPasswordRepeat, ({ value, valueOf }) => { + const newPassword = valueOf(schemaPath.newPassword).trim(); + const newPasswordRepeat = value().trim(); + + if (newPassword !== newPasswordRepeat) { + return { + kind: 'passwordMismatch', + message: 'Passwörter stimmen nicht überein.', + }; + } + + return null; + }); + + validate(schemaPath.newPassword, ({ value }) => { + const oldPassword = this.changePasswordModel().oldPassword.trim(); + const newPassword = value().trim(); + + if (oldPassword && newPassword && oldPassword === newPassword) { + return { + kind: 'passwordSameAsOld', + message: + 'Neues Passwort darf nicht dasselbe wie das alte Passwort sein.', + }; + } + + return null; + }); + }, + { + submission: { + action: async (field) => { + const oldPassword = field.oldPassword().value().trim(); + const newPassword = field.newPassword().value().trim(); + + if (!oldPassword || !newPassword) { + return; + } + this._credentialService + .changePassword(oldPassword, newPassword) + .subscribe({ + error: (error) => { + console.error('Error changing password:', error); + this._snackBar.open( + 'Das alte Passwort ist nicht korrekt. Bitte versuchen Sie es erneut.', + 'OK', + { duration: 7000 }, + ); + }, + }); + }, + }, + }, + ); + + inputFieldType = computed(() => + this.changePasswordModel().showPasswords ? 'text' : 'password', + ); + constructor() { effect(() => { this.roles = [this.accountProfile().role]; diff --git a/webapp/src/app/modules/keywords-search/keywords-search.component.ts b/webapp/src/app/modules/keywords-search/keywords-search.component.ts index cbe0cce0..03a02061 100644 --- a/webapp/src/app/modules/keywords-search/keywords-search.component.ts +++ b/webapp/src/app/modules/keywords-search/keywords-search.component.ts @@ -651,18 +651,30 @@ export class KeywordsSearchComponent { const manualKeywordSet = new Set( this.manualKeywords().map((k) => this.normalizeKeyword(k)), ); - const validSelected = selected.filter((k) => - manualKeywordSet.has(this.normalizeKeyword(k)), + const broadKeywordSet = new Set( + this.broadKeywords().map((k) => this.normalizeKeyword(k)), + ); + const coarseKeywordSet = new Set( + this.coarseKeywords().map((k) => this.normalizeKeyword(k)), + ); + const fineKeywordSet = new Set( + this.fineKeywords().map((k) => this.normalizeKeyword(k)), + ); + const validSelected = selected.filter( + (k) => + manualKeywordSet.has(this.normalizeKeyword(k)) || + broadKeywordSet.has(this.normalizeKeyword(k)) || + coarseKeywordSet.has(this.normalizeKeyword(k)) || + fineKeywordSet.has(this.normalizeKeyword(k)), ); const selectionWasTrimmed = validSelected.length !== selected.length; this.selectedKeywords.set(this.uniqueKeywords(validSelected)); - this.isRestoringState = false; - if (selectionWasTrimmed) { this.updateQueryParams(); } + this.isRestoringState = false; } updateQueryParams(): void { diff --git a/webapp/src/app/shared/services/credential.service.ts b/webapp/src/app/shared/services/credential.service.ts index 8e72953f..f276073c 100644 --- a/webapp/src/app/shared/services/credential.service.ts +++ b/webapp/src/app/shared/services/credential.service.ts @@ -18,6 +18,7 @@ import { AccountProfile, } from '../../api'; import { HttpClient, HttpHeaders } from '@angular/common/http'; +import { Router } from '@angular/router'; type LogoutResult = 'logged-out' | 'stopped-impersonating'; type UnauthorizedRecoveryResult = @@ -66,7 +67,7 @@ export class CredentialService { private request = 0; - constructor() { + constructor(private readonly router: Router) { const data = localStorage.getItem(CredentialService.KEY_GRANT); const id = data ? (JSON.parse(data) as AccessToken) : undefined; if (id && new Date(id.expires_at).getTime() >= Date.now()) { @@ -212,6 +213,7 @@ export class CredentialService { this.login.next(''); this.email.next(''); this.organizationName.next(''); + this.router.navigate(['/login'], { replaceUrl: true }); } public handleUnauthorized(): Observable { @@ -260,8 +262,13 @@ export class CredentialService { { withCredentials: true }, ) .pipe( - tap(() => this.clearLocalAuthState()), + tap(() => { + this.clearLocalAuthState(); + }), map(() => undefined), + catchError((err) => { + throw err; + }), ); }