Changeset View
Changeset View
Standalone View
Standalone View
src/app/Traits/UserConfigTrait.php
Show First 20 Lines • Show All 61 Lines • ▼ Show 20 Lines | trait UserConfigTrait | ||||
* | * | ||||
* @param string $rule Policy rule | * @param string $rule Policy rule | ||||
* | * | ||||
* @return ?string An error message on error, Null otherwise | * @return ?string An error message on error, Null otherwise | ||||
*/ | */ | ||||
protected function validatePasswordPolicyRule(string $rule): ?string | protected function validatePasswordPolicyRule(string $rule): ?string | ||||
{ | { | ||||
$regexp = [ | $regexp = [ | ||||
'min:[0-9]+', 'max:[0-9]+', 'upper', 'lower', 'digit', 'special', | 'min:[0-9]+', 'max:[0-9]+', 'upper', 'lower', 'digit', 'special', 'last:[0-9]+' | ||||
]; | ]; | ||||
if (empty($rule) || !preg_match('/^(' . implode('|', $regexp) . ')$/', $rule)) { | if (empty($rule) || !preg_match('/^(' . implode('|', $regexp) . ')$/', $rule)) { | ||||
return \trans('validation.invalid-password-policy'); | return \trans('validation.invalid-password-policy'); | ||||
} | } | ||||
$systemPolicy = \App\Rules\Password::parsePolicy(\config('app.password_policy')); | $systemPolicy = \App\Rules\Password::parsePolicy(\config('app.password_policy')); | ||||
// Min/Max values cannot exceed the system defaults, i.e. if system policy | // Min/Max values cannot exceed the system defaults, i.e. if system policy | ||||
// is min:5, user's policy cannot be set to a smaller number. | // is min:5, user's policy cannot be set to a smaller number. | ||||
if (!empty($systemPolicy['min']) && strpos($rule, 'min:') === 0) { | if (!empty($systemPolicy['min']) && strpos($rule, 'min:') === 0) { | ||||
$value = trim(substr($rule, 4)); | $value = trim(substr($rule, 4)); | ||||
if ($value < $systemPolicy['min']) { | if ($value < $systemPolicy['min']) { | ||||
return \trans('validation.password-policy-min-len-error', ['min' => $systemPolicy['min']]); | return \trans('validation.password-policy-min-len-error', ['min' => $systemPolicy['min']]); | ||||
} | } | ||||
} | } | ||||
if (!empty($systemPolicy['max']) && strpos($rule, 'max:') === 0) { | if (!empty($systemPolicy['max']) && strpos($rule, 'max:') === 0) { | ||||
$value = trim(substr($rule, 4)); | $value = trim(substr($rule, 4)); | ||||
if ($value > $systemPolicy['max']) { | if ($value > $systemPolicy['max']) { | ||||
return \trans('validation.password-policy-max-len-error', ['max' => $systemPolicy['max']]); | return \trans('validation.password-policy-max-len-error', ['max' => $systemPolicy['max']]); | ||||
} | } | ||||
} | } | ||||
if (!empty($systemPolicy['last']) && strpos($rule, 'last:') === 0) { | |||||
$value = trim(substr($rule, 5)); | |||||
if ($value < $systemPolicy['last']) { | |||||
return \trans('validation.password-policy-last-error', ['last' => $systemPolicy['last']]); | |||||
} | |||||
} | |||||
return null; | return null; | ||||
} | } | ||||
} | } |