From 8f4da0dc7b4417f3118ae02dbfc26b3a5c596bf1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Timoth=C3=A9e=20Jaussoin?= Date: Mon, 16 Jun 2025 14:33:41 +0000 Subject: [PATCH] Fix FLEXIAPI-332 Check if the first line was untouched and that the number of... --- .../Admin/AccountImportController.php | 23 +++++++++++++++++-- flexiapi/lang/fr.json | 2 +- flexiapi/public/accounts_example.csv | 2 +- .../admin/account/import/create.blade.php | 2 +- 4 files changed, 24 insertions(+), 5 deletions(-) diff --git a/flexiapi/app/Http/Controllers/Admin/AccountImportController.php b/flexiapi/app/Http/Controllers/Admin/AccountImportController.php index 651f816..3b14223 100644 --- a/flexiapi/app/Http/Controllers/Admin/AccountImportController.php +++ b/flexiapi/app/Http/Controllers/Admin/AccountImportController.php @@ -55,9 +55,23 @@ class AccountImportController extends Controller 'domain' => 'required|exists:accounts' ]); - $lines = $this->csvToCollection($request->file('csv')); $domain = $request->get('domain'); + /** + * General formating checking + */ + $csv = fopen($request->file('csv'), 'r'); + $line = fgets($csv); + fclose($csv); + + $lines = collect(); + $this->errors['Wrong file format'] = "The number of columns doesn't matches the reference file. The first MUST be the same as the reference file"; + + if ($line == "Username,Password,Role,Status,Phone,Email,External Username,External Domain,External Password,External Realm, External Registrar,External Outbound Proxy,External Protocol\n") { + $lines = $this->csvToCollection($request->file('csv')); + unset($this->errors['Wrong file format']); + } + /** * Error checking */ @@ -314,6 +328,12 @@ class AccountImportController extends Controller $i = 1; while (!feof($csv)) { if ($line = fgetcsv($csv, 1000, ',')) { + if (count($line) != 13) { + $this->errors['Parsing error at line ' . $i] = 'The number of columns is incorrect'; + $i++; + continue; + } + $lines->push((object)[ 'line' => $i, 'username' => !empty($line[0]) ? $line[0] : null, @@ -329,7 +349,6 @@ class AccountImportController extends Controller 'external_registrar' => !empty($line[10]) ? $line[10] : null, 'external_outbound_proxy' => !empty($line[11]) ? $line[11] : null, 'external_protocol' => $line[12], - ]); $i++; diff --git a/flexiapi/lang/fr.json b/flexiapi/lang/fr.json index 33554f5..fe817a6 100644 --- a/flexiapi/lang/fr.json +++ b/flexiapi/lang/fr.json @@ -189,7 +189,7 @@ "The code is not valid": "Le code n'est pas valide", "We will send you a verification code to recover your account.": "Nous vous enverrons un code de vérification pour récupérer votre compte.", "The contact doesn't exists": "Le contact n'existe pas", - "The file must be in CSV following this template": "Le fichier doit être au format CSV et respecter le modèle suivant", + "The file MUST be in CSV following this template": "Le fichier doit être au format CSV et respecter le modèle suivant", "The first line contains the labels": "La premières ligne contient les étiquettes", "The link can only be visited once": "Le lien ne peut être utilisé qu'une fois", "Third-party SIP account": "Compte SIP tiers", diff --git a/flexiapi/public/accounts_example.csv b/flexiapi/public/accounts_example.csv index a29ac6a..68ace44 100644 --- a/flexiapi/public/accounts_example.csv +++ b/flexiapi/public/accounts_example.csv @@ -1,4 +1,4 @@ -Username,Password,Role,Status,Phone,Email,External Username,External Domain,External Password,External Realm, External Registrar,External Outbound Proxy,External Encrypted,External Protocol +Username,Password,Role,Status,Phone,Email,External Username,External Domain,External Password,External Realm, External Registrar,External Outbound Proxy,External Protocol john,number9,user,active,+12341234,john@lennon.com,extjohn,ext.lennon.com,123ext,,,,UDP paul,a_day_in_the_life,admin,active,,paul@apple.com,,,,,,, ringo,allUneedIsL3ve,user,inactive,+123456,ringo@star.co.uk,extringo,ext.star.co.uk,123456,another.realm,,,UDP \ No newline at end of file diff --git a/flexiapi/resources/views/admin/account/import/create.blade.php b/flexiapi/resources/views/admin/account/import/create.blade.php index d02f262..be26bd9 100644 --- a/flexiapi/resources/views/admin/account/import/create.blade.php +++ b/flexiapi/resources/views/admin/account/import/create.blade.php @@ -18,7 +18,7 @@
  • {{ __('Import') }}
  • -

    {{ __('The file must be in CSV following this template') }}: example_template.csv

    +

    {{ __('The file MUST be in CSV following this template') }}: example_template.csv

    {{ __('User') }}

    {{ __('The first line contains the labels') }}