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; | |||||
} | |||||
$regexp = [ | |||||
'min:[0-9]+', 'max:[0-9]+', 'upper', 'lower', 'digit', 'special', | |||||
]; | |||||
foreach (explode(',', $value) as $rule) { | |||||
if (empty($rule) || !preg_match('/^(' . implode('|', $regexp) . ')$/', $rule)) { | |||||
$errors[$key] = \trans('validation.invalid-password-policy'); | |||||
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; | ||||
} | } | ||||
} | } |