import { Component , OnInit } from "@angular/core" ; import { SessionService } from "../../session/session-service/session.service" ; import { WebdavModule } from "../../webdav/webdav.module" ; import { UserAccountService } from "./user-account.service" ; import { FormBuilder , ValidationErrors , Validators } from "@angular/forms" ; import { AppConfig } from "../../app-config/app-config" ; import { LoggingService } from "../../logging/logging.service" ; import { SessionType } from "../../session/session-type" ; /**
 * User account form to allow the user to view and edit information.
 */ @ Component ( { selector : "app-user-account" , templateUrl : "./user-account.component.html" , styleUrls : [ "./user-account.component.scss" ] , } ) export class UserAccountComponent implements OnInit { /** user to be edited */ username : string ; /** whether webdav integration is configured and the cloud settings section should be displayed */ webdavEnabled = WebdavModule . isEnabled ; /** whether password change is disallowed because of demo mode */ disabledForDemoMode : boolean ; disabledForOfflineMode : boolean ; passwordChangeResult : { success : boolean ; error ? : any ; } ; passwordForm = this . fb . group ( { currentPassword : [ "" , Validators . required ] , newPassword : [ "" , [ Validators . required , Validators . minLength ( 8 ) , Validators . pattern ( /[A-Z]/ ) , Validators . pattern ( /[a-z]/ ) , Validators . pattern ( /[0-9]/ ) , Validators . pattern ( /[^A-Za-z0-9]/ ) , ] , ] , confirmPassword : [ "" , [ Validators . required ] ] , } , { validators : ( ) => this . passwordMatchValidator ( ) } ) ; constructor ( private sessionService , private userAccountService , private fb , private loggingService ) { } ngOnInit ( ) { this . checkIfPasswordChangeAllowed ( ) ; this . username = this . sessionService . getCurrentUser ( ) ?. name ; } checkIfPasswordChangeAllowed ( ) { this . disabledForDemoMode = false ; this . disabledForOfflineMode = false ; this . passwordForm . enable ( ) ; if ( AppConfig . settings . session_type !== SessionType . synced ) { this . disabledForDemoMode = true ; this . passwordForm . disable ( ) ; } else if ( ! navigator . onLine ) { this . disabledForOfflineMode = true ; this . passwordForm . disable ( ) ; } } changePassword ( ) { this . passwordChangeResult = undefined ; const currentPassword = this . passwordForm . get ( "currentPassword" ) . value ; if ( ! this . sessionService . checkPassword ( this . username , currentPassword ) ) { this . passwordForm . get ( "currentPassword" ) . setErrors ( { incorrectPassword : true } ) ; return ; } const newPassword = this . passwordForm . get ( "newPassword" ) . value ; this . userAccountService . changePassword ( this . username , currentPassword , newPassword ) . then ( ( ) => this . sessionService . login ( this . username , newPassword ) ) . then ( ( ) => ( this . passwordChangeResult = { success : true } ) ) . catch ( ( err ) => { this . passwordChangeResult = { success : false , error : err . message } ; this . loggingService . error ( { error : "password change failed" , details : err . message , } ) ; throw err ; } ) ; } private passwordMatchValidator ( ) { const newPassword = this ?. passwordForm ?. get ( "newPassword" ) ?. value ; const confirmPassword = this ?. passwordForm ?. get ( "confirmPassword" ) ?. value ; if ( newPassword !== confirmPassword ) { this . passwordForm . get ( "confirmPassword" ) . setErrors ( { passwordConfirmationMismatch : true } ) ; return { passwordConfirmationMismatch : true } ; } return null ; } }