From 959d50339aa0bc36281cd2542e1feccc45157274 Mon Sep 17 00:00:00 2001 From: Nguyen Van Nguyen Date: Wed, 24 Apr 2024 14:10:03 +0700 Subject: [PATCH] WIP Signed-off-by: Nguyen Van Nguyen --- app/Filament/Pages/ManageZimbraSettings.php | 42 ++++++++++++++++++++- app/Support/ZimbraAdminClient.php | 7 ++++ config/app.php | 2 +- 3 files changed, 48 insertions(+), 3 deletions(-) diff --git a/app/Filament/Pages/ManageZimbraSettings.php b/app/Filament/Pages/ManageZimbraSettings.php index b06a81c..4ee631a 100644 --- a/app/Filament/Pages/ManageZimbraSettings.php +++ b/app/Filament/Pages/ManageZimbraSettings.php @@ -9,10 +9,13 @@ namespace App\Filament\Pages; use App\Settings\ZimbraSettings; +use App\Support\ZimbraAdminClient; use Filament\Actions\Action; use Filament\Forms\Components\TextInput; use Filament\Forms\Form; +use Filament\Notifications\Notification; use Filament\Pages\SettingsPage; +use Filament\Support\Exceptions\Halt; /** * Zimbra settings page @@ -52,8 +55,43 @@ class ManageZimbraSettings extends SettingsPage protected function getHeaderActions(): array { return [ - Action::make('test_connection') - ->label(__('Test Connection')), + Action::make('test_connection')->action( + fn () => $this->testConnection() + )->label(__('Test Connection')), ]; } + + public function afterValidate() + { + if (!$this->testConnection(false)) { + throw (new Halt())->rollBackDatabaseTransaction(false); + } + } + + private function testConnection(bool $notifySuccess = true): bool + { + try { + $data = $this->getForm()->getState(); + $client = new ZimbraAdminClient($data['serviceUrl']); + $client->auth( + $data['adminUser'], $data['adminPassword'] + ); + if ($notifySuccess) { + Notification::make() + ->success() + ->title(__('Connection to zimbra service success!')) + ->send(); + } + return true; + } + catch (\Throwable $t) { + logger()->error($t); + Notification::make() + ->warning() + ->title(__('Connection to zimbra service failed!')) + ->body($t->getMessage()) + ->send(); + return false; + } + } } diff --git a/app/Support/ZimbraAdminClient.php b/app/Support/ZimbraAdminClient.php index f0ce6b6..3ddce9d 100644 --- a/app/Support/ZimbraAdminClient.php +++ b/app/Support/ZimbraAdminClient.php @@ -8,7 +8,9 @@ namespace App\Support; +use Illuminate\Support\Facades\Http; use Illuminate\Support\Traits\ForwardsCalls; +use PsrDiscovery\Implementations\Psr18\Clients; use Zimbra\Admin\AdminApi; /** @@ -26,6 +28,11 @@ class ZimbraAdminClient public function __construct(string $serviceUrl) { + $debug = config('app.debug'); + Clients::use(Http::withOptions([ + 'debug' => $debug, + 'verify' => !$debug, + ])->buildClient()); $this->api = new AdminApi($serviceUrl); $this->api->setLoger(logger()); } diff --git a/config/app.php b/config/app.php index b0bbd56..b2caf81 100644 --- a/config/app.php +++ b/config/app.php @@ -13,7 +13,7 @@ return [ | */ - 'name' => env('APP_NAME', 'Laravel'), + 'name' => env('APP_NAME', 'Zimbra Admin Panel'), /* |--------------------------------------------------------------------------