Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
71.43% |
25 / 35 |
|
50.00% |
4 / 8 |
CRAP | |
0.00% |
0 / 1 |
| IdentityDocumentValidator | |
71.43% |
25 / 35 |
|
50.00% |
4 / 8 |
33.29 | |
0.00% |
0 / 1 |
| __construct | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| validateIdDocIsOwnedByUser | |
0.00% |
0 / 3 |
|
0.00% |
0 / 1 |
6 | |||
| validateIdDocBelongsToSignRequest | |
0.00% |
0 / 3 |
|
0.00% |
0 / 1 |
6 | |||
| validateUserHasNoFileWithThisType | |
50.00% |
1 / 2 |
|
0.00% |
0 / 1 |
2.50 | |||
| canSignWithIdentificationDocumentStatus | |
100.00% |
8 / 8 |
|
100.00% |
1 / 1 |
4 | |||
| validateFileTypeExists | |
100.00% |
2 / 2 |
|
100.00% |
1 / 1 |
2 | |||
| userCanApproveValidationDocuments | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| haveValidMail | |
80.00% |
12 / 15 |
|
0.00% |
0 / 1 |
8.51 | |||
| 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\Service\Validation; |
| 10 | |
| 11 | use OCA\Libresign\Db\FileTypeMapper; |
| 12 | use OCA\Libresign\Db\IdDocsMapper; |
| 13 | use OCA\Libresign\Exception\LibresignException; |
| 14 | use OCA\Libresign\Helper\JSActions; |
| 15 | use OCA\Libresign\Service\FileService; |
| 16 | use OCA\Libresign\Service\IdDocsPolicyService; |
| 17 | use OCP\IL10N; |
| 18 | use OCP\IUser; |
| 19 | use OCP\IUserManager; |
| 20 | |
| 21 | class IdentityDocumentValidator { |
| 22 | public function __construct( |
| 23 | private IL10N $l10n, |
| 24 | private IdDocsMapper $idDocsMapper, |
| 25 | private FileTypeMapper $fileTypeMapper, |
| 26 | private IdDocsPolicyService $idDocsPolicyService, |
| 27 | private IUserManager $userManager, |
| 28 | ) { |
| 29 | } |
| 30 | |
| 31 | public function validateIdDocIsOwnedByUser(int $nodeId, string $uid): void { |
| 32 | try { |
| 33 | $this->idDocsMapper->getByUserIdAndNodeId($uid, $nodeId); |
| 34 | } catch (\Throwable) { |
| 35 | throw new LibresignException($this->l10n->t('This file is not yours')); |
| 36 | } |
| 37 | } |
| 38 | |
| 39 | public function validateIdDocBelongsToSignRequest(int $nodeId, int $signRequestId): void { |
| 40 | try { |
| 41 | $this->idDocsMapper->getBySignRequestIdAndNodeId($signRequestId, $nodeId); |
| 42 | } catch (\Throwable) { |
| 43 | throw new LibresignException($this->l10n->t('Not allowed')); |
| 44 | } |
| 45 | } |
| 46 | |
| 47 | public function validateUserHasNoFileWithThisType(string $uid, string $type): void { |
| 48 | if ($this->idDocsMapper->getByUserAndType($uid, $type) !== null) { |
| 49 | throw new LibresignException($this->l10n->t('A file of this type has been associated.')); |
| 50 | } |
| 51 | } |
| 52 | |
| 53 | public function canSignWithIdentificationDocumentStatus(?IUser $user, int $status): void { |
| 54 | if ($user && $this->userCanApproveValidationDocuments($user, false)) { |
| 55 | return; |
| 56 | } |
| 57 | $allowedStatus = [FileService::IDENTIFICATION_DOCUMENTS_DISABLED, FileService::IDENTIFICATION_DOCUMENTS_APPROVED]; |
| 58 | if (!in_array($status, $allowedStatus, true)) { |
| 59 | throw new LibresignException( |
| 60 | $this->l10n->t('You need to have an approved identification document to sign.'), |
| 61 | JSActions::ACTION_SIGN_ID_DOC, |
| 62 | ); |
| 63 | } |
| 64 | } |
| 65 | |
| 66 | public function validateFileTypeExists(string $type): void { |
| 67 | if (!array_key_exists($type, $this->fileTypeMapper->getTypes())) { |
| 68 | throw new LibresignException($this->l10n->t('Invalid file type.')); |
| 69 | } |
| 70 | } |
| 71 | |
| 72 | public function userCanApproveValidationDocuments(?IUser $user, bool $throw = true): bool { |
| 73 | return $this->idDocsPolicyService->userCanApproveValidationDocuments($user, $throw); |
| 74 | } |
| 75 | |
| 76 | public function haveValidMail(array $data, ?int $type = null): void { |
| 77 | if ($type === FileInputValidator::TYPE_TO_SIGN) { |
| 78 | return; |
| 79 | } |
| 80 | if (empty($data)) { |
| 81 | throw new LibresignException($this->l10n->t('No user data')); |
| 82 | } |
| 83 | if (empty($data['email'])) { |
| 84 | if (!empty($data['uid'])) { |
| 85 | $user = $this->userManager->get($data['uid']); |
| 86 | if (!$user) { |
| 87 | throw new LibresignException($this->l10n->t('User not found.')); |
| 88 | } |
| 89 | if (!$user->getEMailAddress()) { |
| 90 | throw new LibresignException($this->l10n->t('User %s has no email address.', [$data['uid']])); |
| 91 | } |
| 92 | return; |
| 93 | } |
| 94 | throw new LibresignException($this->l10n->t('Email required')); |
| 95 | } |
| 96 | if (!filter_var($data['email'], FILTER_VALIDATE_EMAIL)) { |
| 97 | throw new LibresignException($this->l10n->t('Invalid email')); |
| 98 | } |
| 99 | } |
| 100 | } |