From a13d04857fe510ad3bb9204d94ea0102e39ae22c Mon Sep 17 00:00:00 2001 From: soufiane Date: Tue, 21 Nov 2023 23:47:49 +0100 Subject: [PATCH] wip #6477 @7:00 --- ...ookExternalInstallServerCrudController.php | 2 +- .../Admin/Operations/ServerOperation.php | 47 +++++++++++++++++++ app/Models/FluidbookExternalInstallServer.php | 31 ++++++++++++ .../crud/edit-external-server.blade.php | 43 +++++++++++++++++ 4 files changed, 122 insertions(+), 1 deletion(-) create mode 100644 app/Http/Controllers/Admin/Operations/ServerOperation.php create mode 100644 resources/views/vendor/backpack/crud/edit-external-server.blade.php diff --git a/app/Http/Controllers/Admin/FluidbookExternalInstallServerCrudController.php b/app/Http/Controllers/Admin/FluidbookExternalInstallServerCrudController.php index 36b55a502..e3b016947 100644 --- a/app/Http/Controllers/Admin/FluidbookExternalInstallServerCrudController.php +++ b/app/Http/Controllers/Admin/FluidbookExternalInstallServerCrudController.php @@ -13,7 +13,7 @@ class FluidbookExternalInstallServerCrudController extends \Cubist\Backpack\Magi use \Backpack\CRUD\app\Http\Controllers\Operations\DeleteOperation; use \Backpack\CRUD\app\Http\Controllers\Operations\BulkDeleteOperation; use \Cubist\Backpack\Http\Controllers\Operations\ReviseOperation; - + /* diff --git a/app/Http/Controllers/Admin/Operations/ServerOperation.php b/app/Http/Controllers/Admin/Operations/ServerOperation.php new file mode 100644 index 000000000..bec1a1783 --- /dev/null +++ b/app/Http/Controllers/Admin/Operations/ServerOperation.php @@ -0,0 +1,47 @@ +crud->setEditView('vendor.backpack.crud.edit-external-server'); + } + + protected function run(Request $request, $id) { + $validation = [ + 'password' => 'required|string|min:8', + 'protocol' => 'required|string|max:255', + 'username' => 'required|string|max:255', + 'port' => 'nullable|numeric', + ]; + $validator = Validator::make($request->all(), $validation); + + if ($validator->fails()) { + throw new ValidationException($validator); + } + + $validator->validate(); + + $result = FluidbookExternalInstallServer::verifyServerConnexion($id, $validator); + if($result) { + Alert::add('success', __('La connexion a été établie avec succès'))->flash(); + } else { + Alert::add('error', __('La connexion a échouée'))->flash(); + } + + die(); + } +} diff --git a/app/Models/FluidbookExternalInstallServer.php b/app/Models/FluidbookExternalInstallServer.php index 689556394..95d457d21 100644 --- a/app/Models/FluidbookExternalInstallServer.php +++ b/app/Models/FluidbookExternalInstallServer.php @@ -2,6 +2,7 @@ namespace App\Models; +use App\Http\Controllers\Admin\Operations\ServerOperation; use Cubist\Backpack\Magic\Fields\Checkbox; use Cubist\Backpack\Magic\Fields\Table; use Cubist\Backpack\Magic\Fields\Text; @@ -18,8 +19,11 @@ class FluidbookExternalInstallServer extends ExternalServer 'singular' => 'serveur', 'plural' => 'serveurs']; + protected static $isValidConnexion = false; + protected static $_permissionBase = 'fluidbook-external-install-server'; + protected $_operations = [ServerOperation::class]; protected static function _getOneServer($server) { @@ -85,4 +89,31 @@ 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 { + $connection = ssh2_connect($host, 22); + try { + self::$isValidConnexion = ssh2_auth_password($connection, $usr, $pwd); + } catch (\Exception $e) { + + } + ssh2_disconnect($connection); + } + } else { + self::$isValidConnexion = file_exists("/mnt/hosting/status"); + } + + return self::$isValidConnexion; + } + } diff --git a/resources/views/vendor/backpack/crud/edit-external-server.blade.php b/resources/views/vendor/backpack/crud/edit-external-server.blade.php new file mode 100644 index 000000000..902c9e7e4 --- /dev/null +++ b/resources/views/vendor/backpack/crud/edit-external-server.blade.php @@ -0,0 +1,43 @@ +@push('crud_fields_scripts') + +@endpush + +@include('cubist_back::when') +@include('crud::edit') -- 2.39.5