-
-
Notifications
You must be signed in to change notification settings - Fork 111
Expand file tree
/
Copy pathIdentifyService.php
More file actions
180 lines (151 loc) · 4.98 KB
/
IdentifyService.php
File metadata and controls
180 lines (151 loc) · 4.98 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
<?php
declare(strict_types=1);
/**
* SPDX-FileCopyrightText: 2020-2024 LibreCode coop and contributors
* SPDX-License-Identifier: AGPL-3.0-or-later
*/
namespace OCA\Libresign\Service\IdentifyMethod;
use OCA\Libresign\AppInfo\Application;
use OCA\Libresign\Db\FileMapper;
use OCA\Libresign\Db\IdentifyMethod;
use OCA\Libresign\Db\IdentifyMethodMapper;
use OCA\Libresign\Db\SignRequestMapper;
use OCA\Libresign\Service\SessionService;
use OCP\AppFramework\Utility\ITimeFactory;
use OCP\EventDispatcher\IEventDispatcher;
use OCP\Files\IRootFolder;
use OCP\IAppConfig;
use OCP\IL10N;
use OCP\IURLGenerator;
use OCP\IUserManager;
use OCP\Security\IHasher;
use Psr\Log\LoggerInterface;
class IdentifyService {
private array $savedSettings = [];
public function __construct(
private IdentifyMethodMapper $identifyMethodMapper,
private SessionService $sessionService,
private ITimeFactory $timeFactory,
private IEventDispatcher $eventDispatcher,
private IRootFolder $root,
private IAppConfig $appConfig,
private SignRequestMapper $signRequestMapper,
private IL10N $l10n,
private FileMapper $fileMapper,
private IHasher $hasher,
private IUserManager $userManager,
private IURLGenerator $urlGenerator,
private LoggerInterface $logger,
) {
}
public function save(IdentifyMethod $identifyMethod): void {
$this->refreshIdFromDatabaseIfNecessary($identifyMethod);
if ($identifyMethod->getId()) {
$this->identifyMethodMapper->update($identifyMethod);
$this->propagateIdentifiedDateToEnvelopeChildren($identifyMethod);
return;
}
$this->identifyMethodMapper->insertOrUpdate($identifyMethod);
$this->propagateIdentifiedDateToEnvelopeChildren($identifyMethod);
return;
}
private function propagateIdentifiedDateToEnvelopeChildren(IdentifyMethod $identifyMethod): void {
if (!$identifyMethod->getIdentifiedAtDate()) {
return;
}
if (!$identifyMethod->getSignRequestId()) {
return;
}
$signRequest = $this->signRequestMapper->getById($identifyMethod->getSignRequestId());
$fileEntity = $this->fileMapper->getById($signRequest->getFileId());
if ($fileEntity->getNodeType() === 'envelope') {
$envelopeId = $fileEntity->getId();
} elseif ($fileEntity->hasParent()) {
$envelopeId = $fileEntity->getParentFileId();
} else {
return;
}
$children = $this->signRequestMapper->getByEnvelopeChildrenAndIdentifyMethod(
$envelopeId,
$signRequest->getId(),
);
foreach ($children as $childSignRequest) {
// Skip the current sign request to avoid updating it twice
if ($childSignRequest->getId() === $signRequest->getId()) {
continue;
}
$childMethods = $this->identifyMethodMapper->getIdentifyMethodsFromSignRequestId($childSignRequest->getId());
foreach ($childMethods as $childEntity) {
if (
$childEntity->getIdentifierKey() === $identifyMethod->getIdentifierKey()
&& $childEntity->getIdentifierValue() === $identifyMethod->getIdentifierValue()
) {
$childEntity->setIdentifiedAtDate($identifyMethod->getIdentifiedAtDate());
$this->identifyMethodMapper->update($childEntity);
}
}
}
}
public function delete(IdentifyMethod $identifyMethod): void {
if ($identifyMethod->getId()) {
$this->identifyMethodMapper->delete($identifyMethod);
}
}
private function refreshIdFromDatabaseIfNecessary(IdentifyMethod $identifyMethod): void {
if ($identifyMethod->getId()) {
return;
}
if (!$identifyMethod->getSignRequestId() || !$identifyMethod->getIdentifierKey()) {
return;
}
$identifyMethods = $this->identifyMethodMapper->getIdentifyMethodsFromSignRequestId($identifyMethod->getSignRequestId());
$exists = array_filter($identifyMethods, fn (IdentifyMethod $current): bool => $current->getIdentifierKey() === $identifyMethod->getIdentifierKey());
if (!$exists) {
return;
}
$exists = current($exists);
$identifyMethod->setId($exists->getId());
}
public function getSavedSettings(): array {
if (!empty($this->savedSettings)) {
return $this->savedSettings;
}
return $this->getAppConfig()->getValueArray(Application::APP_ID, 'identify_methods', []);
}
public function getEventDispatcher(): IEventDispatcher {
return $this->eventDispatcher;
}
public function getSessionService(): SessionService {
return $this->sessionService;
}
public function getTimeFactory(): ITimeFactory {
return $this->timeFactory;
}
public function getRootFolder(): IRootFolder {
return $this->root;
}
public function getAppConfig(): IAppConfig {
return $this->appConfig;
}
public function getSignRequestMapper(): SignRequestMapper {
return $this->signRequestMapper;
}
public function getL10n(): IL10N {
return $this->l10n;
}
public function getFileMapper(): FileMapper {
return $this->fileMapper;
}
public function getHasher(): IHasher {
return $this->hasher;
}
public function getUserManager(): IUserManager {
return $this->userManager;
}
public function getUrlGenerator(): IURLGenerator {
return $this->urlGenerator;
}
public function getLogger(): LoggerInterface {
return $this->logger;
}
}