From 05e04948e609c069bcf421a8c2cf70200d7817db Mon Sep 17 00:00:00 2001 From: sumeyyeKurtulus Date: Thu, 29 Aug 2024 11:52:17 +0300 Subject: [PATCH] add: `getRequiredPasswordLength` to sustain possible length constraint --- .../core/src/lib/utils/generator-utils.ts | 16 +++++++++++++++- 1 file changed, 15 insertions(+), 1 deletion(-) diff --git a/npm/ng-packs/packages/core/src/lib/utils/generator-utils.ts b/npm/ng-packs/packages/core/src/lib/utils/generator-utils.ts index 7b95e71a59..4c4dd99628 100644 --- a/npm/ng-packs/packages/core/src/lib/utils/generator-utils.ts +++ b/npm/ng-packs/packages/core/src/lib/utils/generator-utils.ts @@ -1,3 +1,7 @@ +import { Injector } from '@angular/core'; +import { ABP } from '../models'; +import { ConfigStateService } from '../services'; + export function uuid(a?: any): string { return a ? (a ^ ((Math.random() * 16) >> (a / 4))).toString(16) @@ -16,7 +20,11 @@ export function generateHash(value: string): number { return hashed; } -export function generatePassword(length = 8) { +export function generatePassword(injector?: Injector, length = 8) { + if (injector) { + length = getRequiredPasswordLength(injector); + } + length = Math.min(Math.max(4, length), 128); const lowers = 'abcdefghijklmnopqrstuvwxyz'; @@ -39,3 +47,9 @@ export function generatePassword(length = 8) { return password.sort(() => 0.5 - Math.random()).join(''); } + +function getRequiredPasswordLength(injector: Injector) { + const configState = injector.get(ConfigStateService); + const passwordRules: ABP.Dictionary = configState.getSettings('Identity.Password'); + return Number(passwordRules['Abp.Identity.Password.RequiredLength']) || 8; +}