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; | |||||
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 = []; | $config = []; | ||||
// TODO: Should we store the default value somewhere in config? | // TODO: Should we store the default value somewhere in config? | ||||
$config['greylisting'] = $this->getSetting('greylist_enabled') !== 'false'; | $config['greylisting'] = Greylist\Setting::where('key', 'greylist_enabled') | ||||
->where('object_id', $this->id) | |||||
->where('object_type', \App\User::class) | |||||
->where('value', 'false') | |||||
->exists() ? false : true; | |||||
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 == 'greylisting') { | if ($key == 'greylisting') { | ||||
$this->setSetting('greylist_enabled', $value ? 'true' : 'false'); | $setting_value = $value ? 'true' : 'false'; | ||||
$setting = Greylist\Setting::where('key', 'greylist_enabled') | |||||
->where('object_id', $this->id) | |||||
->where('object_type', \App\User::class) | |||||
->first(); | |||||
if (!$setting) { | |||||
Greylist\Setting::create([ | |||||
'key' => 'greylist_enabled', | |||||
'object_id' => $this->id, | |||||
'object_type' => \App\User::class, | |||||
'value' => $setting_value, | |||||
]); | |||||
} else { | |||||
$setting->value = $setting_value; | |||||
$setting->save(); | |||||
} | |||||
} else { | } else { | ||||
$errors[$key] = \trans('validation.invalid-config-parameter'); | $errors[$key] = \trans('validation.invalid-config-parameter'); | ||||
} | } | ||||
} | } | ||||
return $errors; | return $errors; | ||||
} | } | ||||
} | } |