diff --git a/sql/data_fr.sql b/sql/data_fr.sql index a0f45dba007f0b13efbba06fc3fabf84677caf74..d5c2d7aaad844afbaa1eefd062313130f75d6bb2 100755 --- a/sql/data_fr.sql +++ b/sql/data_fr.sql @@ -65,4 +65,5 @@ INSERT INTO password_rules (label, "value") VALUES ('renewal', 90); ----- 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"}'); -ALTER SEQUENCE configurations_id_seq RESTART WITH 2; +INSERT INTO configurations (identifier, value) VALUES ('ldapServer', '[{"uri" : "10.2.95.60", "prefix" : "MAARCH", "ssl" : false}]'); +INSERT INTO configurations (identifier, value) VALUES ('connection', '{"standard" : true, "ldap" : false}'); diff --git a/sql/structure.sql b/sql/structure.sql index e0ea8535263c60c41632ba0cd6929d8ddb574efb..0e5675edbbcbd0155dc12aa9c38535943fedf472 100755 --- a/sql/structure.sql +++ b/sql/structure.sql @@ -56,7 +56,7 @@ CREATE TABLE configurations ( id serial NOT NULL, identifier CHARACTER VARYING (64) NOT NULL, -value json DEFAULT '{}' NOT NULL, +value jsonb DEFAULT '{}' NOT NULL, CONSTRAINT configuration_pkey PRIMARY KEY (id), CONSTRAINT configuration_unique_key UNIQUE (identifier) ) diff --git a/src/app/configuration/models/ConfigurationModel.php b/src/app/configuration/models/ConfigurationModel.php index 56c09e524f9d6867a491c635b714b5f314d50f9a..ea2fafc7caf4d57f715651a2fbcbc95f6d714972 100755 --- a/src/app/configuration/models/ConfigurationModel.php +++ b/src/app/configuration/models/ConfigurationModel.php @@ -69,4 +69,27 @@ class ConfigurationModel return true; } + + public static function getConnection() + { + $configuration = DatabaseModel::select([ + 'select' => ['value'], + 'table' => ['configurations'], + 'where' => ['identifier = ?'], + 'data' => ['connection'] + ]); + + if (empty($configuration[0])) { + return 'standard'; + } + + $connections = json_decode($configuration[0]['value'], true); + foreach ($connections as $key => $connection) { + if ($connection) { + return $key; + } + } + + return 'standard'; + } } diff --git a/src/core/controllers/AuthenticationController.php b/src/core/controllers/AuthenticationController.php index d409932fe687c2387d8e988ad36e09e992a63e58..8bb4906328821aabd4e417bbed54061d39c38439 100755 --- a/src/core/controllers/AuthenticationController.php +++ b/src/core/controllers/AuthenticationController.php @@ -14,6 +14,7 @@ namespace SrcCore\controllers; +use Configuration\models\ConfigurationModel; use History\controllers\HistoryController; use Respect\Validation\Validator; use Slim\Http\Request; @@ -53,7 +54,32 @@ class AuthenticationController return $response->withStatus(400)->withJson(['errors' => 'Bad Request']); } - if (!AuthenticationModel::authentication(['login' => $body['login'], 'password' => $body['password']])) { + $connection = ConfigurationModel::getConnection(); + if ($connection == 'ldap') { + $ldapConfigurations = ConfigurationModel::getByIdentifier(['identifier' => 'ldapServer', 'select' => ['value']]); + if (empty($ldapConfigurations)) { + return $response->withStatus(400)->withJson(['errors' => 'Ldap configuration is missing']); + } + $ldapConfigurations = json_decode($ldapConfigurations['value'], true); + foreach ($ldapConfigurations as $ldapConfiguration) { + $uri = ($ldapConfiguration['ssl'] === true ? "LDAPS://{$ldapConfiguration['uri']}" : $ldapConfiguration['uri']); + $ldap = ldap_connect($uri); + if ($ldap !== false) { + break; + } + } + if (empty($ldap)) { + return $response->withStatus(400)->withJson(['errors' => 'Ldap connection failed']); + } + ldap_set_option($ldap, LDAP_OPT_PROTOCOL_VERSION, 3); + ldap_set_option($ldap, LDAP_OPT_REFERRALS, 0); + $login = (!empty($ldapConfiguration['prefix']) ? $ldapConfiguration['prefix'] . '\\' . $body['login'] : $body['login']); + $authenticated = @ldap_bind($ldap, $login, $body['password']); + } else { + $authenticated = AuthenticationModel::authentication(['login' => $body['login'], 'password' => $body['password']]); + } + + if (!$authenticated) { return $response->withStatus(401)->withJson(['errors' => 'Authentication Failed']); }