From 9e00eed6c1fba1a0555f064e02cd379da83a252e Mon Sep 17 00:00:00 2001 From: philipp lang Date: Mon, 17 Jul 2023 16:50:05 +0200 Subject: [PATCH] Add Create service for Mailman dispatcher type --- app/Maildispatcher/Actions/StoreAction.php | 3 +- app/Mailgateway/Types/LocalType.php | 4 ++ app/Mailgateway/Types/MailmanType.php | 17 ++++- app/Mailgateway/Types/Type.php | 2 + app/Mailman/Actions/SettingIndexAction.php | 51 -------------- app/Mailman/Actions/SettingSaveAction.php | 80 ---------------------- app/Mailman/MailmanSettings.php | 53 -------------- app/Mailman/Support/MailmanService.php | 33 ++++++++- app/Setting/SettingServiceProvider.php | 2 - resources/js/views/mailgateway/Index.vue | 2 +- tests/Unit/Mailman/ServiceTest.php | 21 ++++++ 11 files changed, 76 insertions(+), 192 deletions(-) delete mode 100644 app/Mailman/Actions/SettingIndexAction.php delete mode 100644 app/Mailman/Actions/SettingSaveAction.php delete mode 100644 app/Mailman/MailmanSettings.php diff --git a/app/Maildispatcher/Actions/StoreAction.php b/app/Maildispatcher/Actions/StoreAction.php index 7ce74ce4..d409ced9 100644 --- a/app/Maildispatcher/Actions/StoreAction.php +++ b/app/Maildispatcher/Actions/StoreAction.php @@ -16,10 +16,11 @@ class StoreAction */ public function handle(array $input): void { - Maildispatcher::create([ + $dispatcher = Maildispatcher::create([ ...$input, 'filter' => (object) $input['filter'], ]); + $dispatcher->gateway->type->createList($dispatcher->name, $dispatcher->gateway->domain); ResyncAction::dispatch(); } diff --git a/app/Mailgateway/Types/LocalType.php b/app/Mailgateway/Types/LocalType.php index a214a0f6..0965b2ad 100644 --- a/app/Mailgateway/Types/LocalType.php +++ b/app/Mailgateway/Types/LocalType.php @@ -53,6 +53,10 @@ class LocalType extends Type ]); } + public function createList(string $name, string $domain): void + { + } + public function remove(string $name, string $domain, string $email): void { Localmaildispatcher::where('from', "{$name}@{$domain}")->where('to', $email)->delete(); diff --git a/app/Mailgateway/Types/MailmanType.php b/app/Mailgateway/Types/MailmanType.php index fff40655..1360febb 100644 --- a/app/Mailgateway/Types/MailmanType.php +++ b/app/Mailgateway/Types/MailmanType.php @@ -13,12 +13,14 @@ class MailmanType extends Type public string $url; public string $user; public string $password; + public string $owner; public function setParams(array $params): static { $this->url = data_get($params, 'url'); $this->user = data_get($params, 'user'); $this->password = data_get($params, 'password'); + $this->owner = data_get($params, 'owner', ''); return $this; } @@ -63,6 +65,14 @@ class MailmanType extends Type 'updateValidator' => 'nullable|max:255', 'default' => '', ], + [ + 'name' => 'owner', + 'label' => 'E-Mail-Adresse des Eigentümers', + 'type' => 'email', + 'storeValidator' => 'required|max:255', + 'updateValidator' => 'required|max:255', + 'default' => '', + ], ]; } @@ -98,6 +108,11 @@ class MailmanType extends Type $this->service()->removeMember($member); } + public function createList(string $name, string $domain): void + { + $this->service()->createList("{$name}@{$domain}"); + } + private function getList(string $name, string $domain): MailingList { $list = $this->service()->getLists()->first(fn ($list) => $list->fqdnListname === "{$name}@{$domain}"); @@ -108,6 +123,6 @@ class MailmanType extends Type private function service(): MailmanService { - return app(MailmanService::class)->setCredentials($this->url, $this->user, $this->password); + return app(MailmanService::class)->setCredentials($this->url, $this->user, $this->password)->setOwner($this->owner); } } diff --git a/app/Mailgateway/Types/Type.php b/app/Mailgateway/Types/Type.php index d1123215..e7e08066 100644 --- a/app/Mailgateway/Types/Type.php +++ b/app/Mailgateway/Types/Type.php @@ -20,6 +20,8 @@ abstract class Type abstract public function add(string $name, string $domain, string $email): void; + abstract public function createList(string $name, string $domain): void; + abstract public function remove(string $name, string $domain, string $email): void; /** diff --git a/app/Mailman/Actions/SettingIndexAction.php b/app/Mailman/Actions/SettingIndexAction.php deleted file mode 100644 index 0d33762d..00000000 --- a/app/Mailman/Actions/SettingIndexAction.php +++ /dev/null @@ -1,51 +0,0 @@ - - */ - public function handle(MailmanSettings $settings): array - { - return [ - 'is_active' => $settings->is_active, - 'base_url' => $settings->base_url, - 'username' => $settings->username, - 'all_list' => $settings->all_list, - 'all_parents_list' => $settings->all_parents_list, - 'active_leaders_list' => $settings->active_leaders_list, - 'passive_leaders_list' => $settings->passive_leaders_list, - 'password' => '', - ]; - } - - public function asController(MailmanSettings $settings): Response - { - session()->put('menu', 'setting'); - session()->put('title', 'Mailman-Einstellungen'); - - if ($settings->is_active) { - $state = app(MailmanService::class)->fromSettings($settings)->check(); - $lists = app(MailmanService::class)->fromSettings($settings)->getLists(); - } else { - $state = null; - $lists = []; - } - - return Inertia::render('setting/Mailman', [ - 'data' => $this->handle($settings), - 'state' => $state, - 'lists' => $lists, - ]); - } -} diff --git a/app/Mailman/Actions/SettingSaveAction.php b/app/Mailman/Actions/SettingSaveAction.php deleted file mode 100644 index 4176d305..00000000 --- a/app/Mailman/Actions/SettingSaveAction.php +++ /dev/null @@ -1,80 +0,0 @@ - $input - */ - public function handle(array $input): void - { - $settings = app(MailmanSettings::class); - - $settings->fill([ - 'base_url' => $input['base_url'] ?? null, - 'username' => $input['username'] ?? null, - 'password' => $input['password'] ?? null, - 'all_list' => $input['all_list'] ?? null, - 'all_parents_list' => $input['all_parents_list'] ?? null, - 'active_leaders_list' => $input['active_leaders_list'] ?? null, - 'passive_leaders_list' => $input['passive_leaders_list'] ?? null, - 'is_active' => $input['is_active'] ?? false, - ]); - - $settings->save(); - } - - /** - * @return array - */ - public function rules(): array - { - return [ - 'base_url' => 'required_if:is_active,true', - 'username' => 'required_if:is_active,true', - 'password' => 'required_if:is_active,true', - ]; - } - - /** - * @return array - */ - public function getValidationMessages(): array - { - return [ - 'base_url.required_if' => 'URL ist erforderlich.', - 'username.required_if' => 'Benutzername ist erforderlich.', - 'password.required_if' => 'Passwort ist erforderlich.', - ]; - } - - public function afterValidator(Validator $validator, ActionRequest $request): void - { - if (false === $request->is_active || !$request->filled(['base_url', 'username', 'password'])) { - return; - } - - $result = app(MailmanService::class)->setCredentials($request->input('base_url'), $request->input('username'), $request->input('password'))->check(); - - if (!$result) { - $validator->errors()->add('mailman', 'Verbindung fehlgeschlagen.'); - } - } - - public function asController(ActionRequest $request): RedirectResponse - { - $this->handle($request->all()); - - return redirect()->back(); - } -} diff --git a/app/Mailman/MailmanSettings.php b/app/Mailman/MailmanSettings.php deleted file mode 100644 index d9862726..00000000 --- a/app/Mailman/MailmanSettings.php +++ /dev/null @@ -1,53 +0,0 @@ -setCredentials($settings->base_url, $settings->username, $settings->password); + $this->owner = $owner; + + return $this; + } + + public function createList(string $mailAddress): MailingList + { + $response = $this->http()->post('/lists', [ + 'fqdn_listname' => $mailAddress, + ]); + throw_unless(201 === $response->status(), MailmanServiceException::class, 'Creating list failed'); + $list = $this->getLists()->first(fn ($list) => $list->fqdnListname === $mailAddress); + $response = $this->http()->patch("/lists/{$list->listId}/config", [ + 'advertised' => 'False', + 'respond_to_post_requests' => 'False', + 'send_goodbye_message' => 'False', + 'send_welcome_message' => 'False', + 'admin_immed_notify' => 'True', + ]); + throw_unless(204 === $response->status(), MailmanServiceException::class, 'Updating list config failed'); + $response = $this->http()->post('/members', [ + 'list_id' => $list->listId, + 'subscriber' => $this->owner, + 'role' => 'owner', + ]); + throw_unless(201 === $response->status(), MailmanServiceException::class, 'Creating list owner failed'); + + return $list; } public function check(): bool diff --git a/app/Setting/SettingServiceProvider.php b/app/Setting/SettingServiceProvider.php index 4dec534a..75cfc39a 100644 --- a/app/Setting/SettingServiceProvider.php +++ b/app/Setting/SettingServiceProvider.php @@ -4,7 +4,6 @@ namespace App\Setting; use App\Invoice\InvoiceSettings; use App\Mailgateway\MailgatewaySettings; -use App\Mailman\MailmanSettings; use Illuminate\Support\ServiceProvider; class SettingServiceProvider extends ServiceProvider @@ -27,7 +26,6 @@ class SettingServiceProvider extends ServiceProvider public function boot() { app(SettingFactory::class)->register(InvoiceSettings::class); - app(SettingFactory::class)->register(MailmanSettings::class); app(SettingFactory::class)->register(MailgatewaySettings::class); } } diff --git a/resources/js/views/mailgateway/Index.vue b/resources/js/views/mailgateway/Index.vue index 4397eee2..22414a48 100644 --- a/resources/js/views/mailgateway/Index.vue +++ b/resources/js/views/mailgateway/Index.vue @@ -26,7 +26,7 @@