From 31f13a1b27b2de15bbad7f181e444be3e09cdac8 Mon Sep 17 00:00:00 2001 From: mehmet-erim Date: Wed, 8 Apr 2020 15:40:51 +0300 Subject: [PATCH] refactor: get password validators from getPasswordValidators fn #3514 --- .../change-password.component.ts | 52 +++---------------- .../components/register/register.component.ts | 46 +++------------- .../lib/components/users/users.component.ts | 43 ++------------- 3 files changed, 18 insertions(+), 123 deletions(-) diff --git a/npm/ng-packs/packages/account/src/lib/components/change-password/change-password.component.ts b/npm/ng-packs/packages/account/src/lib/components/change-password/change-password.component.ts index 14ed582d95..c2b02c197b 100644 --- a/npm/ng-packs/packages/account/src/lib/components/change-password/change-password.component.ts +++ b/npm/ng-packs/packages/account/src/lib/components/change-password/change-password.component.ts @@ -1,14 +1,14 @@ -import { ChangePassword, ConfigState, ABP } from '@abp/ng.core'; -import { ToasterService } from '@abp/ng.theme.shared'; +import { ChangePassword } from '@abp/ng.core'; +import { getPasswordValidators, ToasterService } from '@abp/ng.theme.shared'; import { Component, OnInit } from '@angular/core'; import { FormBuilder, FormGroup, Validators } from '@angular/forms'; -import { comparePasswords, Validation, PasswordRules, validatePassword } from '@ngx-validate/core'; +import { comparePasswords, Validation } from '@ngx-validate/core'; import { Store } from '@ngxs/store'; -import snq from 'snq'; import { finalize } from 'rxjs/operators'; +import snq from 'snq'; import { Account } from '../../models/account'; -const { minLength, required, maxLength } = Validators; +const { required } = Validators; const PASSWORD_FIELDS = ['newPassword', 'repeatNewPassword']; @@ -36,33 +36,7 @@ export class ChangePasswordComponent ) {} ngOnInit(): void { - const passwordRules: ABP.Dictionary = this.store.selectSnapshot( - ConfigState.getSettings('Identity.Password'), - ); - const passwordRulesArr = [] as PasswordRules; - let requiredLength = 1; - - if ((passwordRules['Abp.Identity.Password.RequireDigit'] || '').toLowerCase() === 'true') { - passwordRulesArr.push('number'); - } - - if ((passwordRules['Abp.Identity.Password.RequireLowercase'] || '').toLowerCase() === 'true') { - passwordRulesArr.push('small'); - } - - if ((passwordRules['Abp.Identity.Password.RequireUppercase'] || '').toLowerCase() === 'true') { - passwordRulesArr.push('capital'); - } - - if ( - (passwordRules['Abp.Identity.Password.RequireNonAlphanumeric'] || '').toLowerCase() === 'true' - ) { - passwordRulesArr.push('special'); - } - - if (Number.isInteger(+passwordRules['Abp.Identity.Password.RequiredLength'])) { - requiredLength = +passwordRules['Abp.Identity.Password.RequiredLength']; - } + const passwordValidations = getPasswordValidators(this.store); this.form = this.fb.group( { @@ -70,23 +44,13 @@ export class ChangePasswordComponent newPassword: [ '', { - validators: [ - required, - validatePassword(passwordRulesArr), - minLength(requiredLength), - maxLength(128), - ], + validators: [required, ...passwordValidations], }, ], repeatNewPassword: [ '', { - validators: [ - required, - validatePassword(passwordRulesArr), - minLength(requiredLength), - maxLength(128), - ], + validators: [required, ...passwordValidations], }, ], }, diff --git a/npm/ng-packs/packages/account/src/lib/components/register/register.component.ts b/npm/ng-packs/packages/account/src/lib/components/register/register.component.ts index 6785fc67fd..c9ff280792 100644 --- a/npm/ng-packs/packages/account/src/lib/components/register/register.component.ts +++ b/npm/ng-packs/packages/account/src/lib/components/register/register.component.ts @@ -1,18 +1,15 @@ -import { ConfigState, GetAppConfiguration, ABP, SessionState, AuthService } from '@abp/ng.core'; -import { ToasterService } from '@abp/ng.theme.shared'; +import { AuthService, ConfigState } from '@abp/ng.core'; +import { getPasswordValidators, ToasterService } from '@abp/ng.theme.shared'; import { Component, OnInit } from '@angular/core'; import { FormBuilder, FormGroup, Validators } from '@angular/forms'; -import { Navigate } from '@ngxs/router-plugin'; import { Store } from '@ngxs/store'; import { OAuthService } from 'angular-oauth2-oidc'; -import { from, throwError } from 'rxjs'; -import { catchError, finalize, switchMap, take, tap } from 'rxjs/operators'; +import { throwError } from 'rxjs'; +import { catchError, finalize, switchMap } from 'rxjs/operators'; import snq from 'snq'; import { RegisterRequest } from '../../models'; import { AccountService } from '../../services/account.service'; -import { PasswordRules, validatePassword } from '@ngx-validate/core'; -import { HttpHeaders } from '@angular/common/http'; -const { maxLength, minLength, required, email } = Validators; +const { maxLength, required, email } = Validators; @Component({ selector: 'abp-register', @@ -53,40 +50,9 @@ export class RegisterComponent implements OnInit { return; } - const passwordRules: ABP.Dictionary = this.store.selectSnapshot( - ConfigState.getSettings('Identity.Password'), - ); - const passwordRulesArr = [] as PasswordRules; - let requiredLength = 1; - - if ((passwordRules['Abp.Identity.Password.RequireDigit'] || '').toLowerCase() === 'true') { - passwordRulesArr.push('number'); - } - - if ((passwordRules['Abp.Identity.Password.RequireLowercase'] || '').toLowerCase() === 'true') { - passwordRulesArr.push('small'); - } - - if ((passwordRules['Abp.Identity.Password.RequireUppercase'] || '').toLowerCase() === 'true') { - passwordRulesArr.push('capital'); - } - - if ( - (passwordRules['Abp.Identity.Password.RequireNonAlphanumeric'] || '').toLowerCase() === 'true' - ) { - passwordRulesArr.push('special'); - } - - if (Number.isInteger(+passwordRules['Abp.Identity.Password.RequiredLength'])) { - requiredLength = +passwordRules['Abp.Identity.Password.RequiredLength']; - } - this.form = this.fb.group({ username: ['', [required, maxLength(255)]], - password: [ - '', - [required, validatePassword(passwordRulesArr), minLength(requiredLength), maxLength(128)], - ], + password: ['', [required, ...getPasswordValidators(this.store)]], email: ['', [required, email]], }); } diff --git a/npm/ng-packs/packages/identity/src/lib/components/users/users.component.ts b/npm/ng-packs/packages/identity/src/lib/components/users/users.component.ts index 0be2c3d66d..1e943a6be6 100644 --- a/npm/ng-packs/packages/identity/src/lib/components/users/users.component.ts +++ b/npm/ng-packs/packages/identity/src/lib/components/users/users.component.ts @@ -1,5 +1,5 @@ -import { ABP, ConfigState } from '@abp/ng.core'; -import { ConfirmationService, Confirmation } from '@abp/ng.theme.shared'; +import { ABP } from '@abp/ng.core'; +import { Confirmation, ConfirmationService, getPasswordValidators } from '@abp/ng.theme.shared'; import { Component, OnInit, TemplateRef, TrackByFunction, ViewChild } from '@angular/core'; import { AbstractControl, @@ -9,7 +9,6 @@ import { FormGroup, Validators, } from '@angular/forms'; -import { PasswordRules, validatePassword } from '@ngx-validate/core'; import { Select, Store } from '@ngxs/store'; import { Observable } from 'rxjs'; import { finalize, pluck, switchMap, take } from 'rxjs/operators'; @@ -23,8 +22,8 @@ import { UpdateUser, } from '../../actions/identity.actions'; import { Identity } from '../../models/identity'; -import { IdentityState } from '../../states/identity.state'; import { IdentityService } from '../../services/identity.service'; +import { IdentityState } from '../../states/identity.state'; @Component({ selector: 'abp-users', templateUrl: './users.component.html', @@ -63,10 +62,6 @@ export class UsersComponent implements OnInit { sortKey = ''; - passwordRulesArr = [] as PasswordRules; - - requiredPasswordLength = 1; - trackByFn: TrackByFunction = (index, item) => Object.keys(item)[0] || index; onVisiblePermissionChange = event => { @@ -86,32 +81,6 @@ export class UsersComponent implements OnInit { ngOnInit() { this.get(); - - const passwordRules: ABP.Dictionary = this.store.selectSnapshot( - ConfigState.getSettings('Identity.Password'), - ); - - if ((passwordRules['Abp.Identity.Password.RequireDigit'] || '').toLowerCase() === 'true') { - this.passwordRulesArr.push('number'); - } - - if ((passwordRules['Abp.Identity.Password.RequireLowercase'] || '').toLowerCase() === 'true') { - this.passwordRulesArr.push('small'); - } - - if ((passwordRules['Abp.Identity.Password.RequireUppercase'] || '').toLowerCase() === 'true') { - this.passwordRulesArr.push('capital'); - } - - if ( - (passwordRules['Abp.Identity.Password.RequireNonAlphanumeric'] || '').toLowerCase() === 'true' - ) { - this.passwordRulesArr.push('special'); - } - - if (Number.isInteger(+passwordRules['Abp.Identity.Password.RequiredLength'])) { - this.requiredPasswordLength = +passwordRules['Abp.Identity.Password.RequiredLength']; - } } onSearch(value: string) { @@ -146,11 +115,7 @@ export class UsersComponent implements OnInit { ), }); - const passwordValidators = [ - validatePassword(this.passwordRulesArr), - Validators.minLength(this.requiredPasswordLength), - Validators.maxLength(128), - ]; + const passwordValidators = getPasswordValidators(this.store); this.form.addControl('password', new FormControl('', [...passwordValidators]));