]> _ Git - fluidbook-toolbox.git/commitdiff
wip #6555 @0.75
authorVincent Vanwaelscappel <vincent@cubedesigners.com>
Tue, 5 Dec 2023 16:04:54 +0000 (17:04 +0100)
committerVincent Vanwaelscappel <vincent@cubedesigners.com>
Tue, 5 Dec 2023 16:04:54 +0000 (17:04 +0100)
app/Http/Controllers/Admin/Operations/ServerOperation.php
app/Models/FluidbookExternalInstallServer.php

index 9cfc842126acef0a43da61b5953531e42c1087b9..50003467c3255b0cd8c8b559c49ed43ee08cf209 100644 (file)
@@ -17,33 +17,15 @@ trait ServerOperation
         Route::post($segment . '/{id}/verifyconnection', $controller . '@run');
     }
 
-    public function setup() {
+    public function setup()
+    {
         parent::setup();
         $this->crud->setEditView('vendor.backpack.crud.edit-external-server');
     }
 
-    protected function run(Request $request, $id) {
-        $validation = [
-            'password' => $request->protocol === "hosting" ? 'nullable' : 'required|string|min:8',
-            'username' => $request->protocol === "hosting" ? 'nullable' : 'required|string|max:255',
-            'host' => $request->protocol === "hosting" ? 'nullable' : 'required|string|max:255',
-            'port' => 'nullable|numeric',
-        ];
-        $validator = Validator::make([
-            'password' => $request->password,
-            '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);
-        }
-
-        $validateData = $validator->validate();
-
-        $result = FluidbookExternalInstallServer::verifyServerConnexion($id, $validateData);
-
+    protected function run(Request $request, $id)
+    {
+        $result = FluidbookExternalInstallServer::verifyServerConnexion($request->toArray());
         return response()->json($result);
     }
 }
index ae26473232d00963501d00c8c5384b4db2cc54ff..ca9b2e963f4891065596013c676122734660b9a2 100644 (file)
@@ -7,11 +7,13 @@ use Cubist\Backpack\Magic\Fields\Checkbox;
 use Cubist\Backpack\Magic\Fields\Table;
 use Cubist\Backpack\Magic\Fields\Textarea;
 use Cubist\Backpack\Magic\Models\ExternalServer;
+use Cubist\Net\Transfer\IServer;
 use Cubist\Net\Transfer\Local;
 use Cubist\Util\Files\Files;
 use Cubist\Util\Json;
 use Cubist\Util\Text;
 use Illuminate\Support\Facades\DB;
+use Illuminate\Support\Facades\Validator;
 
 // __('!! Serveurs externes')
 class FluidbookExternalInstallServer extends ExternalServer
@@ -30,7 +32,7 @@ class FluidbookExternalInstallServer extends ExternalServer
 
     protected static $hostingBasePaths = [
         'hosting' => '/mnt/hosting/',
-        'hosting2' => '/mnt/hosting2',
+        'hosting2' => '/mnt/hosting2/',
         'ushosting' => '/application/usstorage/hosting/'
     ];
 
@@ -43,12 +45,13 @@ class FluidbookExternalInstallServer extends ExternalServer
 
     public function setFields()
     {
-        $nothostingProtocols = array_diff(array_keys($this->getProtocols(), $this->getHostingProtocols()));
+        $hostingProtocols = static::getHostingProtocols();
+        $nothostingProtocols = array_diff(array_keys($this->getProtocols(), $hostingProtocols));
 
         parent::setFields();
         $this->getField('base_url')->setAttribute('when', ['protocol' => $nothostingProtocols]);
-        $this->addField('subdomains', Textarea::class, __('Sous-domaines'), ['when' => ['protocol' => $this->getHostingProtocols()]]);
-        $this->addField('php', Checkbox::class, __('Activer le support de PHP'), ['default' => false, 'when' => ['protocol' => $this->getHostingProtocols()]]);
+        $this->addField('subdomains', Textarea::class, __('Sous-domaines'), ['when' => ['protocol' => $hostingProtocols]]);
+        $this->addField('php', Checkbox::class, __('Activer le support de PHP'), ['default' => false, 'when' => ['protocol' => $hostingProtocols]]);
         $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]);
     }
@@ -67,7 +70,7 @@ class FluidbookExternalInstallServer extends ExternalServer
 
     protected function isHosting()
     {
-        return in_array($this->getProtocol(), $this->getHostingProtocols());
+        return static::isHostingProtocol($this->getProtocol());
     }
 
     public function getBaseURL()
@@ -126,7 +129,7 @@ class FluidbookExternalInstallServer extends ExternalServer
         return ['FTP' => __('FTP non sécurisé'), 'SFTP' => 'SFTP', 'hosting' => 'Hosting', 'hosting2' => 'Hosting #2', 'ushosting' => 'US Hosting'];
     }
 
-    protected function getHostingProtocols()
+    protected static function getHostingProtocols()
     {
         return array_keys(static::$hostingBasePaths);
     }
@@ -145,28 +148,43 @@ class FluidbookExternalInstallServer extends ExternalServer
 
     protected function getHostingBasePath()
     {
-
         return static::$hostingBasePaths[$this->getProtocol()];
     }
 
-    public static function verifyServerConnexion($id, $data = [])
+    public static function verifyServerConnexion(array $data)
     {
-        $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,
+        $isHosting = static::isHostingProtocol($data['protocol']);
+
+        $validation = [
+            'password' => $isHosting ? 'nullable' : 'required|string|min:8',
+            'username' => $isHosting ? 'nullable' : 'required|string|max:255',
+            'host' => $isHosting ? 'nullable' : 'required|string|max:255',
+            'port' => 'nullable|numeric',
         ];
+        $validator = Validator::make([
+            'password' => $data['password'],
+            'username' => $data['username'],
+            'host' => $data['host'],
+            'port' => $data['port']], $validation);
+
+        if ($validator->fails()) {
+            return ['error' => true, 'message' => __('Un ou plusieurs champs sont invalides') . ': ' . implode(', ', array_keys($validator->errors()->toArray()))];
+        }
 
-        $result = static::find($id)->getTransferDriver()->checkConnexion($data);
-        if ($result === true) {
-            $result_['success'] = true;
+        $server = new static();
+        $server->setRawAttributes($data);
+        $check = $server->getTransferDriver()->checkConnexion($data);
+
+        $res = [];
+
+        if ($check === true) {
+            $res['success'] = true;
+            $res['message'] = __("La connexion a été établie avec succès");
         } else {
-            $result_['error'] = true;
+            $res['error'] = true;
+            $res['message'] = static::messages()[$check];
         }
-        $result_['message'] = $result === true ? __("La connexion a été établie avec succès") : static::messages()[$result];
-        return $result_;
+        return $res;
     }
 
     public static function getListFluidbook($id)
@@ -184,7 +202,17 @@ class FluidbookExternalInstallServer extends ExternalServer
             __("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"),
+            __("Le protocole n'est pas supporté"),
         ];
     }
+
+
+    /**
+     * @param $protocol
+     * @return bool
+     */
+    public static function isHostingProtocol($protocol)
+    {
+        return in_array($protocol, static::getHostingProtocols());
+    }
 }