From 946468dc8159f0fb2f676fba607227e0164743ed Mon Sep 17 00:00:00 2001
From: Damien <damien.burel@maarch.org>
Date: Mon, 24 Jun 2019 17:26:39 +0200
Subject: [PATCH] FEAT #10886 TIME 0:30 Configurations label + connection

---
 sql/data_fr.sql                               |  6 ++---
 sql/structure.sql                             |  1 +
 .../controllers/ConfigurationController.php   | 23 ++++++++++++++-----
 .../models/ConfigurationModel.php             |  5 ++--
 4 files changed, 24 insertions(+), 11 deletions(-)

diff --git a/sql/data_fr.sql b/sql/data_fr.sql
index 363e54b5f2..b6e6ffe036 100755
--- a/sql/data_fr.sql
+++ b/sql/data_fr.sql
@@ -66,6 +66,6 @@ INSERT INTO password_rules (label, "value") VALUES ('renewal', 90);
 -- CONFIGURATIONS
 -----
 TRUNCATE TABLE configurations;
-INSERT INTO configurations (identifier, value) VALUES ('emailServer', '{"type" : "smtp", "host" : "smtp.gmail.com", "port" : 465, "user" : "", "password" : "", "auth" : true, "secure" : "ssl", "from" : "notifications@maarch.org", "charset" : "utf-8"}');
-INSERT INTO configurations (identifier, value) VALUES ('ldapServer', '{"uri" : "10.2.95.60", "prefix" : "MAARCH", "suffix" : "", "ssl" : false, "baseDN" : ""}');
-INSERT INTO configurations (identifier, value) VALUES ('connection', '"default"');
+INSERT INTO configurations (identifier, label, value) VALUES ('emailServer', 'Configuration Courriel', '{"type" : "smtp", "host" : "smtp.gmail.com", "port" : 465, "user" : "", "password" : "", "auth" : true, "secure" : "ssl", "from" : "notifications@maarch.org", "charset" : "utf-8"}');
+INSERT INTO configurations (identifier, label, value) VALUES ('ldapServer', 'Configuration serveur LDAP #1', '{"uri" : "10.2.95.60", "prefix" : "MAARCH", "suffix" : "", "ssl" : false, "baseDN" : ""}');
+INSERT INTO configurations (identifier, label, value) VALUES ('connection', 'Méthode de connexion', '"default"');
diff --git a/sql/structure.sql b/sql/structure.sql
index 6e0f0f0cfc..06109de210 100755
--- a/sql/structure.sql
+++ b/sql/structure.sql
@@ -56,6 +56,7 @@ CREATE TABLE configurations
 (
 id serial NOT NULL,
 identifier CHARACTER VARYING (64) NOT NULL,
+label text NOT NULL,
 value jsonb DEFAULT '{}' NOT NULL,
 CONSTRAINT configuration_pkey PRIMARY KEY (id)
 )
diff --git a/src/app/configuration/controllers/ConfigurationController.php b/src/app/configuration/controllers/ConfigurationController.php
index bf2cefceb7..afb36217d6 100755
--- a/src/app/configuration/controllers/ConfigurationController.php
+++ b/src/app/configuration/controllers/ConfigurationController.php
@@ -40,7 +40,12 @@ class ConfigurationController
             return $response->withStatus(403)->withJson(['errors' => 'Privilege forbidden']);
         }
 
-        $configurations = ConfigurationModel::getByIdentifier(['identifier' => $queryParams['identifier']]);
+        $configurations = ConfigurationModel::getByIdentifier(['identifier' => $queryParams['identifier'], 'select' => ['id', 'label']]);
+        if ($queryParams['identifier'] == 'connection') {
+            $ldapConfigurations = ConfigurationModel::getByIdentifier(['identifier' => 'ldapServer', 'select' => [1]]);
+            $configurations = $configurations[0];
+            $configurations['availableConnections'] = [['id' => 'default', 'allowed' => true], ['id' => 'ldap', 'allowed' => !empty($ldapConfigurations)]];
+        }
 
         return $response->withJson(['configurations' => $configurations]);
     }
@@ -59,6 +64,8 @@ class ConfigurationController
             return $response->withStatus(403)->withJson(['errors' => 'Privilege forbidden']);
         }
 
+        $configuration['value'] = json_decode($configuration['value'], true);
+
         return $response->withJson(['configuration' => $configuration]);
     }
 
