Browse Source

refactor: get password validators from getPasswordValidators fn

#3514
pull/3518/head
mehmet-erim 6 years ago
parent
commit
31f13a1b27
  1. 52
      npm/ng-packs/packages/account/src/lib/components/change-password/change-password.component.ts
  2. 46
      npm/ng-packs/packages/account/src/lib/components/register/register.component.ts
  3. 43
      npm/ng-packs/packages/identity/src/lib/components/users/users.component.ts

52
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<string> = 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],
},
],
},

46
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<string> = 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]],
});
}

43
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<AbstractControl> = (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<string> = 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]));

Loading…
Cancel
Save