Changeset View
Changeset View
Standalone View
Standalone View
src/app/Traits/UserConfigTrait.php
Show All 10 Lines | trait UserConfigTrait | ||||
*/ | */ | ||||
public function getConfig(): array | public function getConfig(): array | ||||
{ | { | ||||
$config = []; | $config = []; | ||||
// TODO: Should we store the default value somewhere in config? | // TODO: Should we store the default value somewhere in config? | ||||
$config['greylist_enabled'] = $this->getSetting('greylist_enabled') !== 'false'; | $config['greylist_enabled'] = $this->getSetting('greylist_enabled') !== 'false'; | ||||
$config['password_policy'] = $this->getSetting('password_policy'); | |||||
return $config; | return $config; | ||||
} | } | ||||
/** | /** | ||||
* A helper to update user configuration. | * A helper to update user configuration. | ||||
* | * | ||||
* @param array $config An array of configuration options | * @param array $config An array of configuration options | ||||
* | * | ||||
* @return array A list of input validation error messages | * @return array A list of input validation error messages | ||||
*/ | */ | ||||
public function setConfig(array $config): array | public function setConfig(array $config): array | ||||
{ | { | ||||
$errors = []; | $errors = []; | ||||
foreach ($config as $key => $value) { | foreach ($config as $key => $value) { | ||||
if ($key == 'greylist_enabled') { | if ($key == 'greylist_enabled') { | ||||
$this->setSetting('greylist_enabled', $value ? 'true' : 'false'); | $this->setSetting('greylist_enabled', $value ? 'true' : 'false'); | ||||
} elseif ($key == 'password_policy') { | |||||
if (!is_string($value) || (strlen($value) && !preg_match('/^[a-z0-9:,]+$/', $value))) { | |||||
$errors[$key] = \trans('validation.invalid-password-policy'); | |||||
continue; | |||||
} | |||||
foreach (explode(',', $value) as $rule) { | |||||
if ($error = $this->validatePasswordPolicyRule($rule)) { | |||||
$errors[$key] = $error; | |||||
continue 2; | |||||
} | |||||
} | |||||
$this->setSetting('password_policy', $value); | |||||
} else { | } else { | ||||
$errors[$key] = \trans('validation.invalid-config-parameter'); | $errors[$key] = \trans('validation.invalid-config-parameter'); | ||||
} | } | ||||
} | } | ||||
return $errors; | return $errors; | ||||
} | } | ||||
/** | |||||
* Validates password policy rule. | |||||
* | |||||
* @param string $rule Policy rule | |||||
* | |||||
* @return ?string An error message on error, Null otherwise | |||||
*/ | |||||
protected function validatePasswordPolicyRule(string $rule): ?string | |||||
{ | |||||
$regexp = [ | |||||
'min:[0-9]+', 'max:[0-9]+', 'upper', 'lower', 'digit', 'special', | |||||
]; | |||||
if (empty($rule) || !preg_match('/^(' . implode('|', $regexp) . ')$/', $rule)) { | |||||
return \trans('validation.invalid-password-policy'); | |||||
} | |||||
$systemPolicy = \App\Rules\Password::parsePolicy(\config('app.password_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. | |||||
if (!empty($systemPolicy['min'])) { | |||||
$value = trim(substr($rule, 4)); | |||||
if ($value < $systemPolicy['min']) { | |||||
return \trans('validation.password-policy-min-len-error', ['min' => $systemPolicy['min']]); | |||||
} | |||||
} | |||||
if (!empty($systemPolicy['max'])) { | |||||
$value = trim(substr($rule, 4)); | |||||
if ($value > $systemPolicy['max']) { | |||||
return \trans('validation.password-policy-max-len-error', ['max' => $systemPolicy['max']]); | |||||
} | |||||
} | |||||
return null; | |||||
} | |||||
} | } |