From 45ef60474e46bacf1edc3397953b677b7604ceed Mon Sep 17 00:00:00 2001
From: "florian.azizian" <florian.azizian@maarch.org>
Date: Thu, 29 Nov 2018 11:58:20 +0100
Subject: [PATCH] FEAT #8840 create user from MC to MP

---
 rest/index.php                              |  1 +
 src/app/user/controllers/UserController.php | 59 +++++++++++++++++++++
 src/app/user/models/UserModelAbstract.php   | 18 +++++++
 src/core/lang/lang-en.php                   |  2 +
 src/core/lang/lang-fr.php                   |  2 +
 src/core/models/ValidatorModel.php          | 12 +++++
 6 files changed, 94 insertions(+)

diff --git a/rest/index.php b/rest/index.php
index eaa517ada83..9534a0ac7d9 100755
--- a/rest/index.php
+++ b/rest/index.php
@@ -285,6 +285,7 @@ $app->put('/users/{id}', \User\controllers\UserController::class . ':update');
 $app->put('/users/{id}/password', \User\controllers\UserController::class . ':resetPassword');
 $app->get('/users/{userId}/status', \User\controllers\UserController::class . ':getStatusByUserId');
 $app->put('/users/{id}/status', \User\controllers\UserController::class . ':updateStatus');
+$app->put('/users/{id}/maarchParapheur', \User\controllers\UserController::class . ':sendToMaarchParapheur');
 $app->delete('/users/{id}', \User\controllers\UserController::class . ':delete');
 $app->post('/users/{id}/groups', \User\controllers\UserController::class . ':addGroup');
 $app->put('/users/{id}/groups/{groupId}', \User\controllers\UserController::class . ':updateGroup');
diff --git a/src/app/user/controllers/UserController.php b/src/app/user/controllers/UserController.php
index b14b9e689d8..6f5b8cd5b63 100644
--- a/src/app/user/controllers/UserController.php
+++ b/src/app/user/controllers/UserController.php
@@ -39,6 +39,7 @@ use User\models\UserBasketPreferenceModel;
 use User\models\UserEntityModel;
 use User\models\UserModel;
 use User\models\UserSignatureModel;
+use SrcCore\models\CurlModel;
 
 class UserController
 {
@@ -245,6 +246,64 @@ class UserController
         return $response->withJson(['success' => 'success']);
     }
 
+    public function sendToMaarchParapheur(Request $request, Response $response, array $aArgs)
+    {
+        $error = $this->hasUsersRights(['id' => $aArgs['id']]);
+        if (!empty($error['error'])) {
+            return $response->withStatus($error['status'])->withJson(['errors' => $error['error']]);
+        }
+
+        $loadedXml = CoreConfigModel::getXmlLoaded(['path' => 'modules/visa/xml/remoteSignatoryBooks.xml']);
+
+        if ($loadedXml->signatoryBookEnabled == 'maarchParapheur') {
+            $userInfo = UserModel::getById(['select' => ['firstname', 'lastname', 'mail', 'external_id'], 'id' => $aArgs['id']]);
+
+            $bodyData = [
+                "lastname"  => $userInfo['lastname'],
+                "firstname" => $userInfo['firstname'],
+                "email"     => $userInfo['mail'],
+            ];
+
+            foreach ($loadedXml->signatoryBook as $value) {
+                if ($value->id == "maarchParapheur") {
+                    $url = $value->url;
+                    $userId = $value->userId;
+                    $password = $value->password;
+                    break;
+                }
+            }
+
+            $responseExec = CurlModel::exec([
+                'url'      => $url . '/rest/users',
+                'user'     => $userId,
+                'password' => $password,
+                'method'   => 'POST',
+                'bodyData' => $bodyData
+            ]);
+
+            if (!empty($responseExec['errors'])) {
+                return $response->withStatus(400)->withJson(['errors' => $responseExec['errors']]);
+            }
+        } else {
+            return $response->withStatus(401)->withJson(['errors' => 'maarchParapheur is not enabled']);
+        }
+
+        $externalId = json_decode($userInfo['external_id']);
+        $externalId->maarchParapheur = $responseExec['user']['id'];
+
+        UserModel::updateExternalId(['id' => $aArgs['id'], 'externalId' => json_encode($externalId)]);
+
+        HistoryController::add([
+            'tableName'    => 'users',
+            'recordId'     => $GLOBALS['userId'],
+            'eventType'    => 'ADD',
+            'eventId'      => 'userCreation',
+            'info'         => _USER_CREATED_IN_MAARCHPARAPHEUR . " {$userInfo['firstname']} {$userInfo['lastname']}"
+        ]);
+
+        return $response->withJson(['success' => 'success']);
+    }
+
     public function getProfile(Request $request, Response $response)
     {
         $user = UserModel::getByUserId(['userId' => $GLOBALS['userId'], 'select' => ['id', 'user_id', 'firstname', 'lastname', 'phone', 'mail', 'initials', 'thumbprint']]);
diff --git a/src/app/user/models/UserModelAbstract.php b/src/app/user/models/UserModelAbstract.php
index dcb5f77fa6d..1ca1f13984b 100644
--- a/src/app/user/models/UserModelAbstract.php
+++ b/src/app/user/models/UserModelAbstract.php
@@ -164,6 +164,24 @@ abstract class UserModelAbstract
         return $aUser;
     }
 
