diff --git a/rest/index.php b/rest/index.php index 51c418e3c10316f7195e59bcba0dcc3556cd0835..8e12d2431cd9ed725b507da36f3eb5a12ec88af4 100755 --- a/rest/index.php +++ b/rest/index.php @@ -99,6 +99,7 @@ $app->get('/documents/{id}/history', \History\controllers\HistoryController::cla $app->put('/documents/{id}/actions/{actionId}', \Document\controllers\DocumentController::class . ':setAction'); $app->get('/documents/{id}/workflow', \Workflow\controllers\WorkflowController::class . ':getByDocumentId'); $app->get('/documents/{id}/workflows/{workflowId}/files/{fileId}', \Workflow\controllers\YousignController::class . ':getByFileId'); +$app->get('/documents/{id}/workflows/{workflowId}/files/{fileId}/fastStatus/{fastStatus}', \Workflow\controllers\FastOTPController::class . ':getReturnFast'); $app->get('/documents/{id}/linkedMailing', \Document\controllers\DocumentController::class . ':getLinkedMailing'); $app->get('/documents/{id}/thumbnails/{page}', \Document\controllers\DocumentController::class . ':getThumbnailContent'); $app->put('/documents/{id}/workflows/interrupt', \Workflow\controllers\WorkflowController::class . ':interrupt'); diff --git a/src/app/workflow/controllers/FastOTPController.php b/src/app/workflow/controllers/FastOTPController.php index eb0556f20f73554b6e63c1a89d6a4f15c7db0030..a04ae1cb815c633d5008feae43c902cbd1384398 100644 --- a/src/app/workflow/controllers/FastOTPController.php +++ b/src/app/workflow/controllers/FastOTPController.php @@ -21,7 +21,13 @@ use SrcCore\models\AuthenticationModel; use Respect\Validation\Validator; use SrcCore\models\ValidatorModel; use Configuration\models\ConfigurationModel; - +use Slim\Http\Request; +use Slim\Http\Response; +use Workflow\models\WorkflowModel; +use Document\controllers\DocumentController; +use Docserver\controllers\DocserverController; +use Docserver\models\AdrModel; +use SrcCore\models\CoreConfigModel; class FastOTPController @@ -106,6 +112,104 @@ class FastOTPController return true; } + public function getReturnFast(Request $request, Response $response, array $args) + { + $workflow = WorkflowModel::get([ + 'select' => [1], + 'where' => ['main_document_id = ?', 'id = ?', 'status is null'], + 'data' => [$args['id'], $args['workflowId']] + ]); + if (empty($workflow[0])) { + return $response->withStatus(403)->withJson(['errors' => 'Workflow does not exist']); + } + + $workflowExternalInformations = WorkflowExternalInformationModel::getByWorkflowId(['select' => ['informations', 'external_signatory_book_id'], 'workflowId' => $args['workflowId']]); + if (empty($workflowExternalInformations)) { + return $response->withStatus(403)->withJson(['errors' => 'Workflow does not exist']); + } + + $informations = json_decode($workflowExternalInformations['informations'], true); + if ($informations['fastDocId'] != $args['fileId']) { + return $response->withStatus(403)->withJson(['errors' => 'Unauthorized fileId']); + } + + $externalSB = ExternalSignatoryBookModel::getById(['select' => ['*'], 'id' => $workflowExternalInformations['external_signatory_book_id']]); + if (empty($externalSB)) { + return $response->withStatus(400)->withJson(['errors' => 'No external signatory book configuration found']); + } elseif ($externalSB['type'] != 'fast') { + return $response->withStatus(400)->withJson(['errors' => 'External signatory book configuration is invalid']); + } + $externalSB['connection_data'] = json_decode($externalSB['connection_data'], true); + + + $comments = null; + if ($args['fastStatus'] == 'SIGNED') { + + $fileResponse = FastOTPController::download([ + 'connection_data' => $externalSB['connection_data'], + 'documentId' => $informations['fastDocId'] + ]); + if (!empty($response['errors'])) { + return $response->withStatus(400)->withJson(['errors' => $fileResponse['errors']]); + } + + $storeInfos = DocserverController::storeResourceOnDocServer([ + 'encodedFile' => $fileResponse['response'], + 'format' => 'pdf', + 'docserverType' => 'ESIGN' + ]); + if (!empty($storeInfos['errors'])) { + return $response->withStatus(500)->withJson(['errors' => $storeInfos['errors']]); + } + + AdrModel::deleteDocumentAdr([ + 'where' => ['main_document_id = ?', 'type = ?'], + 'data' => [$args['id'], 'ESIGN'] + ]); + AdrModel::createDocumentAdr([ + 'documentId' => $args['id'], + 'type' => 'ESIGN', + 'path' => $storeInfos['path'], + 'filename' => $storeInfos['filename'], + 'fingerprint' => $storeInfos['fingerprint'] + ]); + + AdrModel::deleteDocumentAdr([ + 'where' => ['main_document_id = ?', 'type like ?'], + 'data' => [$args['id'], 'TNL%'] + ]); + $configPath = CoreConfigModel::getConfigPath(); + exec("php src/app/convert/scripts/ThumbnailScript.php '{$configPath}' {$args['id']} 'document' 0 > /dev/null &"); + + } elseif ($args['fastStatus'] == 'REFUSED') { + + $messageResponse = FastOTPController::getRefusalMessage([ + 'connection_data' => $externalSB['connection_data'], + 'documentId' => $informations['fastDocId'], + ]); + if (!empty($messageResponse['errors'])) { + return $response->withStatus(400)->withJson(['errors' => $messageResponse['errors']]); + } + + $comments = $messageResponse['response']; + + } else { + return $response->withStatus(400)->withJson(['errors' => "Invalid 'fastStatus'"]); + } + + + $GLOBALS['webhook'] = 'fastParapheur webhook'; + $GLOBALS['id'] = 0; + DocumentController::endAction([ + 'id' => $args['id'], + 'workflowId' => $args['workflowId'], + 'status' => $args['fastStatus'] == 'SIGNED' ? 'VAL' : 'REF', + 'note' => $comments ?? null + ]); + + return true; + } + public static function controlConnectionDataConfig(array $args) { if (!Validator::arrayType()->notEmpty()->validate($args['connection_data'])) { @@ -196,4 +300,58 @@ class FastOTPController return ['filename' => $args['filename'], 'content' => $xmlData]; } + public static function download(array $args) + { + ValidatorModel::notEmpty($args, ['connection_data', 'documentId']); + ValidatorModel::arrayType($args, ['connection_data']); + ValidatorModel::intVal($args, ['documentId']); + + $curlReturn = CurlModel::exec([ + 'method' => 'GET', + 'url' => $args['connection_data']['apiUri'] . '/documents/v2/' . $args['documentId'] . '/download', + 'options' => [ + CURLOPT_SSLCERT => $args['connection_data']['certPath'], + CURLOPT_SSLCERTPASSWD => $args['connection_data']['certPass'], + CURLOPT_SSLCERTTYPE => $args['connection_data']['certType'], + ], + 'fileResponse' => true + ]); + + if ($curlReturn['code'] != 200 || !empty($curlReturn['response']['errorCode'])) { + if (!empty($curlReturn['resonse']['userFriendlyMessage']) && $curlReturn['resonse']['userFriendlyMessage'] != 'Erreur') { + return ['errors' => $curlReturn['response']['userFriendlyMessage']]; + } else { + return ['errors' => $curlReturn['response']['developerMessage']]; + } + } + + return ['response' => base64_encode($curlReturn['response'])]; + } + + public static function getRefusalMessage(array $args) + { + ValidatorModel::notEmpty($args, ['connection_data', 'documentId']); + ValidatorModel::arrayType($args, ['connection_data']); + ValidatorModel::intVal($args, ['documentId']); + + $curlReturn = CurlModel::exec([ + 'url' => $args['connection_data']['apiUri'] . '/documents/v2/' . $args['documentId'] . '/comments/refusal', + 'method' => 'GET', + 'options' => [ + CURLOPT_SSLCERT => $args['connection_data']['certPath'], + CURLOPT_SSLCERTPASSWD => $args['connection_data']['certPass'], + CURLOPT_SSLCERTTYPE => $args['connection_data']['certType'] + ] + ]); + + if ($curlReturn['code'] != 200 || !empty($curlReturn['response']['errorCode'])) { + if (!empty($curlReturn['resonse']['userFriendlyMessage']) && $curlReturn['resonse']['userFriendlyMessage'] != 'Erreur') { + return ['errors' => $curlReturn['response']['userFriendlyMessage']]; + } else { + return ['errors' => $curlReturn['response']['developerMessage']]; + } + } + + return ['response' => $curlReturn['response']['comment']]; + } } \ No newline at end of file