@@ -70,6 +77,8 @@ class ConfigurationController
             return $response->withStatus(400)->withJson(['errors' => 'Body is not set or empty']);
         } elseif (!Validator::stringType()->notEmpty()->length(1, 64)->validate($body['identifier'])) {
             return $response->withStatus(400)->withJson(['errors' => 'Body identifier is empty, not a string or longer than 64']);
+        } elseif (!Validator::stringType()->notEmpty()->validate($body['label'])) {
+            return $response->withStatus(400)->withJson(['errors' => 'Body label is empty or not a string']);
         } elseif (!Validator::arrayType()->notEmpty()->validate($body['value'])) {
             return $response->withStatus(400)->withJson(['errors' => 'Body value is empty or not an array']);
         }
@@ -124,14 +133,14 @@ class ConfigurationController
             return $response->withStatus(403)->withJson(['errors' => 'Privilege forbidden']);
         }
 
-        $id = ConfigurationModel::create(['identifier' => $body['identifier'], 'value' => $data]);
+        $id = ConfigurationModel::create(['identifier' => $body['identifier'], 'label' => $body['label'], 'value' => $data]);
 
         HistoryController::add([
             'code'          => 'OK',
             'objectType'    => 'configurations',
             'objectId'      => $id,
             'type'          => 'CREATION',
-            'message'       => '{configurationAdded}',
+            'message'       => "{configurationAdded} : {$body['label']}",
             'data'          => ['identifier' => $body['identifier']]
         ]);
 
@@ -146,6 +155,8 @@ class ConfigurationController
             return $response->withStatus(400)->withJson(['errors' => 'Route id is not an integer']);
         } elseif (empty($body)) {
             return $response->withStatus(400)->withJson(['errors' => 'Body is not set or empty']);
+        } elseif (!Validator::stringType()->notEmpty()->validate($body['label'])) {
+            return $response->withStatus(400)->withJson(['errors' => 'Body label is empty or not a string']);
         } elseif (!Validator::arrayType()->notEmpty()->validate($body['value'])) {
             return $response->withStatus(400)->withJson(['errors' => 'Body value is empty or not an array']);
         }
@@ -231,14 +242,14 @@ class ConfigurationController
             return $response->withStatus(403)->withJson(['errors' => 'Privilege forbidden']);
         }
 
-        ConfigurationModel::update(['set' => ['value' => $data], 'where' => ['id = ?'], 'data' => [$args['id']]]);
+        ConfigurationModel::update(['set' => ['label' => $body['label'], 'value' => $data], 'where' => ['id = ?'], 'data' => [$args['id']]]);
 
         HistoryController::add([
             'code'          => 'OK',
             'objectType'    => 'configurations',
             'objectId'      => $args['id'],
             'type'          => 'MODIFICATION',
-            'message'       => '{configurationUpdated}',
+            'message'       => "{configurationUpdated} : {$body['label']}",
             'data'          => ['identifier' => $configuration['identifier']]
         ]);
 
@@ -271,7 +282,7 @@ class ConfigurationController
             'objectType'    => 'configurations',
             'objectId'      => $args['id'],
             'type'          => 'SUPPRESSION',
-            'message'       => '{configurationDeleted}',
+            'message'       => "{configurationDeleted} : {$configuration['label']}",
             'data'          => ['identifier' => $configuration['identifier']]
         ]);
 
diff --git a/src/app/configuration/models/ConfigurationModel.php b/src/app/configuration/models/ConfigurationModel.php
index 95926d59cc..bf1caecf86 100755
--- a/src/app/configuration/models/ConfigurationModel.php
+++ b/src/app/configuration/models/ConfigurationModel.php
@@ -57,8 +57,8 @@ class ConfigurationModel
 
     public static function create(array $args)
     {
-        ValidatorModel::notEmpty($args, ['identifier', 'value']);
-        ValidatorModel::stringType($args, ['identifier', 'value']);
+        ValidatorModel::notEmpty($args, ['identifier', 'label', 'value']);
+        ValidatorModel::stringType($args, ['identifier', 'label', 'value']);
 
         $nextSequenceId = DatabaseModel::getNextSequenceValue(['sequenceId' => 'users_id_seq']);
 
@@ -67,6 +67,7 @@ class ConfigurationModel
             'columnsValues' => [
                 'id'            => $nextSequenceId,
                 'identifier'    => $args['identifier'],
+                'label'         => $args['label'],
                 'value'         => $args['value']
             ]
         ]);
-- 
GitLab