Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
24 / 24
100.00% covered (success)
100.00%
2 / 2
CRAP
100.00% covered (success)
100.00%
1 / 1
UuidResolverService
100.00% covered (success)
100.00%
24 / 24
100.00% covered (success)
100.00%
2 / 2
8
100.00% covered (success)
100.00%
1 / 1
 __construct
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 resolveUuidForUser
100.00% covered (success)
100.00%
23 / 23
100.00% covered (success)
100.00%
1 / 1
7
1<?php
2
3declare(strict_types=1);
4
5/**
6 * SPDX-FileCopyrightText: 2026 LibreCode coop and contributors
7 * SPDX-License-Identifier: AGPL-3.0-or-later
8 */
9
10namespace OCA\Libresign\Service;
11
12use OCA\Libresign\Db\File;
13use OCA\Libresign\Db\IdDocsMapper;
14use OCA\Libresign\Db\SignRequest;
15use OCA\Libresign\Exception\IdDocAccessDeniedException;
16use OCA\Libresign\Exception\LibresignException;
17use OCA\Libresign\Exception\NotAnIdDocException;
18use OCP\AppFramework\Db\DoesNotExistException;
19use OCP\IUser;
20
21class UuidResolverService {
22    public function __construct(
23        private SignFileService $signFileService,
24        private IdDocsMapper $idDocsMapper,
25        private IdDocsPolicyService $idDocsPolicyService,
26    ) {
27    }
28
29    /**
30     * Resolve UUID for a user, supporting both SignRequest and id-doc File
31     *
32     * @param string $uuid The UUID to resolve
33     * @param IUser|null $user The authenticating user
34     * @return array{signRequest: SignRequest|null, file: File, type: string}
35     *                                                                        - type: 'sign_request' or 'id_doc'
36     *
37     * @throws LibresignException If UUID is invalid or user lacks permission
38     */
39    public function resolveUuidForUser(string $uuid, ?IUser $user): array {
40        try {
41            $signRequest = $this->signFileService->getSignRequestByUuid($uuid);
42            $file = $this->signFileService->getFile($signRequest->getFileId());
43
44            return [
45                'signRequest' => $signRequest,
46                'file' => $file,
47                'type' => 'sign_request',
48            ];
49        } catch (DoesNotExistException|LibresignException) {
50        }
51
52        try {
53            $file = $this->signFileService->getFileByUuid($uuid);
54
55            try {
56                $this->idDocsMapper->getByFileId($file->getId());
57                if (!$user || !$this->idDocsPolicyService->canApproverSignIdDoc($user, $file->getId(), $file->getStatus())) {
58                    throw new IdDocAccessDeniedException('User is not authorized to access this identification document');
59                }
60
61                return [
62                    'signRequest' => null,
63                    'file' => $file,
64                    'type' => 'id_doc',
65                ];
66            } catch (DoesNotExistException) {
67                throw new NotAnIdDocException('File is not an identification document');
68            }
69        } catch (IdDocAccessDeniedException|NotAnIdDocException $e) {
70            throw $e;
71        } catch (DoesNotExistException|LibresignException) {
72        }
73
74        throw new LibresignException('Invalid UUID');
75    }
76}