From 21cec662fdab992ceb6d356b7bf625e36c3be43e Mon Sep 17 00:00:00 2001
From: "florian.azizian" <florian.azizian@maarch.org>
Date: Mon, 15 Apr 2019 10:14:59 +0100
Subject: [PATCH] FEAT #9734 electronic signature if validate + separate cert
 and private key

---
 config/config.xml.default                     |  3 +-
 package.json                                  |  1 -
 .../controllers/DocumentController.php        | 35 ++++++++++---------
 3 files changed, 21 insertions(+), 18 deletions(-)

diff --git a/config/config.xml.default b/config/config.xml.default
index 771d3ba224..2bcc40879b 100755
--- a/config/config.xml.default
+++ b/config/config.xml.default
@@ -15,7 +15,8 @@
     </database>
     <electronicSignature>
         <enable>false</enable>
-        <certPath>/home/maarch/Bureau/tcpdf.crt</certPath>
+        <certPath>/home/maarch/Bureau/tcpdf.crt</certPath><!-- .crt or .p12 or .pem -->
+        <privateKeyPath>/home/maarch/Bureau/tcpdf.crt</privateKeyPath><!-- if private key and certificate are in the same file, put the same path -->
         <password>password</password>
         <certInfo>
             <name>Maarch</name>
diff --git a/package.json b/package.json
index ce432c1424..4703533209 100644
--- a/package.json
+++ b/package.json
@@ -37,7 +37,6 @@
     "ngx-cookie-service": "^2.1.0",
     "ngx-scroll-event": "^1.0.8",
     "pdfjs-dist": "^2.0.943",
-    "rxjs": "^6.4.0",
     "simple-pdf-viewer": "^2.0.3",
     "zone.js": "~0.8.29"
   },
diff --git a/src/app/document/controllers/DocumentController.php b/src/app/document/controllers/DocumentController.php
index b551ac6a9d..15758594a8 100755
--- a/src/app/document/controllers/DocumentController.php
+++ b/src/app/document/controllers/DocumentController.php
@@ -261,7 +261,6 @@ class DocumentController
 
         $processingUser['preferences'] = json_decode($processingUser['preferences'], true);
         if ($processingUser['preferences']['notifications']) {
-
             $lang = LangController::get(['lang' => $processingUser['preferences']['lang']]);
             $url = UrlController::getCoreUrl() . 'dist/index.html#/documents/' . $id;
             EmailController::createEmail([
@@ -377,21 +376,25 @@ class DocumentController
                 }
             }
 
-            $loadedXml = CoreConfigModel::getConfig();
-
-            if ($loadedXml->electronicSignature->enable == 'true') {
-                $certPath = realpath((string)$loadedXml->electronicSignature->certPath);
-                if (is_file($certPath)) {
-                    $certificate = 'file://' . $certPath;
-                    $info = [
-                        'Name'        => (string)$loadedXml->electronicSignature->certInfo->name,
-                        'Location'    => (string)$loadedXml->electronicSignature->certInfo->location,
-                        'Reason'      => (string)$loadedXml->electronicSignature->certInfo->reason,
-                        'ContactInfo' => (string)$loadedXml->electronicSignature->certInfo->contactInfo
-                    ];
-                    $pdf->setSignature($certificate, $certificate, (string)$loadedXml->electronicSignature->password, '', 2, $info);
-                } else {
-                    return $response->withStatus(400)->withJson(['errors' => 'check certPath']);
+            $status = StatusModel::getById(['select' => ['reference'], 'id' => $action['next_status_id']]);
+            if ($status['reference'] == 'VAL' && $document['mode'] == 'SIGN') {
+                $loadedXml = CoreConfigModel::getConfig();
+                if ($loadedXml->electronicSignature->enable == 'true') {
+                    $certPath       = realpath((string)$loadedXml->electronicSignature->certPath);
+                    $privateKeyPath = realpath((string)$loadedXml->electronicSignature->privateKeyPath);
+                    if (is_file($certPath) && is_file($privateKeyPath)) {
+                        $certificate = 'file://' . $certPath;
+                        $privateKey = 'file://' . $privateKeyPath;
+                        $info = [
+                            'Name'        => (string)$loadedXml->electronicSignature->certInfo->name,
+                            'Location'    => (string)$loadedXml->electronicSignature->certInfo->location,
+                            'Reason'      => (string)$loadedXml->electronicSignature->certInfo->reason,
+                            'ContactInfo' => (string)$loadedXml->electronicSignature->certInfo->contactInfo
+                        ];
+                        $pdf->setSignature($certificate, $privateKey, (string)$loadedXml->electronicSignature->password, '', 2, $info);
+                    } else {
+                        return $response->withStatus(400)->withJson(['errors' => 'certPath or privateKeyPath is not valid']);
+                    }
                 }
             }
 
-- 
GitLab