From 300b024e0d170329952924e913590a8d71b47d9f Mon Sep 17 00:00:00 2001 From: soufiane Date: Thu, 23 Nov 2023 17:51:08 +0100 Subject: [PATCH] wait #6477 --- .../Admin/Operations/ServerOperation.php | 16 ++--- app/Models/FluidbookExternalInstallServer.php | 65 ++++++++----------- .../crud/edit-external-server.blade.php | 7 +- 3 files changed, 34 insertions(+), 54 deletions(-) diff --git a/app/Http/Controllers/Admin/Operations/ServerOperation.php b/app/Http/Controllers/Admin/Operations/ServerOperation.php index ac764bf68..2c38eba66 100644 --- a/app/Http/Controllers/Admin/Operations/ServerOperation.php +++ b/app/Http/Controllers/Admin/Operations/ServerOperation.php @@ -25,34 +25,26 @@ trait ServerOperation protected function run(Request $request, $id) { $validation = [ 'password' => 'required|string|min:8', - 'protocol' => 'required|string|max:255', 'username' => 'required|string|max:255', 'host' => 'required|string|max:255', 'port' => 'nullable|numeric', ]; $validator = Validator::make([ 'password' => $request->password, - 'protocol' => $request->protocol, 'username' => $request->username, 'host' => $request->host, 'port' => $request->port], $validation); if ($validator->fails()) { + $result = ['error' => true, 'message' => __('Des champs sont manquants')]; throw new ValidationException($validator); } - $validator->validate(); + $validateData = $validator->validate(); - $result = FluidbookExternalInstallServer::verifyServerConnexion($id, $request->all()); - if($result) { - //Alert::add('success', __('La connexion a été établie avec succès'))->flash(); - $data = ['success' => true, 'message' => __('La connexion a été établie avec succès')]; - } else { - //Alert::add('error', __('La connexion a échouée'))->flash(); - $data = ['error' => true, 'message' => __('La connexion a échouée')]; - } + $result = FluidbookExternalInstallServer::verifyServerConnexion($id, $validateData); - return response()->json($data); + return response()->json($result); } } diff --git a/app/Models/FluidbookExternalInstallServer.php b/app/Models/FluidbookExternalInstallServer.php index 98c03dbd7..0233406e9 100644 --- a/app/Models/FluidbookExternalInstallServer.php +++ b/app/Models/FluidbookExternalInstallServer.php @@ -36,7 +36,6 @@ class FluidbookExternalInstallServer extends ExternalServer parent::setFields(); $this->addField('redirections', Table::class, __('Redirections'), ['entity_singular' => __('redirection'), 'columns' => ['from' => __('De'), 'to' => __('Vers')], 'when' => ['protocol' => 'hosting']]); $this->addField('allows_root', Checkbox::class, __('Autoriser le chargement à la racine (sur le chemin de base)'), ['default' => false]); - } public function onSaved(): bool @@ -96,48 +95,26 @@ class FluidbookExternalInstallServer extends ExternalServer public static function verifyServerConnexion($id, $data = []) { - $server = self::find($id); - $protocol = $data['protocol'] ?? $server->protocol; - $usr = $data['username'] ?? $server->username; - $pwd = $data['password'] ?? $server->password; - $host = $data['host'] ?? $server->host; - - if($protocol !== "hosting") { - if(!$host) { - throw new \Exception('Host missing'); - } else { - try { - $connection = ssh2_connect($host, 22); - } catch (\Exception $e) { - - } - try { - $fingerprint = ssh2_fingerprint($connection, - SSH2_FINGERPRINT_MD5 | SSH2_FINGERPRINT_HEX); - } catch (\Exception $e) { - - } - - if(!$fingerprint) { - throw new \Exception('Fingerprint missing'); - } - - try { - self::$isValidConnexion = ssh2_auth_password($connection, $usr, $pwd); - } catch (\Exception $e) { - - } - ssh2_disconnect($connection); - } + $server = static::find($id); + $data = [ + 'username' => $data['username'] ?? $server->username, + 'password' => $data['password'] ?? $server->password, + 'host' => $data['host'] ?? $server->host, + 'port' => $data['port'] ?? $server->port ?? 22, + ]; + + $result = static::find($id)->getTransferDriver()->checkConnexion($data); + //dump($result); + if($result === true) { + $result_['success'] = true; } else { - self::$isValidConnexion = file_exists("/mnt/hosting/status"); + $result_['error'] = true; } - - return self::$isValidConnexion; + $result_['message'] = $result === true ? __("La connexion a été établie avec succès") : static::messages()[$result]; + return $result_; } - - public static function listFluidbook() + public static function listFluidbook($id) { /*return DB::table('fluidbook_external_install_server') ->join('fluidbook_publication', 'fluidbook_external_install_server.name', '=', 'fluidbook_publication.c_title') @@ -148,4 +125,14 @@ class FluidbookExternalInstallServer extends ExternalServer ->groupBy('fluidbook_external_install_server.host') ->toArray();*/ } + + public static function messages() { + return [ + __("L'hôte est manquant"), + __("L'hôte ou le mot de passe est invalide"), + __("La connexion a échouée"), + __("Le dossier n'a pas été monté"), + __("Le protocol est indisponible"), + ]; + } } diff --git a/resources/views/vendor/backpack/crud/edit-external-server.blade.php b/resources/views/vendor/backpack/crud/edit-external-server.blade.php index cd8afa1d8..b15eb674f 100644 --- a/resources/views/vendor/backpack/crud/edit-external-server.blade.php +++ b/resources/views/vendor/backpack/crud/edit-external-server.blade.php @@ -16,7 +16,7 @@ function removeBtn() { setTimeout(() => { $("#message").remove() - }, "3000"); + }, "5000"); } $(document).on("click", "#verifyconnection", function(e) { @@ -42,8 +42,9 @@ $('#saveActions').prepend('
'+data.message+'
') removeBtn() }, - error: function(data) { - $('#saveActions').prepend('
{{ __('Une erreur est survenue') }}
') + error: function(xhr) { + let message = xhr.responseJSON.message ?? "{{ __('Une erreur est survenue') }}" + $('#saveActions').prepend('
'+message+'
') removeBtn() } }); -- 2.39.5