|
|
|
@ -1,7 +1,8 @@ |
|
|
|
import { ABP, ConfigStateService } from '@abp/ng.core'; |
|
|
|
import { Injector } from '@angular/core'; |
|
|
|
import { ValidatorFn, Validators } from '@angular/forms'; |
|
|
|
import { PasswordRules, validatePassword } from '@ngx-validate/core'; |
|
|
|
import { AbstractControl, ValidatorFn, Validators } from '@angular/forms'; |
|
|
|
import { normalizeDiacritics, PasswordRules } from '@ngx-validate/core'; |
|
|
|
import { PasswordRule } from "../models/validation"; |
|
|
|
|
|
|
|
const { minLength, maxLength } = Validators; |
|
|
|
|
|
|
|
@ -31,7 +32,8 @@ export function getPasswordValidators(injector: Injector): ValidatorFn[] { |
|
|
|
requiredLength = +getRule('RequiredLength'); |
|
|
|
} |
|
|
|
|
|
|
|
return [validatePassword(passwordRulesArr), minLength(requiredLength), maxLength(128)]; |
|
|
|
const passwordValidators = passwordRulesArr.map(rule => validatePassword(rule)); |
|
|
|
return [...passwordValidators, minLength(requiredLength), maxLength(128)]; |
|
|
|
} |
|
|
|
|
|
|
|
function getRuleFn(injector: Injector) { |
|
|
|
@ -43,3 +45,39 @@ function getRuleFn(injector: Injector) { |
|
|
|
return (passwordRules[`Abp.Identity.Password.${key}`] || '').toLowerCase(); |
|
|
|
}; |
|
|
|
} |
|
|
|
const errorMessageMap = { |
|
|
|
small: 'passwordRequiresLower', |
|
|
|
capital: 'passwordRequiresUpper', |
|
|
|
number: 'passwordRequiresDigit', |
|
|
|
special: 'passwordRequiresNonAlphanumeric', |
|
|
|
}; |
|
|
|
|
|
|
|
export function validatePassword( |
|
|
|
shouldContain: PasswordRule |
|
|
|
): ValidatorFn { |
|
|
|
return (control: AbstractControl) => { |
|
|
|
if (!control.value) return null; |
|
|
|
|
|
|
|
const value = normalizeDiacritics(control.value); |
|
|
|
|
|
|
|
const regexMap = { |
|
|
|
small: /.*[a-z].*/, |
|
|
|
capital: /.*[A-Z].*/, |
|
|
|
number: /.*[0-9].*/, |
|
|
|
special: /.*[^0-9a-zA-Z].*/, |
|
|
|
}; |
|
|
|
const regex = regexMap[shouldContain]; |
|
|
|
|
|
|
|
const isValid = regex.test(value); |
|
|
|
|
|
|
|
if (isValid) { |
|
|
|
return null; |
|
|
|
} |
|
|
|
|
|
|
|
const error = errorMessageMap[shouldContain]; |
|
|
|
|
|
|
|
return { |
|
|
|
[error]: true, |
|
|
|
}; |
|
|
|
}; |
|
|
|
} |
|
|
|
|