Changeset View
Changeset View
Standalone View
Standalone View
src/app/Traits/UserConfigTrait.php
<?php | <?php | ||||
namespace App\Traits; | namespace App\Traits; | ||||
use App\Policy\Greylist; | use App\Policy\Greylist; | ||||
trait UserConfigTrait | trait UserConfigTrait | ||||
{ | { | ||||
/** | /** | ||||
* A helper to get the user configuration. | * A helper to get the user configuration. | ||||
*/ | */ | ||||
public function getConfig(): array | public function getConfig(): array | ||||
{ | { | ||||
$config = []; | $settings = $this->getSettings(['greylist_enabled', 'password_policy', 'max_password_age']); | ||||
// TODO: Should we store the default value somewhere in config? | $config = [ | ||||
'greylist_enabled' => $settings['greylist_enabled'] !== 'false', | |||||
$config['greylist_enabled'] = $this->getSetting('greylist_enabled') !== 'false'; | 'max_password_age' => $settings['max_password_age'], | ||||
$config['password_policy'] = $this->getSetting('password_policy'); | 'password_policy' => $settings['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($key, $value ? 'true' : 'false'); | ||||
} elseif ($key == 'max_password_age') { | |||||
$this->setSetting($key, intval($value) > 0 ? (int) $value : null); | |||||
mollekopf: nitpick: intval($value) > 0 would do the job I think. | |||||
} elseif ($key == 'password_policy') { | } elseif ($key == 'password_policy') { | ||||
// Validate the syntax and make sure min and max is included | // Validate the syntax and make sure min and max is included | ||||
if ( | if ( | ||||
!is_string($value) | !is_string($value) | ||||
|| strpos($value, 'min:') === false | || strpos($value, 'min:') === false | ||||
|| strpos($value, 'max:') === false | || strpos($value, 'max:') === false | ||||
|| !preg_match('/^[a-z0-9:,]+$/', $value) | || !preg_match('/^[a-z0-9:,]+$/', $value) | ||||
) { | ) { | ||||
$errors[$key] = \trans('validation.invalid-password-policy'); | $errors[$key] = \trans('validation.invalid-password-policy'); | ||||
continue; | continue; | ||||
} | } | ||||
foreach (explode(',', $value) as $rule) { | foreach (explode(',', $value) as $rule) { | ||||
if ($error = $this->validatePasswordPolicyRule($rule)) { | if ($error = $this->validatePasswordPolicyRule($rule)) { | ||||
$errors[$key] = $error; | $errors[$key] = $error; | ||||
continue 2; | continue 2; | ||||
} | } | ||||
} | } | ||||
$this->setSetting('password_policy', $value); | $this->setSetting($key, $value); | ||||
} else { | } else { | ||||
$errors[$key] = \trans('validation.invalid-config-parameter'); | $errors[$key] = \trans('validation.invalid-config-parameter'); | ||||
} | } | ||||
} | } | ||||
return $errors; | return $errors; | ||||
} | } | ||||
▲ Show 20 Lines • Show All 45 Lines • Show Last 20 Lines |
nitpick: intval($value) > 0 would do the job I think.