-
-
Notifications
You must be signed in to change notification settings - Fork 111
Expand file tree
/
Copy pathRequestSignatureController.php
More file actions
277 lines (269 loc) · 9.2 KB
/
RequestSignatureController.php
File metadata and controls
277 lines (269 loc) · 9.2 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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
<?php
declare(strict_types=1);
/**
* SPDX-FileCopyrightText: 2020-2024 LibreCode coop and contributors
* SPDX-License-Identifier: AGPL-3.0-or-later
*/
namespace OCA\Libresign\Controller;
use OCA\Libresign\AppInfo\Application;
use OCA\Libresign\Exception\LibresignException;
use OCA\Libresign\Helper\ValidateHelper;
use OCA\Libresign\Middleware\Attribute\RequireManager;
use OCA\Libresign\ResponseDefinitions;
use OCA\Libresign\Service\FileService;
use OCA\Libresign\Service\RequestSignatureService;
use OCP\AppFramework\Http;
use OCP\AppFramework\Http\Attribute\ApiRoute;
use OCP\AppFramework\Http\Attribute\NoAdminRequired;
use OCP\AppFramework\Http\Attribute\NoCSRFRequired;
use OCP\AppFramework\Http\DataResponse;
use OCP\IL10N;
use OCP\IRequest;
use OCP\IUserSession;
/**
* @psalm-import-type LibresignNewFile from ResponseDefinitions
* @psalm-import-type LibresignNewSigner from ResponseDefinitions
* @psalm-import-type LibresignValidateFile from ResponseDefinitions
* @psalm-import-type LibresignSettings from ResponseDefinitions
* @psalm-import-type LibresignSigner from ResponseDefinitions
* @psalm-import-type LibresignVisibleElement from ResponseDefinitions
*/
class RequestSignatureController extends AEnvironmentAwareController {
public function __construct(
IRequest $request,
protected IL10N $l10n,
protected IUserSession $userSession,
protected FileService $fileService,
protected ValidateHelper $validateHelper,
protected RequestSignatureService $requestSignatureService,
) {
parent::__construct(Application::APP_ID, $request);
}
/**
* Request signature
*
* Request that a file be signed by a group of people.
* Each user in the users array can optionally include a 'signing_order' field
* to control the order of signatures when ordered signing flow is enabled.
*
* @param LibresignNewFile $file File object.
* @param LibresignNewSigner[] $users Collection of users who must sign the document. Each user can have: identify, displayName, description, notify, signing_order
* @param string $name The name of file to sign
* @param string|null $callback URL that will receive a POST after the document is signed
* @param integer|null $status Numeric code of status * 0 - no signers * 1 - signed * 2 - pending
* @param string|null $signatureFlow Signature flow mode: 'parallel' or 'ordered_numeric'. If not provided, uses global configuration
* @return DataResponse<Http::STATUS_OK, array{data: LibresignValidateFile, message: string}, array{}>|DataResponse<Http::STATUS_UNPROCESSABLE_ENTITY, array{message?: string, action?: integer, errors?: list<array{message: string, title?: string}>}, array{}>
*
* 200: OK
* 422: Unauthorized
*/
#[NoAdminRequired]
#[NoCSRFRequired]
#[RequireManager]
#[ApiRoute(verb: 'POST', url: '/api/{apiVersion}/request-signature', requirements: ['apiVersion' => '(v1)'])]
public function request(
array $file,
array $users,
string $name,
?string $callback = null,
?int $status = 1,
?string $signatureFlow = null,
): DataResponse {
$user = $this->userSession->getUser();
$data = [
'file' => $file,
'name' => $name,
'users' => $users,
'status' => $status,
'callback' => $callback,
'userManager' => $user,
'signatureFlow' => $signatureFlow,
];
try {
$this->requestSignatureService->validateNewRequestToFile($data);
$file = $this->requestSignatureService->save($data);
$return = $this->fileService
->setFile($file)
->setHost($this->request->getServerHost())
->setMe($data['userManager'])
->showVisibleElements()
->showSigners()
->showSettings()
->showMessages()
->toArray();
return new DataResponse(
[
'message' => $this->l10n->t('Success'),
'data' => $return
],
Http::STATUS_OK
);
} catch (LibresignException $e) {
$errorMessage = $e->getMessage();
$decoded = json_decode($errorMessage, true);
if (json_last_error() === JSON_ERROR_NONE && isset($decoded['errors'])) {
$errorMessage = $decoded['errors'][0]['message'] ?? $errorMessage;
}
return new DataResponse(
[
'message' => $errorMessage,
],
Http::STATUS_UNPROCESSABLE_ENTITY
);
} catch (\Throwable $th) {
$errorMessage = $th->getMessage();
return new DataResponse(
[
'message' => $errorMessage,
],
Http::STATUS_UNPROCESSABLE_ENTITY
);
}
}
/**
* Updates signatures data
*
* Is necessary to inform the UUID of the file and a list of people
*
* @param LibresignNewSigner[]|null $users Collection of users who must sign the document
* @param string|null $uuid UUID of sign request. The signer UUID is what the person receives via email when asked to sign. This is not the file UUID.
* @param LibresignVisibleElement[]|null $visibleElements Visible elements on document
* @param LibresignNewFile|array<empty>|null $file File object.
* @param integer|null $status Numeric code of status * 0 - no signers * 1 - signed * 2 - pending
* @return DataResponse<Http::STATUS_OK, array{message: string, data: LibresignValidateFile}, array{}>|DataResponse<Http::STATUS_UNPROCESSABLE_ENTITY, array{message?: string, action?: integer, errors?: list<array{message: string, title?: string}>}, array{}>
*
* 200: OK
* 422: Unauthorized
*/
#[NoAdminRequired]
#[NoCSRFRequired]
#[RequireManager]
#[ApiRoute(verb: 'PATCH', url: '/api/{apiVersion}/request-signature', requirements: ['apiVersion' => '(v1)'])]
public function updateSign(?array $users = [], ?string $uuid = null, ?array $visibleElements = null, ?array $file = [], ?int $status = null): DataResponse {
$user = $this->userSession->getUser();
$data = [
'uuid' => $uuid,
'file' => $file,
'users' => $users,
'userManager' => $user,
'status' => $status,
'visibleElements' => $visibleElements
];
try {
$this->validateHelper->validateExistingFile($data);
$this->validateHelper->validateFileStatus($data);
$this->validateHelper->validateIdentifySigners($data);
if (!empty($data['visibleElements'])) {
$this->validateHelper->validateVisibleElements($data['visibleElements'], $this->validateHelper::TYPE_VISIBLE_ELEMENT_PDF);
}
$file = $this->requestSignatureService->save($data);
$return = $this->fileService
->setFile($file)
->setHost($this->request->getServerHost())
->setMe($data['userManager'])
->showVisibleElements()
->showSigners()
->showSettings()
->showMessages()
->toArray();
return new DataResponse(
[
'message' => $this->l10n->t('Success'),
'data' => $return
],
Http::STATUS_OK
);
} catch (\Throwable $th) {
return new DataResponse(
[
'message' => $th->getMessage(),
],
Http::STATUS_UNPROCESSABLE_ENTITY
);
}
}
/**
* Delete sign request
*
* You can only request exclusion as any sign
*
* @param integer $fileId Node id of a Nextcloud file
* @param integer $signRequestId The sign request id
* @return DataResponse<Http::STATUS_OK, array{message: string}, array{}>|DataResponse<Http::STATUS_UNAUTHORIZED, array{message: string}, array{}>|DataResponse<Http::STATUS_UNPROCESSABLE_ENTITY, array{action: integer, errors: list<array{message: string, title?: string}>}, array{}>
*
* 200: OK
* 401: Failed
* 422: Failed
*/
#[NoAdminRequired]
#[NoCSRFRequired]
#[RequireManager]
#[ApiRoute(verb: 'DELETE', url: '/api/{apiVersion}/sign/file_id/{fileId}/{signRequestId}', requirements: ['apiVersion' => '(v1)'])]
public function deleteOneRequestSignatureUsingFileId(int $fileId, int $signRequestId): DataResponse {
try {
$data = [
'userManager' => $this->userSession->getUser(),
'file' => [
'fileId' => $fileId
]
];
$this->validateHelper->validateExistingFile($data);
$this->validateHelper->validateIsSignerOfFile($signRequestId, $fileId);
$this->requestSignatureService->unassociateToUser($fileId, $signRequestId);
} catch (\Throwable $th) {
return new DataResponse(
[
'message' => $th->getMessage(),
],
Http::STATUS_UNAUTHORIZED
);
}
return new DataResponse(
[
'message' => $this->l10n->t('Success')
],
Http::STATUS_OK
);
}
/**
* Delete sign request
*
* You can only request exclusion as any sign
*
* @param integer $fileId Node id of a Nextcloud file
* @return DataResponse<Http::STATUS_OK, array{message: string}, array{}>|DataResponse<Http::STATUS_UNAUTHORIZED, array{message: string}, array{}>|DataResponse<Http::STATUS_UNPROCESSABLE_ENTITY, array{action: integer, errors: list<array{message: string, title?: string}>}, array{}>
*
* 200: OK
* 401: Failed
* 422: Failed
*/
#[NoAdminRequired]
#[NoCSRFRequired]
#[RequireManager]
#[ApiRoute(verb: 'DELETE', url: '/api/{apiVersion}/sign/file_id/{fileId}', requirements: ['apiVersion' => '(v1)'])]
public function deleteAllRequestSignatureUsingFileId(int $fileId): DataResponse {
try {
$data = [
'userManager' => $this->userSession->getUser(),
'file' => [
'fileId' => $fileId
]
];
$this->validateHelper->validateExistingFile($data);
$this->requestSignatureService->deleteRequestSignature($data);
} catch (\Throwable $th) {
return new DataResponse(
[
'message' => $th->getMessage(),
],
Http::STATUS_UNAUTHORIZED
);
}
return new DataResponse(
[
'message' => $this->l10n->t('Success')
],
Http::STATUS_OK
);
}
}