diff --git a/rest/index.php b/rest/index.php index b070c88b0b10cd58e6dffc90dacb02fa67988d7c..d663c662f8fbedbdbe09b56d4a33de63ef56bfe0 100755 --- a/rest/index.php +++ b/rest/index.php @@ -95,6 +95,9 @@ $app->put('/sortedBaskets/{id}', \Basket\controllers\BasketController::class . ' //BatchHistories $app->get('/batchHistories', \History\controllers\BatchHistoryController::class . ':get'); +//Configurations +$app->put('/configurations/{name}', \Configuration\controllers\ConfigurationController::class . ':update'); + //Contacts $app->post('/contacts', \Contact\controllers\ContactController::class . ':create'); $app->put('/contacts/{id}', \Contact\controllers\ContactController::class . ':update'); diff --git a/sql/develop.sql b/sql/develop.sql index b0f5f02aede8c4fbaf8b6f44e1dea7a8428067a6..cbfcb4c74757f3269baad9f87f04c3b9f06745d2 100755 --- a/sql/develop.sql +++ b/sql/develop.sql @@ -51,4 +51,4 @@ CONSTRAINT configuration_pkey PRIMARY KEY (id), CONSTRAINT configuration_unique_key UNIQUE (name) ) WITH (OIDS=FALSE); -INSERT INTO configuration (name, value) VALUES ('mailer', '{"type" : "smtp", "host" : ""}'); +INSERT INTO configuration (name, value) VALUES ('mailer', '{"type" : "smtp", "host" : "ssl://smtp.gmail.com", "port" : 465, "user" : "", "password" : "", "auth" : true, "secure" : "tls", "from" : "notifications@maarch.org"}'); diff --git a/src/app/configuration/controllers/ConfigurationController.php b/src/app/configuration/controllers/ConfigurationController.php new file mode 100644 index 0000000000000000000000000000000000000000..9404befa3ff08360dddc3603d144899f3db59136 --- /dev/null +++ b/src/app/configuration/controllers/ConfigurationController.php @@ -0,0 +1,67 @@ +<?php + +/** + * Copyright Maarch since 2008 under licence GPLv3. + * See LICENCE.txt file at the root folder for more details. + * This file is part of Maarch software. + * + */ + +/** + * @brief Configuration Controller + * @author dev@maarch.org + */ + +namespace Configuration\controllers; + +use Configuration\models\ConfigurationModel; +use Group\models\ServiceModel; +use Respect\Validation\Validator; +use Slim\Http\Request; +use Slim\Http\Response; + +class ConfigurationController +{ + public function update(Request $request, Response $response, array $aArgs) + { + if (!ServiceModel::hasService(['id' => 'admin_technical_configuration', 'userId' => $GLOBALS['userId'], 'location' => 'apps', 'type' => 'admin'])) { + return $response->withStatus(403)->withJson(['errors' => 'Service forbidden']); + } + + if (empty(ConfigurationModel::getByName(['name' => $aArgs['name']]))) { + return $response->withStatus(400)->withJson(['errors' => 'Configuration does not exist']); + } + + $data = $request->getParams(); + + if ($aArgs['name'] == 'mailer') { + $check = ConfigurationController::checkMailer($data); + if (!empty($check['errors'])) { + return $response->withStatus($check['code'])->withJson(['errors' => $check['errors']]); + } + } + + $data = json_encode($data); + ConfigurationModel::update(['set' => ['value' => $data], 'where' => ['name = ?'], 'data' => [$aArgs['name']]]); + + return $response->withJson(['success' => 'success']); + } + + private static function checkMailer(array $aArgs) + { + if ($aArgs['mode'] != 'sendmail') { + $check = Validator::stringType()->notEmpty()->validate($aArgs['host']); + $check = $check && Validator::intVal()->notEmpty()->validate($aArgs['port']); + $check = $check && Validator::stringType()->notEmpty()->validate($aArgs['user']); + $check = $check && Validator::stringType()->notEmpty()->validate($aArgs['password']); + $check = $check && Validator::boolType()->validate($aArgs['auth']); + $check = $check && Validator::stringType()->validate($aArgs['secure']); + $check = $check && Validator::stringType()->validate($aArgs['from']); + if (!$check) { + return ['errors' => "{$aArgs['mode']} configuration data is missing", 'code' => 400]; + } + } + + return ['success' => 'success']; + } +} diff --git a/src/app/configuration/models/ConfigurationModel.php b/src/app/configuration/models/ConfigurationModel.php new file mode 100644 index 0000000000000000000000000000000000000000..bf95946ea8e1a5305eadfb046c36ec61b9829a90 --- /dev/null +++ b/src/app/configuration/models/ConfigurationModel.php @@ -0,0 +1,56 @@ +<?php + +/** +* Copyright Maarch since 2008 under licence GPLv3. +* See LICENCE.txt file at the root folder for more details. +* This file is part of Maarch software. +* +*/ + +/** +* @brief Configuration Model +* @author dev@maarch.org +*/ + +namespace Configuration\models; + +use SrcCore\models\DatabaseModel; +use SrcCore\models\ValidatorModel; + +class ConfigurationModel +{ + public static function getByName(array $aArgs) + { + ValidatorModel::notEmpty($aArgs, ['name']); + ValidatorModel::stringType($aArgs, ['name']); + ValidatorModel::arrayType($aArgs, ['select']); + + $configuration = DatabaseModel::select([ + 'select' => empty($aArgs['select']) ? ['*'] : $aArgs['select'], + 'table' => ['configuration'], + 'where' => ['name = ?'], + 'data' => [$aArgs['name']], + ]); + + if (empty($configuration[0])) { + return []; + } + + return $configuration[0]; + } + + public static function update(array $aArgs) + { + ValidatorModel::notEmpty($aArgs, ['set', 'where', 'data']); + ValidatorModel::arrayType($aArgs, ['set', 'where', 'data']); + + DatabaseModel::update([ + 'table' => 'configuration', + 'set' => $aArgs['set'], + 'where' => $aArgs['where'], + 'data' => $aArgs['data'] + ]); + + return true; + } +} diff --git a/src/core/controllers/PreparedClauseController.php b/src/core/controllers/PreparedClauseController.php index e3bb354fa8aba9c9d694f073dae5218fdab32203..62fc92ad7329903a9b30d791a448168a1f24d914 100755 --- a/src/core/controllers/PreparedClauseController.php +++ b/src/core/controllers/PreparedClauseController.php @@ -208,7 +208,7 @@ class PreparedClauseController } } - return $clause; + return "({$clause})"; } public static function isRequestValid(array $aArgs)