Changeset View
Changeset View
Standalone View
Standalone View
src/app/Traits/UserConfigTrait.php
<?php | <?php | ||||
namespace App\Traits; | namespace App\Traits; | ||||
use App\Backends\Amavis\Policy as AmavisPolicy; | |||||
use App\Backends\Amavis\User as AmavisUser; | |||||
use App\Backends\Spamassassin\Userpref as SpamPref; | |||||
use App\Policy\Greylist; | use App\Policy\Greylist; | ||||
trait UserConfigTrait | trait UserConfigTrait | ||||
{ | { | ||||
/** | /** | ||||
* Boot function from Laravel. | |||||
*/ | |||||
protected static function bootUserConfigTrait() | |||||
{ | |||||
// Remove (external) settings on user delete | |||||
static::deleted(function ($user) { | |||||
// FIXME: Should we remove the settings on force-deleting only? | |||||
// if (!$user->isForceDeleting()) { | |||||
// return; | |||||
// } | |||||
SpamPref::where('username', $user->email)->delete(); | |||||
AmavisUser::where('email', $user->email)->each(function ($amavis) { | |||||
$amavis->policy()->delete(); | |||||
$amavis->delete(); | |||||
}); | |||||
}); | |||||
} | |||||
/** | |||||
* 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([ | $settings = $this->getSettings([ | ||||
'greylist_enabled', | 'greylist_enabled', | ||||
'guam_enabled', | 'guam_enabled', | ||||
'password_policy', | 'password_policy', | ||||
'max_password_age', | 'max_password_age', | ||||
'limit_geo' | 'limit_geo' | ||||
]); | ]); | ||||
$config = [ | $config = [ | ||||
'greylist_enabled' => $settings['greylist_enabled'] !== 'false', | 'greylist_enabled' => $settings['greylist_enabled'] !== 'false', | ||||
'guam_enabled' => $settings['guam_enabled'] === 'true', | 'guam_enabled' => $settings['guam_enabled'] === 'true', | ||||
'limit_geo' => $settings['limit_geo'] ? json_decode($settings['limit_geo'], true) : [], | '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'], | ||||
]; | ]; | ||||
// Merge the Spamassassin/Amavis settings (defaults) | |||||
$config = array_merge( | |||||
$config, | |||||
collect(SpamPref::defaults())->mapWithKeys(fn($val, $key) => ["sa_{$key}" => $val])->all(), | |||||
collect(AmavisPolicy::defaults())->mapWithKeys(fn($val, $key) => ["amavis_{$key}" => $val])->all() | |||||
); | |||||
// Spamassassin settings | |||||
SpamPref::where('username', $this->email)->get() | |||||
->each(function ($pref) use (&$config) { | |||||
$config["sa_{$pref->preference}"] = $pref->value; | |||||
}); | |||||
// Amavis settings | |||||
if (($amavis_user = AmavisUser::where('email', $this->email)->first()) | |||||
&& ($policy = $amavis_user->policy()->first()) | |||||
) { | |||||
foreach (array_keys($policy->policyDefinition()) as $opt) { | |||||
$config["amavis_{$opt}"] = $policy->{$opt}; | |||||
} | |||||
} | |||||
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 = []; | ||||
$amavis = []; | |||||
$sa = []; | |||||
foreach ($config as $key => $value) { | foreach ($config as $key => $value) { | ||||
if ($key == 'greylist_enabled') { | if (strpos($key, 'sa_') === 0) { | ||||
$sa_key = substr($key, 3); | |||||
if (SpamPref::validate($sa_key, $value, $err_idx)) { | |||||
$sa[$sa_key] = $value; | |||||
} elseif ($err_idx !== null) { | |||||
$errors[$key] = [$err_idx => \trans('validation.option-invalid-value')]; | |||||
} else { | |||||
$errors[$key] = \trans('validation.option-invalid-value'); | |||||
} | |||||
} elseif (strpos($key, 'amavis_') === 0) { | |||||
$amavis_key = substr($key, 7); | |||||
if (AmavisPolicy::validate($amavis_key, $value)) { | |||||
$amavis[$amavis_key] = $value; | |||||
} else { | |||||
$errors[$key] = \trans('validation.option-invalid-value'); | |||||
} | |||||
} elseif ($key == 'greylist_enabled') { | |||||
$this->setSetting($key, $value ? 'true' : 'false'); | $this->setSetting($key, $value ? 'true' : 'false'); | ||||
} elseif ($key == 'guam_enabled') { | } elseif ($key == 'guam_enabled') { | ||||
$this->setSetting($key, $value ? 'true' : null); | $this->setSetting($key, $value ? 'true' : null); | ||||
} elseif ($key == 'limit_geo') { | } elseif ($key == 'limit_geo') { | ||||
if (!is_array($value)) { | if (!is_array($value)) { | ||||
$errors[$key] = \trans('validation.invalid-limit-geo'); | $errors[$key] = \trans('validation.invalid-limit-geo'); | ||||
continue; | continue; | ||||
} | } | ||||
Show All 34 Lines | public function setConfig(array $config): array | ||||
} | } | ||||
$this->setSetting($key, $value); | $this->setSetting($key, $value); | ||||
} else { | } else { | ||||
$errors[$key] = \trans('validation.invalid-config-parameter'); | $errors[$key] = \trans('validation.invalid-config-parameter'); | ||||
} | } | ||||
} | } | ||||
SpamPref::saveFor($this->email, $sa); | |||||
AmavisPolicy::saveFor($this->email, $amavis); | |||||
return $errors; | return $errors; | ||||
} | } | ||||
/** | /** | ||||
* Validates password policy rule. | * Validates password policy rule. | ||||
* | * | ||||
* @param string $rule Policy rule | * @param string $rule Policy rule | ||||
* | * | ||||
Show All 40 Lines |