juice-shop

Форк
0
/
change-password.component.ts 
85 строк · 3.4 Кб
1
/*
2
 * Copyright (c) 2014-2024 Bjoern Kimminich & the OWASP Juice Shop contributors.
3
 * SPDX-License-Identifier: MIT
4
 */
5

6
import { type AbstractControl, UntypedFormControl, Validators } from '@angular/forms'
7
import { UserService } from '../Services/user.service'
8
import { Component } from '@angular/core'
9
import { library } from '@fortawesome/fontawesome-svg-core'
10
import { faSave } from '@fortawesome/free-solid-svg-icons'
11
import { faEdit } from '@fortawesome/free-regular-svg-icons'
12
import { FormSubmitService } from '../Services/form-submit.service'
13
import { TranslateService } from '@ngx-translate/core'
14

15
library.add(faSave, faEdit)
16

17
@Component({
18
  selector: 'app-change-password',
19
  templateUrl: './change-password.component.html',
20
  styleUrls: ['./change-password.component.scss']
21
})
22
export class ChangePasswordComponent {
23
  public passwordControl: UntypedFormControl = new UntypedFormControl('', [Validators.required])
24
  public newPasswordControl: UntypedFormControl = new UntypedFormControl('', [Validators.required, Validators.minLength(5), Validators.maxLength(40)])
25
  public repeatNewPasswordControl: UntypedFormControl = new UntypedFormControl('', [Validators.required, Validators.minLength(5), Validators.maxLength(40), matchValidator(this.newPasswordControl)])
26
  public error: any
27
  public confirmation: any
28

29
  constructor (private readonly userService: UserService, private readonly formSubmitService: FormSubmitService, private readonly translate: TranslateService) { }
30

31
  ngOnInit () {
32
    this.formSubmitService.attachEnterKeyHandler('password-form', 'changeButton', () => { this.changePassword() })
33
  }
34

35
  changePassword () {
36
    if (localStorage.getItem('email')?.match(/support@.*/) && !this.newPasswordControl.value.match(/(?=.*[a-z])(?=.*[A-Z])(?=.*\d)(?=.*[@$!%*?&])[A-Za-z\d@$!%*?&]{12,30}/)) {
37
      console.error('Parola echipei de asistență nu respectă politica corporativă pentru conturile privilegiate! Vă rugăm să schimbați parola în consecință!')
38
    }
39
    this.userService.changePassword({
40
      current: this.passwordControl.value,
41
      new: this.newPasswordControl.value,
42
      repeat: this.repeatNewPasswordControl.value
43
    }).subscribe((response: any) => {
44
      this.error = undefined
45
      this.translate.get('PASSWORD_SUCCESSFULLY_CHANGED').subscribe((passwordSuccessfullyChanged) => {
46
        this.confirmation = passwordSuccessfullyChanged
47
      }, (translationId) => {
48
        this.confirmation = { error: translationId }
49
      })
50
      this.resetForm()
51
    }, (error) => {
52
      console.log(error)
53
      this.error = error
54
      this.confirmation = undefined
55
      this.resetPasswords()
56
    })
57
  }
58

59
  resetForm () {
60
    this.passwordControl.setValue('')
61
    this.resetPasswords()
62
  }
63

64
  resetPasswords () {
65
    this.passwordControl.markAsPristine()
66
    this.passwordControl.markAsUntouched()
67
    this.newPasswordControl.setValue('')
68
    this.newPasswordControl.markAsPristine()
69
    this.newPasswordControl.markAsUntouched()
70
    this.repeatNewPasswordControl.setValue('')
71
    this.repeatNewPasswordControl.markAsPristine()
72
    this.repeatNewPasswordControl.markAsUntouched()
73
  }
74
}
75

76
function matchValidator (newPasswordControl: AbstractControl) {
77
  return function matchOtherValidate (repeatNewPasswordControl: UntypedFormControl) {
78
    const password = newPasswordControl.value
79
    const passwordRepeat = repeatNewPasswordControl.value
80
    if (password !== passwordRepeat) {
81
      return { notSame: true }
82
    }
83
    return null
84
  }
85
}
86

Использование cookies

Мы используем файлы cookie в соответствии с Политикой конфиденциальности и Политикой использования cookies.

Нажимая кнопку «Принимаю», Вы даете АО «СберТех» согласие на обработку Ваших персональных данных в целях совершенствования нашего веб-сайта и Сервиса GitVerse, а также повышения удобства их использования.

Запретить использование cookies Вы можете самостоятельно в настройках Вашего браузера.