juice-shop
85 строк · 3.4 Кб
1/*
2* Copyright (c) 2014-2024 Bjoern Kimminich & the OWASP Juice Shop contributors.
3* SPDX-License-Identifier: MIT
4*/
5
6import { type AbstractControl, UntypedFormControl, Validators } from '@angular/forms'
7import { UserService } from '../Services/user.service'
8import { Component } from '@angular/core'
9import { library } from '@fortawesome/fontawesome-svg-core'
10import { faSave } from '@fortawesome/free-solid-svg-icons'
11import { faEdit } from '@fortawesome/free-regular-svg-icons'
12import { FormSubmitService } from '../Services/form-submit.service'
13import { TranslateService } from '@ngx-translate/core'
14
15library.add(faSave, faEdit)
16
17@Component({
18selector: 'app-change-password',
19templateUrl: './change-password.component.html',
20styleUrls: ['./change-password.component.scss']
21})
22export class ChangePasswordComponent {
23public passwordControl: UntypedFormControl = new UntypedFormControl('', [Validators.required])
24public newPasswordControl: UntypedFormControl = new UntypedFormControl('', [Validators.required, Validators.minLength(5), Validators.maxLength(40)])
25public repeatNewPasswordControl: UntypedFormControl = new UntypedFormControl('', [Validators.required, Validators.minLength(5), Validators.maxLength(40), matchValidator(this.newPasswordControl)])
26public error: any
27public confirmation: any
28
29constructor (private readonly userService: UserService, private readonly formSubmitService: FormSubmitService, private readonly translate: TranslateService) { }
30
31ngOnInit () {
32this.formSubmitService.attachEnterKeyHandler('password-form', 'changeButton', () => { this.changePassword() })
33}
34
35changePassword () {
36if (localStorage.getItem('email')?.match(/support@.*/) && !this.newPasswordControl.value.match(/(?=.*[a-z])(?=.*[A-Z])(?=.*\d)(?=.*[@$!%*?&])[A-Za-z\d@$!%*?&]{12,30}/)) {
37console.error('Parola echipei de asistență nu respectă politica corporativă pentru conturile privilegiate! Vă rugăm să schimbați parola în consecință!')
38}
39this.userService.changePassword({
40current: this.passwordControl.value,
41new: this.newPasswordControl.value,
42repeat: this.repeatNewPasswordControl.value
43}).subscribe((response: any) => {
44this.error = undefined
45this.translate.get('PASSWORD_SUCCESSFULLY_CHANGED').subscribe((passwordSuccessfullyChanged) => {
46this.confirmation = passwordSuccessfullyChanged
47}, (translationId) => {
48this.confirmation = { error: translationId }
49})
50this.resetForm()
51}, (error) => {
52console.log(error)
53this.error = error
54this.confirmation = undefined
55this.resetPasswords()
56})
57}
58
59resetForm () {
60this.passwordControl.setValue('')
61this.resetPasswords()
62}
63
64resetPasswords () {
65this.passwordControl.markAsPristine()
66this.passwordControl.markAsUntouched()
67this.newPasswordControl.setValue('')
68this.newPasswordControl.markAsPristine()
69this.newPasswordControl.markAsUntouched()
70this.repeatNewPasswordControl.setValue('')
71this.repeatNewPasswordControl.markAsPristine()
72this.repeatNewPasswordControl.markAsUntouched()
73}
74}
75
76function matchValidator (newPasswordControl: AbstractControl) {
77return function matchOtherValidate (repeatNewPasswordControl: UntypedFormControl) {
78const password = newPasswordControl.value
79const passwordRepeat = repeatNewPasswordControl.value
80if (password !== passwordRepeat) {
81return { notSame: true }
82}
83return null
84}
85}
86