+    public static function updateExternalId(array $aArgs)
+    {
+        ValidatorModel::notEmpty($aArgs, ['id', 'externalId']);
+        ValidatorModel::intVal($aArgs, ['id']);
+        ValidatorModel::json($aArgs, ['externalId']);
+
+        DatabaseModel::update([
+            'table'     => 'users',
+            'set'       => [
+                'external_id' => $aArgs['externalId']
+            ],
+            'where'     => ['id = ?'],
+            'data'      => [$aArgs['id']]
+        ]);
+
+        return true;
+    }
+
     public static function updatePassword(array $aArgs)
     {
         ValidatorModel::notEmpty($aArgs, ['id', 'password']);
diff --git a/src/core/lang/lang-en.php b/src/core/lang/lang-en.php
index d1bd0745f9b..7ceb994b05d 100644
--- a/src/core/lang/lang-en.php
+++ b/src/core/lang/lang-en.php
@@ -418,3 +418,5 @@ define('_SVA', 'SVA');
 define('_SVR', 'SVR');
 define('_REDACTOR', 'Redactor');
 define('_ASSIGNEE', 'Assignee');
+
+define('_USER_CREATED_IN_MAARCHPARAPHEUR', 'Utilisateur créé dans Maarch Parapheur');
diff --git a/src/core/lang/lang-fr.php b/src/core/lang/lang-fr.php
index 348a64c2f22..cf656ba42fb 100644
--- a/src/core/lang/lang-fr.php
+++ b/src/core/lang/lang-fr.php
@@ -418,3 +418,5 @@ define('_SVA', 'SVA');
 define('_SVR', 'SVR');
 define('_REDACTOR', 'Rédacteur');
 define('_ASSIGNEE', 'Attributaire');
+
+define('_USER_CREATED_IN_MAARCHPARAPHEUR', 'Utilisateur créé dans Maarch Parapheur');
diff --git a/src/core/models/ValidatorModel.php b/src/core/models/ValidatorModel.php
index c090005b0c4..a6f51085c2e 100755
--- a/src/core/models/ValidatorModel.php
+++ b/src/core/models/ValidatorModel.php
@@ -42,6 +42,18 @@ class ValidatorModel
         }
     }
 
+    public static function json(array $aArgs, $aKeys)
+    {
+        foreach ($aKeys as $key) {
+            if (!isset($aArgs[$key])) {
+                continue;
+            }
+            if (!Validator::json()->validate($aArgs[$key])) {
+                throw new \Exception("Argument {$key} is not a json (value)");
+            }
+        }
+    }
+
     public static function intType(array $aArgs, $aKeys)
     {
         foreach ($aKeys as $key) {
-- 
GitLab