|
|
|
@ -1,7 +1,7 @@ |
|
|
|
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 { PasswordRules, normalizeDiacritics } from '@ngx-validate/core'; |
|
|
|
|
|
|
|
const { minLength, maxLength } = Validators; |
|
|
|
|
|
|
|
@ -31,7 +31,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 +44,37 @@ function getRuleFn(injector: Injector) { |
|
|
|
return (passwordRules[`Abp.Identity.Password.${key}`] || '').toLowerCase(); |
|
|
|
}; |
|
|
|
} |
|
|
|
const errorMessageMap = { |
|
|
|
small: 'passwordRequiresLower', |
|
|
|
capital: 'passwordRequiresUpper', |
|
|
|
number: 'passwordRequiresDigit', |
|
|
|
special: 'passwordRequiresNonAlphanumeric', |
|
|
|
}; |
|
|
|
|
|
|
|
function validatePassword(shouldContain: 'small' | 'capital' | 'number' | 'special'): 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 hasError = regex[shouldContain].test(value); |
|
|
|
|
|
|
|
if (!hasError) { |
|
|
|
return null; |
|
|
|
} |
|
|
|
|
|
|
|
const error = errorMessageMap[shouldContain]; |
|
|
|
|
|
|
|
return { |
|
|
|
[error]: true, |
|
|
|
}; |
|
|
|
}; |
|
|
|
} |
|
|
|
|