From 4301b1b82a8f097151fa376477148e4fcc0ced36 Mon Sep 17 00:00:00 2001 From: Damien <damien.burel@maarch.org> Date: Thu, 13 Feb 2020 16:41:53 +0100 Subject: [PATCH] FEAT #12072 TIME 0:20 Email get by id refactoring --- modules/visa/sign_file.php | 5 +- src/app/email/controllers/EmailController.php | 61 +++------ .../controllers/SignatureBookController.php | 123 ++++++++++++++++++ 3 files changed, 144 insertions(+), 45 deletions(-) diff --git a/modules/visa/sign_file.php b/modules/visa/sign_file.php index 0b56ae5af43..b3e1c1bbbe5 100755 --- a/modules/visa/sign_file.php +++ b/modules/visa/sign_file.php @@ -59,7 +59,7 @@ if (!empty($_REQUEST['id']) && !empty($_REQUEST['collId'])) { $objectId = $_REQUEST['id']; $tableName = 'res_attachments'; - $stmt = $db->query("select relation, res_id, format, res_id_master, title, identifier, type_id, attachment_type, dest_contact_id, dest_address_id, dest_user from " + $stmt = $db->query("select relation, res_id, format, res_id_master, title, identifier, attachment_type from " . $tableName . " where (attachment_type NOT IN ('converted_pdf','print_folder')) and res_id = ?", array($objectId)); @@ -72,9 +72,6 @@ if (!empty($_REQUEST['id']) && !empty($_REQUEST['collId'])) { $_SESSION['visa']['last_resId_signed']['title'] = $line->title; $_SESSION['visa']['last_resId_signed']['identifier'] = $line->identifier; $_SESSION['visa']['last_resId_signed']['type_id'] = $line->type_id; - $_SESSION['visa']['last_resId_signed']['dest_contact'] = $line->dest_contact_id; - $_SESSION['visa']['last_resId_signed']['dest_address'] = $line->dest_address_id; - $_SESSION['visa']['last_resId_signed']['dest_user'] = $line->dest_user; if ($line->attachment_type == 'response_project') { //Update outgoing date diff --git a/src/app/email/controllers/EmailController.php b/src/app/email/controllers/EmailController.php index 5a57bce2bd8..95647787ece 100644 --- a/src/app/email/controllers/EmailController.php +++ b/src/app/email/controllers/EmailController.php @@ -153,51 +153,29 @@ class EmailController public function getById(Request $request, Response $response, array $args) { - $emailArray = EmailModel::getById(['id' => $args['id']]); - $document = (array)json_decode($emailArray['document']); + $rawEmail = EmailModel::getById(['id' => $args['id']]); + $document = json_decode($rawEmail['document'], true); - if (!ResController::hasRightByResId(['resId' => [$document['id']], 'userId' => $GLOBALS['id']])) { + if (!empty($document['id']) && !ResController::hasRightByResId(['resId' => [$document['id']], 'userId' => $GLOBALS['id']])) { return $response->withStatus(403)->withJson(['errors' => 'Document out of perimeter']); } - $sender = json_decode($emailArray['sender'], true); - $email['to'] = json_decode($emailArray['recipients'], true); - $email['cc'] = json_decode($emailArray['cc'], true); - $email['cci'] = json_decode($emailArray['cci'], true); - $email['id'] = $emailArray['id']; - $email['resId'] = $document['id']; - - $user = UserModel::getById(['id' => $emailArray['user_id'], 'select' => ['user_id']]); - $email['login'] = $user['user_id']; - - $email['attachments'] = []; - $email['attachments_version'] = []; - - if (!empty($document['attachments'])) { - $document['attachments'] = (array)$document['attachments']; - foreach ($document['attachments'] as $attachment) { - $attachment = (array)$attachment; - $email['attachments'][] = $attachment['id']; - } - } - - $email['notes'] = $document['notes']; - - $email['object'] = $emailArray['object']; - $email['body'] = $emailArray['body']; - $email['resMasterAttached'] = ($document['isLinked']) ? 'Y' : 'N'; - $email['isHtml'] = ($emailArray['is_html']) ? 'Y' : 'N'; - $email['status'] = $emailArray['status']; - $email['creationDate'] = $emailArray['creation_date']; - $email['sendDate'] = $emailArray['send_date']; - - if (!empty($sender['entityId'])) { - $entity = EntityModel::getById(['select' => ['entity_id'], 'id' => $sender['entityId']]); - $email['sender_email'] = $entity['entity_id'] . ',' . $sender['email']; - } else { - $email['sender_email'] = $sender['email']; - } - + $email = [ + 'id' => $rawEmail['id'], + 'sender' => json_decode($rawEmail['sender'], true), + 'to' => json_decode($rawEmail['recipients'], true), + 'cc' => json_decode($rawEmail['cc'], true), + 'cci' => json_decode($rawEmail['cci'], true), + 'userId' => $rawEmail['user_id'], + 'object' => $rawEmail['object'], + 'body' => $rawEmail['body'], + 'isHtml' => $rawEmail['is_html'], + 'status' => $rawEmail['status'], + 'creationDate' => $rawEmail['creation_date'], + 'sendDate' => $rawEmail['send_date'], + 'document' => $document + ]; + return $response->withJson($email); } @@ -533,6 +511,7 @@ class EmailController $notes[] = [ 'id' => $rawNote['id'], 'label' => $rawNote['note_text'], + 'typeLabel' => 'note', 'creator' => UserModel::getLabelledUserById(['id' => $rawNote['user_id']]), 'format' => 'html', 'size' => null diff --git a/src/app/signatureBook/controllers/SignatureBookController.php b/src/app/signatureBook/controllers/SignatureBookController.php index a8d8fd20f5d..983ddb917e0 100755 --- a/src/app/signatureBook/controllers/SignatureBookController.php +++ b/src/app/signatureBook/controllers/SignatureBookController.php @@ -32,6 +32,7 @@ use Note\models\NoteModel; use Priority\models\PriorityModel; use Resource\controllers\ResController; use Resource\controllers\ResourceListController; +use Resource\controllers\StoreController; use Resource\models\ResModel; use Respect\Validation\Validator; use Slim\Http\Request; @@ -508,6 +509,128 @@ class SignatureBookController return $response->withStatus(204); } + public function signAttachment(Request $request, Response $response, array $args) + { + if (!Validator::intVal()->validate($args['id'])) { + return $response->withStatus(400)->withJson(['errors' => 'Route id is not an integer']); + } + + $attachment = AttachmentModel::getById(['id' => $args['id'], 'select' => ['res_id_master']]); + if (empty($attachment)) { + return $response->withStatus(403)->withJson(['errors' => 'Attachment out of perimeter']); + } elseif (!SignatureBookController::isResourceInSignatureBook(['resId' => $attachment['res_id_master'], 'userId' => $GLOBALS['id']])) { + return $response->withStatus(403)->withJson(['errors' => 'Document out of signatory book']); + } + + $body = $request->getParsedBody(); + if (!Validator::intVal()->notEmpty()->validate($body['signatureId'])) { + return $response->withStatus(400)->withJson(['errors' => 'Body signatureId is empty or not an integer']); + } + + $signature = UserSignatureModel::getById(['id' => $body['signatureId'], 'select' => ['user_serial_id', 'signature_path', 'signature_file_name']]); + if (empty($signature)) { + return $response->withStatus(400)->withJson(['errors' => 'Signature does not exist']); + } elseif ($signature['user_serial_id'] != $GLOBALS['id']) { + return $response->withStatus(400)->withJson(['errors' => 'Signature out of perimeter']); + } + + $docserver = DocserverModel::getCurrentDocserver(['typeId' => 'TEMPLATES', 'collId' => 'templates', 'select' => ['path_template']]); + if (empty($docserver['path_template']) || !is_dir($docserver['path_template'])) { + return $response->withStatus(400)->withJson(['errors' => 'Docserver TEMPLATES does not exist']); + } + $signaturePath = $docserver['path_template'] . str_replace('#', '/', $signature['signature_path']) . $signature['signature_file_name']; + if (!file_exists($signaturePath)) { + return $response->withStatus(404)->withJson(['errors' => 'Signature not found on docserver']); + } + + $convertedDocument = AdrModel::getAttachments([ + 'select' => ['docserver_id', 'path', 'filename', 'type'], + 'where' => ['res_id = ?', 'type = ?'], + 'data' => [$args['id'], 'PDF'] + ]); + if (empty($convertedDocument[0])) { + return $response->withStatus(400)->withJson(['errors' => 'Converted document does not exist']); + } + + $convertedDocument = $convertedDocument[0]; + $docserver = DocserverModel::getByDocserverId(['docserverId' => $convertedDocument['docserver_id'], 'select' => ['path_template', 'docserver_type_id']]); + if (empty($docserver['path_template']) || !is_dir($docserver['path_template'])) { + return $response->withStatus(400)->withJson(['errors' => 'Docserver does not exist']); + } + $pathToDocument = $docserver['path_template'] . str_replace('#', DIRECTORY_SEPARATOR, $convertedDocument['path']) . $convertedDocument['filename']; + if (!file_exists($pathToDocument)) { + return $response->withStatus(404)->withJson(['errors' => 'Document not found on docserver']); + } + + $loadedXml = CoreConfigModel::getXmlLoaded(['path' => 'modules/visa/xml/config.xml']); + $width = (int)$loadedXml->CONFIG->width_blocsign ?? 150; + $height = (int)$loadedXml->CONFIG->height_blocsign ?? 100; + $tmpPath = CoreConfigModel::getTmpPath(); + + $command = "java -jar modules/visa/dist/SignPdf.jar {$pathToDocument} {$signaturePath} {$width} {$height} {$tmpPath}"; + exec($command, $output, $return); + + $signedDocument = @file_get_contents($tmpPath.$convertedDocument['filename']); + if ($signedDocument === false) { + return $response->withStatus(400)->withJson(['errors' => 'Signature failed : ' . implode($output)]); + } + unlink($tmpPath.$convertedDocument['filename']); + + $storeResult = DocserverController::storeResourceOnDocServer([ + 'collId' => 'attachments_coll', + 'docserverTypeId' => 'DOC', + 'encodedResource' => base64_encode($signedDocument), + 'format' => 'pdf' + ]); + if (!empty($storeResult['errors'])) { + return ['errors' => "[storeResourceOnDocServer] {$storeResult['errors']}"]; + } + $resource = ResModel::getById(['resId' => $args['resId'], 'select' => ['version']]); + AdrModel::createDocumentAdr([ + 'resId' => $args['resId'], + 'type' => 'SIGN', + 'docserverId' => $storeResult['docserver_id'], + 'path' => $storeResult['directory'], + 'filename' => $storeResult['file_destination_name'], + 'version' => $resource['version'], + 'fingerprint' => $storeResult['fingerPrint'] + ]); + + $data = [ + 'title' => $attachment['title'], + 'encodedFile' => base64_encode($signedDocument), + 'format' => $attachment['format'], + 'resIdMaster' => $attachment['res_id_master'], + 'chrono' => $attachment['identifier'], + 'type' => 'signed_response', + 'recipientId' => $attachment['recipient_id'], + 'recipientType' => $attachment['recipient_type'], + 'inSignatureBook' => true + ]; + + $isStored = StoreController::storeAttachment($data); + if (!empty($isStored['errors'])) { + return ['errors' => $isStored['errors']]; + } + + + ListInstanceModel::update([ + 'set' => ['signatory' => 'true'], + 'where' => ['res_id = ?', 'item_id = ?', 'difflist_type = ?'], + 'data' => [$args['resId'], $GLOBALS['userId'], 'VISA_CIRCUIT'] + ]); + + HistoryController::add([ + 'tableName' => 'res_letterbox', + 'recordId' => $args['resId'], + 'eventType' => 'SIGN', + 'eventId' => 'resourceSign', + 'info' => _DOCUMENT_SIGNED + ]); + + return $response->withStatus(204); + } + public function unsignAttachment(Request $request, Response $response, array $args) { if (!Validator::intVal()->validate($args['id'])) { -- GitLab