Skip to content
Snippets Groups Projects
Verified Commit 2cb34312 authored by Damien's avatar Damien
Browse files

FEAT #8882 Update email configuration

parent 2245e3e6
No related branches found
No related tags found
No related merge requests found
......@@ -4,6 +4,7 @@
"SrcCore\\" : "src/core/",
"Attachment\\" : "src/app/attachment/",
"Action\\" : "src/app/action/",
"Configuration\\" : "src/app/configuration/",
"Convert\\" : "src/app/convert/",
"Docserver\\" : "src/app/docserver/",
"Document\\" : "src/app/document/",
......
......@@ -53,6 +53,9 @@ $app->get('/actions', \Action\controllers\ActionController::class . ':get');
//Attachments
$app->get('/attachments/{id}', \Attachment\controllers\AttachmentController::class . ':getById');
//Configurations
$app->put('/configurations/{identifier}', \Configuration\controllers\ConfigurationController::class . ':update');
//Documents
$app->post('/documents', \Document\controllers\DocumentController::class . ':create');
$app->get('/documents', \Document\controllers\DocumentController::class . ':get');
......
......@@ -25,6 +25,7 @@ ALTER SEQUENCE groups_privileges_id_seq RESTART WITH 1;
INSERT INTO groups_privileges (group_id, privilege) VALUES (1, 'manage_rest_users');
INSERT INTO groups_privileges (group_id, privilege) VALUES (2, 'manage_users');
INSERT INTO groups_privileges (group_id, privilege) VALUES (2, 'manage_documents');
INSERT INTO groups_privileges (group_id, privilege) VALUES (2, 'manage_configuration');
TRUNCATE TABLE users_groups;
ALTER SEQUENCE users_groups_id_seq RESTART WITH 1;
......@@ -79,3 +80,8 @@ INSERT INTO password_rules (label, "value") VALUES ('lockTime', 5);
INSERT INTO password_rules (label, "value") VALUES ('historyLastUse', 2);
INSERT INTO password_rules (label, "value") VALUES ('renewal', 90);
-----
-- 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;
......@@ -216,3 +216,14 @@ CREATE TABLE password_history
CONSTRAINT password_history_pkey PRIMARY KEY (id)
)
WITH (OIDS=FALSE);
DROP TABLE IF EXISTS configurations;
CREATE TABLE configurations
(
id serial NOT NULL,
identifier CHARACTER VARYING (64) NOT NULL,
value json DEFAULT '{}' NOT NULL,
CONSTRAINT configuration_pkey PRIMARY KEY (id),
CONSTRAINT configuration_unique_key UNIQUE (identifier)
)
WITH (OIDS=FALSE);
<?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 Respect\Validation\Validator;
use Slim\Http\Request;
use Slim\Http\Response;
use SrcCore\models\AuthenticationModel;
use User\controllers\UserController;
class ConfigurationController
{
public function update(Request $request, Response $response, array $args)
{
if (!UserController::hasPrivilege(['userId' => $GLOBALS['id'], 'privilege' => 'manage_configuration'])) {
return $response->withStatus(403)->withJson(['errors' => 'Privilege forbidden']);
}
$body = $request->getParsedBody();
$configuration = ConfigurationModel::getByIdentifier(['identifier' => $args['identifier']]);
if ($args['identifier'] == 'emailServer') {
$check = ConfigurationController::checkMailer($body);
if (!empty($check['errors'])) {
return $response->withStatus(400)->withJson(['errors' => $check['errors']]);
}
if ($body['auth'] && empty($body['password']) && !empty($configuration)) {
$configuration['value'] = json_decode($configuration['value'], true);
if (!empty($configuration['value']['password'])) {
$body['password'] = $configuration['value']['password'];
}
} elseif ($body['auth'] && !empty($body['password'])) {
$body['password'] = AuthenticationModel::encrypt(['password' => $body['password']]);
}
$data = json_encode([
'type' => $body['type'],
'host' => $body['host'],
'port' => $body['port'],
'user' => empty($body['user']) ? null : $body['user'],
'password' => empty($body['password']) ? null : $body['password'],
'auth' => $body['auth'],
'secure' => $body['secure'],
'from' => $body['from'],
'charset' => empty($body['charset']) ? 'utf-8' : $body['charset']
]);
}
if (!empty($data)) {
if (empty($configuration)) {
ConfigurationModel::create(['identifier' => $args['identifier'], 'value' => $data]);
} else {
ConfigurationModel::update(['set' => ['value' => $data], 'where' => ['identifier = ?'], 'data' => [$args['identifier']]]);
}
}
return $response->withStatus(204);
}
private static function checkMailer(array $args)
{
if (!Validator::stringType()->notEmpty()->validate($args['type'])) {
return ['errors' => 'Body type is empty or not a string'];
}
if ($args['type'] == 'smtp') {
if (!Validator::stringType()->notEmpty()->validate($args['host'])) {
return ['errors' => 'Body host is empty or not a string'];
} elseif (!Validator::intVal()->notEmpty()->validate($args['port'])) {
return ['errors' => 'Body port is empty or not an integer'];
} elseif (!Validator::boolType()->validate($args['auth'])) {
return ['errors' => 'Body auth is empty or not a boolean'];
} elseif (!Validator::stringType()->notEmpty()->validate($args['secure'])) {
return ['errors' => 'Body secure is empty or not a string'];
} elseif (!Validator::stringType()->notEmpty()->validate($args['from'])) {
return ['errors' => 'Body from is empty or not a string'];
}
if ($args['auth']) {
if (!Validator::stringType()->notEmpty()->validate($args['user'])) {
return ['errors' => 'Body user is empty or not a string'];
}
}
}
return ['success' => 'success'];
}
}
<?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 getByIdentifier(array $aArgs)
{
ValidatorModel::notEmpty($aArgs, ['identifier']);
ValidatorModel::stringType($aArgs, ['identifier']);
ValidatorModel::arrayType($aArgs, ['select']);
$configuration = DatabaseModel::select([
'select' => empty($aArgs['select']) ? ['*'] : $aArgs['select'],
'table' => ['configurations'],
'where' => ['identifier = ?'],
'data' => [$aArgs['identifier']],
]);
if (empty($configuration[0])) {
return [];
}
return $configuration[0];
}
public static function create(array $args)
{
ValidatorModel::notEmpty($args, ['identifier', 'value']);
ValidatorModel::stringType($args, ['identifier', 'value']);
DatabaseModel::insert([
'table' => 'configurations',
'columnsValues' => [
'identifier' => $args['identifier'],
'value' => $args['value']
]
]);
return true;
}
public static function update(array $aArgs)
{
ValidatorModel::notEmpty($aArgs, ['set', 'where', 'data']);
ValidatorModel::arrayType($aArgs, ['set', 'where', 'data']);
DatabaseModel::update([
'table' => 'configurations',
'set' => $aArgs['set'],
'where' => $aArgs['where'],
'data' => $aArgs['data']
]);
return true;
}
}
......@@ -153,4 +153,33 @@ class AuthenticationModel
return true;
}
public static function encrypt(array $aArgs)
{
ValidatorModel::notEmpty($aArgs, ['password']);
ValidatorModel::stringType($aArgs, ['password']);
$encryptKey = CoreConfigModel::getEncryptKey();
$cipher_method = 'AES-128-CTR';
$enc_iv = openssl_random_pseudo_bytes(openssl_cipher_iv_length($cipher_method));
$cryptedPassword = openssl_encrypt($aArgs['password'], $cipher_method, $encryptKey, 0, $enc_iv) . "::" . bin2hex($enc_iv);
return $cryptedPassword;
}
public static function decrypt(array $aArgs)
{
ValidatorModel::notEmpty($aArgs, ['cryptedPassword']);
ValidatorModel::stringType($aArgs, ['cryptedPassword']);
$encryptKey = CoreConfigModel::getEncryptKey();
$cipher_method = 'AES-128-CTR';
list($crypted_token, $enc_iv) = explode("::", $aArgs['cryptedPassword']);
$password = openssl_decrypt($crypted_token, $cipher_method, $encryptKey, 0, hex2bin($enc_iv));
return $password;
}
}
......@@ -94,4 +94,17 @@ class CoreConfigModel
return $xmlfile;
}
public static function getEncryptKey()
{
if (!empty($_SERVER['MAARCH_ENCRYPT_KEY'])) {
$encriptKey = $_SERVER['MAARCH_ENCRYPT_KEY'];
} elseif (!empty($_SERVER['REDIRECT_MAARCH_ENCRYPT_KEY'])) {
$encriptKey = $_SERVER['REDIRECT_MAARCH_ENCRYPT_KEY'];
} else {
$encriptKey = "Security Key Maarch Courrier #2008";
}
return $encriptKey;
}
}
......@@ -279,7 +279,7 @@ class ClassLoader
*/
public function setApcuPrefix($apcuPrefix)
{
$this->apcuPrefix = function_exists('apcu_fetch') && ini_get('apc.enabled') ? $apcuPrefix : null;
$this->apcuPrefix = function_exists('apcu_fetch') && filter_var(ini_get('apc.enabled'), FILTER_VALIDATE_BOOLEAN) ? $apcuPrefix : null;
}
/**
......@@ -377,11 +377,11 @@ class ClassLoader
$subPath = $class;
while (false !== $lastPos = strrpos($subPath, '\\')) {
$subPath = substr($subPath, 0, $lastPos);
$search = $subPath.'\\';
$search = $subPath . '\\';
if (isset($this->prefixDirsPsr4[$search])) {
$pathEnd = DIRECTORY_SEPARATOR . substr($logicalPathPsr4, $lastPos + 1);
foreach ($this->prefixDirsPsr4[$search] as $dir) {
$length = $this->prefixLengthsPsr4[$first][$search];
if (file_exists($file = $dir . DIRECTORY_SEPARATOR . substr($logicalPathPsr4, $length))) {
if (file_exists($file = $dir . $pathEnd)) {
return $file;
}
}
......
......@@ -21,6 +21,7 @@ return array(
'Document\\' => array($baseDir . '/src/app/document'),
'Docserver\\' => array($baseDir . '/src/app/docserver'),
'Convert\\' => array($baseDir . '/src/app/convert'),
'Configuration\\' => array($baseDir . '/src/app/configuration'),
'Attachment\\' => array($baseDir . '/src/app/attachment'),
'Action\\' => array($baseDir . '/src/app/action'),
);
......@@ -56,6 +56,7 @@ class ComposerStaticInit637514d10f1ed5d4c55a005a428a3656
'C' =>
array (
'Convert\\' => 8,
'Configuration\\' => 14,
),
'A' =>
array (
......@@ -125,6 +126,10 @@ class ComposerStaticInit637514d10f1ed5d4c55a005a428a3656
array (
0 => __DIR__ . '/../..' . '/src/app/convert',
),
'Configuration\\' =>
array (
0 => __DIR__ . '/../..' . '/src/app/configuration',
),
'Attachment\\' =>
array (
0 => __DIR__ . '/../..' . '/src/app/attachment',
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment