Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
48.57% |
17 / 35 |
|
55.56% |
5 / 9 |
CRAP | |
0.00% |
0 / 1 |
| LibresignTrait | |
48.57% |
17 / 35 |
|
55.56% |
5 / 9 |
45.61 | |
0.00% |
0 / 1 |
| loadEntitiesFromUuid | |
91.67% |
11 / 12 |
|
0.00% |
0 / 1 |
4.01 | |||
| validateSignRequestUuid | |
100.00% |
2 / 2 |
|
100.00% |
1 / 1 |
1 | |||
| validateParticipantUuid | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| validateRenewSigner | |
0.00% |
0 / 2 |
|
0.00% |
0 / 1 |
2 | |||
| loadNextcloudFileFromUuid | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| loadIdDocApprovalFromResolution | |
0.00% |
0 / 12 |
|
0.00% |
0 / 1 |
12 | |||
| getSignRequestEntity | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| getFileEntity | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| getNextcloudFiles | |
0.00% |
0 / 3 |
|
0.00% |
0 / 1 |
6 | |||
| 1 | <?php |
| 2 | |
| 3 | declare(strict_types=1); |
| 4 | /** |
| 5 | * SPDX-FileCopyrightText: 2020-2024 LibreCode coop and contributors |
| 6 | * SPDX-License-Identifier: AGPL-3.0-or-later |
| 7 | */ |
| 8 | |
| 9 | namespace OCA\Libresign\Controller; |
| 10 | |
| 11 | use OC\AppFramework\Http as AppFrameworkHttp; |
| 12 | use OCA\Libresign\Db\File as FileEntity; |
| 13 | use OCA\Libresign\Db\SignRequest as SignRequestEntity; |
| 14 | use OCA\Libresign\Exception\LibresignException; |
| 15 | use OCA\Libresign\Helper\JSActions; |
| 16 | use OCA\Libresign\Service\SignFileService; |
| 17 | use OCP\AppFramework\Db\DoesNotExistException; |
| 18 | use OCP\Files\File; |
| 19 | use OCP\IL10N; |
| 20 | use OCP\IUserSession; |
| 21 | |
| 22 | trait LibresignTrait { |
| 23 | protected SignFileService $signFileService; |
| 24 | protected IL10N $l10n; |
| 25 | protected IUserSession $userSession; |
| 26 | private ?SignRequestEntity $signRequestEntity = null; |
| 27 | private ?FileEntity $fileEntity = null; |
| 28 | |
| 29 | /** |
| 30 | * @throws LibresignException |
| 31 | */ |
| 32 | private function loadEntitiesFromUuid(string $uuid): void { |
| 33 | if ($this->signRequestEntity instanceof SignRequestEntity |
| 34 | && $this->fileEntity instanceof FileEntity) { |
| 35 | return; |
| 36 | } |
| 37 | try { |
| 38 | $this->signRequestEntity = $this->signFileService->getSignRequestByUuid($uuid); |
| 39 | $this->fileEntity = $this->signFileService->getFile( |
| 40 | $this->signRequestEntity->getFileId(), |
| 41 | ); |
| 42 | } catch (DoesNotExistException|LibresignException) { |
| 43 | throw new LibresignException(json_encode([ |
| 44 | 'action' => JSActions::ACTION_DO_NOTHING, |
| 45 | // TRANSLATORS Error shown when a signing or validation request uses a malformed or unknown document identifier. |
| 46 | 'errors' => [['message' => $this->l10n->t('Invalid UUID')]], |
| 47 | ]), AppFrameworkHttp::STATUS_NOT_FOUND); |
| 48 | } |
| 49 | } |
| 50 | |
| 51 | /** |
| 52 | * Authorize a signer UUID for signing and other write operations. |
| 53 | * |
| 54 | * Observers are rejected by signer validation. |
| 55 | * |
| 56 | * @throws LibresignException |
| 57 | */ |
| 58 | public function validateSignRequestUuid(string $uuid): void { |
| 59 | $this->loadEntitiesFromUuid($uuid); |
| 60 | $this->signFileService->validateSigner($uuid, $this->userSession->getUser()); |
| 61 | } |
| 62 | |
| 63 | /** |
| 64 | * Authorize any participant UUID (signer or observer) for read-only access. |
| 65 | * |
| 66 | * @throws LibresignException |
| 67 | */ |
| 68 | public function validateParticipantUuid(string $uuid): void { |
| 69 | $this->loadEntitiesFromUuid($uuid); |
| 70 | } |
| 71 | |
| 72 | /** |
| 73 | * @throws LibresignException |
| 74 | */ |
| 75 | public function validateRenewSigner(string $uuid): void { |
| 76 | $this->loadEntitiesFromUuid($uuid); |
| 77 | $this->signFileService->validateRenewSigner($uuid, $this->userSession->getUser()); |
| 78 | } |
| 79 | |
| 80 | /** |
| 81 | * Load LibreSign file/sign-request entities for any participant UUID. |
| 82 | * |
| 83 | * @throws LibresignException |
| 84 | */ |
| 85 | public function loadNextcloudFileFromUuid(string $uuid): void { |
| 86 | $this->loadEntitiesFromUuid($uuid); |
| 87 | } |
| 88 | |
| 89 | /** |
| 90 | * Load identification document approval entities from uuid resolver result |
| 91 | * Used when an approver is signing an identification document |
| 92 | * |
| 93 | * @param array $resolution Contains: 'file' => File entity, 'signRequest' => null, 'type' => 'id_doc' |
| 94 | * @throws LibresignException |
| 95 | */ |
| 96 | public function loadIdDocApprovalFromResolution(array $resolution): void { |
| 97 | if ($resolution['type'] !== 'id_doc') { |
| 98 | throw new LibresignException(json_encode([ |
| 99 | 'action' => JSActions::ACTION_DO_NOTHING, |
| 100 | // TRANSLATORS Error shown when loading an identification-document approval request with an invalid identifier. |
| 101 | 'errors' => [['message' => $this->l10n->t('Invalid identification document request')]], |
| 102 | ]), AppFrameworkHttp::STATUS_BAD_REQUEST); |
| 103 | } |
| 104 | |
| 105 | if (!$resolution['file'] instanceof FileEntity) { |
| 106 | throw new LibresignException(json_encode([ |
| 107 | 'action' => JSActions::ACTION_DO_NOTHING, |
| 108 | // TRANSLATORS Error shown when the requested LibreSign file cannot be loaded for the current action. |
| 109 | 'errors' => [['message' => $this->l10n->t('Invalid file')]], |
| 110 | ]), AppFrameworkHttp::STATUS_NOT_FOUND); |
| 111 | } |
| 112 | |
| 113 | $this->signRequestEntity = null; |
| 114 | $this->fileEntity = $resolution['file']; |
| 115 | } |
| 116 | |
| 117 | public function getSignRequestEntity(): ?SignRequestEntity { |
| 118 | return $this->signRequestEntity; |
| 119 | } |
| 120 | |
| 121 | public function getFileEntity(): ?FileEntity { |
| 122 | return $this->fileEntity; |
| 123 | } |
| 124 | |
| 125 | /** |
| 126 | * @return File[] Array of files, empty if no file entity loaded |
| 127 | */ |
| 128 | public function getNextcloudFiles(): array { |
| 129 | if (!$this->fileEntity) { |
| 130 | return []; |
| 131 | } |
| 132 | |
| 133 | return $this->signFileService->getNextcloudFiles($this->fileEntity); |
| 134 | } |
| 135 | } |