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 | ||||
{ | { | ||||
$settings = $this->getSettings(['greylist_enabled', 'password_policy', 'max_password_age']); | $settings = $this->getSettings(['greylist_enabled', 'password_policy', 'max_password_age', 'limit_geo']); | ||||
$config = [ | $config = [ | ||||
'greylist_enabled' => $settings['greylist_enabled'] !== 'false', | 'greylist_enabled' => $settings['greylist_enabled'] !== 'false', | ||||
'limit_geo' => $settings['limit_geo'] ? json_decode($settings['limit_geo'], true) : [], | |||||
'max_password_age' => $settings['max_password_age'], | 'max_password_age' => $settings['max_password_age'], | ||||
'password_policy' => $settings['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($key, $value ? 'true' : 'false'); | $this->setSetting($key, $value ? 'true' : 'false'); | ||||
} elseif ($key == 'limit_geo') { | |||||
if (!is_array($value)) { | |||||
$errors[$key] = \trans('validation.invalid-limit-geo'); | |||||
continue; | |||||
} | |||||
foreach ($value as $idx => $country) { | |||||
if (!preg_match('/^[a-zA-Z]{2}$/', $country)) { | |||||
$errors[$key] = \trans('validation.invalid-limit-geo'); | |||||
continue 2; | |||||
} | |||||
$value[$idx] = \strtoupper($country); | |||||
} | |||||
if (count($value) > 250) { | |||||
$errors[$key] = \trans('validation.invalid-limit-geo'); | |||||
} | |||||
$this->setSetting($key, !empty($value) ? json_encode($value) : null); | |||||
} elseif ($key == 'max_password_age') { | } elseif ($key == 'max_password_age') { | ||||
$this->setSetting($key, intval($value) > 0 ? (int) $value : null); | $this->setSetting($key, intval($value) > 0 ? (int) $value : null); | ||||
} 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 | ||||
▲ Show 20 Lines • Show All 67 Lines • Show Last 20 Lines |