• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

LibreSign / libresign / 3717593689

pending completion
3717593689

Pull #1287

github

GitHub
Merge 42fe701e4 into ff8c1edaf
Pull Request #1287: [stable25] Bump packages

2548 of 4384 relevant lines covered (58.12%)

4.74 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

83.65
/lib/Controller/SignFileController.php
1
<?php
2

3
namespace OCA\Libresign\Controller;
4

5
use OCA\Libresign\AppInfo\Application;
6
use OCA\Libresign\Db\FileMapper;
7
use OCA\Libresign\Db\FileUserMapper;
8
use OCA\Libresign\Exception\LibresignException;
9
use OCA\Libresign\Helper\JSActions;
10
use OCA\Libresign\Helper\ValidateHelper;
11
use OCA\Libresign\Service\FileService;
12
use OCA\Libresign\Service\SignFileService;
13
use OCA\TwoFactorGateway\Exception\SmsTransmissionException;
14
use OCP\AppFramework\ApiController;
15
use OCP\AppFramework\Http;
16
use OCP\AppFramework\Http\JSONResponse;
17
use OCP\IL10N;
18
use OCP\IRequest;
19
use OCP\IUserSession;
20
use Psr\Log\LoggerInterface;
21

22
class SignFileController extends ApiController {
23
        /** @var IL10N */
24
        protected $l10n;
25
        /** @var FileUserMapper */
26
        private $fileUserMapper;
27
        /** @var FileMapper */
28
        private $fileMapper;
29
        /** @var IUserSession */
30
        private $userSession;
31
        /** @var SignFileService */
32
        protected $signFileService;
33
        /** @var FileService */
34
        private $fileService;
35
        /** @var ValidateHelper */
36
        protected $validateHelper;
37
        /** @var LoggerInterface */
38
        private $logger;
39

40
        public function __construct(
41
                IRequest $request,
42
                IL10N $l10n,
43
                FileUserMapper $fileUserMapper,
44
                FileMapper $fileMapper,
45
                IUserSession $userSession,
46
                ValidateHelper $validateHelper,
47
                SignFileService $signFileService,
48
                FileService $fileService,
49
                LoggerInterface $logger
50
        ) {
51
                parent::__construct(Application::APP_ID, $request);
18✔
52
                $this->l10n = $l10n;
18✔
53
                $this->fileUserMapper = $fileUserMapper;
18✔
54
                $this->fileMapper = $fileMapper;
18✔
55
                $this->userSession = $userSession;
18✔
56
                $this->validateHelper = $validateHelper;
18✔
57
                $this->signFileService = $signFileService;
18✔
58
                $this->fileService = $fileService;
18✔
59
                $this->logger = $logger;
18✔
60
        }
61

62
        /**
63
         * Request signature
64
         *
65
         * Request that a file be signed by a group of people
66
         *
67
         * @NoAdminRequired
68
         * @NoCSRFRequired
69
         *
70
         * @param array $file
71
         * @param array $users
72
         * @param string $name
73
         * @param string|null $callback
74
         * @return JSONResponse
75
         */
76
        public function requestSign(array $file, array $users, string $name, ?string $callback = null, ?int $status = 1) {
77
                $user = $this->userSession->getUser();
2✔
78
                $data = [
2✔
79
                        'file' => $file,
2✔
80
                        'name' => $name,
2✔
81
                        'users' => $users,
2✔
82
                        'status' => $status,
2✔
83
                        'callback' => $callback,
2✔
84
                        'userManager' => $user
2✔
85
                ];
2✔
86
                try {
87
                        $this->signFileService->validateNewRequestToFile($data);
2✔
88
                        $return = $this->signFileService->save($data);
1✔
89
                        unset(
1✔
90
                                $return['id'],
1✔
91
                                $return['users'],
1✔
92
                        );
1✔
93
                } catch (\Throwable $th) {
1✔
94
                        return new JSONResponse(
1✔
95
                                [
1✔
96
                                        'message' => $th->getMessage(),
1✔
97
                                ],
1✔
98
                                Http::STATUS_UNPROCESSABLE_ENTITY
1✔
99
                        );
1✔
100
                }
101
                return new JSONResponse(
1✔
102
                        [
1✔
103
                                'message' => $this->l10n->t('Success'),
1✔
104
                                'data' => $return
1✔
105
                        ],
1✔
106
                        Http::STATUS_OK
1✔
107
                );
1✔
108
        }
109

110
        /**
111
         * @NoAdminRequired
112
         * @NoCSRFRequired
113
         *
114
         * @param string $uuid
115
         * @param array $users
116
         * @return JSONResponse
117
         */
118
        public function updateSign(?array $users = [], ?string $uuid = null, ?array $visibleElements = null, ?array $file = [], ?int $status = null) {
119
                $user = $this->userSession->getUser();
3✔
120
                $data = [
3✔
121
                        'uuid' => $uuid,
3✔
122
                        'file' => $file,
3✔
123
                        'users' => $users,
3✔
124
                        'userManager' => $user,
3✔
125
                        'status' => $status,
3✔
126
                        'visibleElements' => $visibleElements
3✔
127
                ];
3✔
128
                try {
129
                        $this->signFileService->validateUserManager($data);
3✔
130
                        $this->validateHelper->validateExistingFile($data);
2✔
131
                        $this->validateHelper->validateFileStatus($data);
2✔
132
                        if (!empty($data['visibleElements'])) {
2✔
133
                                $this->validateHelper->validateVisibleElements($data['visibleElements'], $this->validateHelper::TYPE_VISIBLE_ELEMENT_PDF);
×
134
                        }
135
                        $return = $this->signFileService->save($data);
2✔
136
                        unset(
2✔
137
                                $return['id'],
2✔
138
                                $return['users'],
2✔
139
                        );
2✔
140
                } catch (\Throwable $th) {
1✔
141
                        return new JSONResponse(
1✔
142
                                [
1✔
143
                                        'message' => $th->getMessage(),
1✔
144
                                ],
1✔
145
                                Http::STATUS_UNPROCESSABLE_ENTITY
1✔
146
                        );
1✔
147
                }
148
                return new JSONResponse(
2✔
149
                        [
2✔
150
                                'message' => $this->l10n->t('Success'),
2✔
151
                                'data' => $return
2✔
152
                        ],
2✔
153
                        Http::STATUS_OK
2✔
154
                );
2✔
155
        }
156

157
        /**
158
         * @NoAdminRequired
159
         *
160
         * @NoCSRFRequired
161
         *
162
         * @param int $fileId
163
         * @param string $password
164
         *
165
         * @return JSONResponse
166
         */
167
        public function signUsingFileId(int $fileId, string $password = null, array $elements = [], string $code = null): JSONResponse {
168
                return $this->sign($password, $fileId, null, $elements, $code);
1✔
169
        }
170

171
        /**
172
         * @NoAdminRequired
173
         * @NoCSRFRequired
174
         *
175
         * @param string $uuid
176
         * @param string $password
177
         * @return JSONResponse
178
         */
179
        public function signUsingUuid(string $uuid, string $password = null, array $elements = [], string $code = null): JSONResponse {
180
                return $this->sign($password, null, $uuid, $elements, $code);
6✔
181
        }
182

183
        public function sign(string $password = null, int $fileId = null, string $fileUserUuid = null, array $elements = [], string $code = null): JSONResponse {
184
                try {
185
                        $user = $this->userSession->getUser();
7✔
186
                        $this->validateHelper->canSignWithIdentificationDocumentStatus(
7✔
187
                                $this->fileService->getIdentificationDocumentsStatus($user->getUID())
7✔
188
                        );
7✔
189
                        $libreSignFile = $this->signFileService->getLibresignFile($fileId, $fileUserUuid);
7✔
190
                        $fileUser = $this->signFileService->getFileUserToSign($libreSignFile, $user);
5✔
191
                        $this->validateHelper->validateVisibleElementsRelation($elements, $fileUser);
4✔
192
                        $this->validateHelper->validateCredentials($fileUser, [
4✔
193
                                'password' => $password,
4✔
194
                                'code' => $code,
4✔
195
                        ]);
4✔
196
                        $this->signFileService
4✔
197
                                ->setLibreSignFile($libreSignFile)
4✔
198
                                ->setFileUser($fileUser)
4✔
199
                                ->setVisibleElements($elements)
4✔
200
                                ->setSignWithoutPassword(!empty($code))
4✔
201
                                ->setPassword($password)
4✔
202
                                ->sign();
4✔
203

204
                        return new JSONResponse(
1✔
205
                                [
1✔
206
                                        'success' => true,
1✔
207
                                        'action' => JSActions::ACTION_SIGNED,
1✔
208
                                        'message' => $this->l10n->t('File signed'),
1✔
209
                                        'file' => [
1✔
210
                                                'uuid' => $libreSignFile->getUuid()
1✔
211
                                        ]
1✔
212
                                ],
1✔
213
                                Http::STATUS_OK
1✔
214
                        );
1✔
215
                } catch (LibresignException $e) {
6✔
216
                        return new JSONResponse(
5✔
217
                                [
5✔
218
                                        'success' => false,
5✔
219
                                        'action' => JSActions::ACTION_DO_NOTHING,
5✔
220
                                        'errors' => [$e->getMessage()]
5✔
221
                                ],
5✔
222
                                Http::STATUS_UNPROCESSABLE_ENTITY
5✔
223
                        );
5✔
224
                } catch (\Throwable $th) {
1✔
225
                        $message = $th->getMessage();
1✔
226
                        $action = JSActions::ACTION_DO_NOTHING;
1✔
227
                        switch ($message) {
228
                                case 'Password to sign not defined. Create a password to sign':
1✔
229
                                        $action = JSActions::ACTION_CREATE_SIGNATURE_PASSWORD;
×
230
                                        // no break
231
                                case 'Host violates local access rules.':
1✔
232
                                case 'Certificate Password Invalid.':
1✔
233
                                case 'Certificate Password is Empty.':
1✔
234
                                        $message = $this->l10n->t($message);
1✔
235
                                        break;
1✔
236
                                default:
237
                                        $this->logger->error($message);
×
238
                                        $message = $this->l10n->t('Internal error. Contact admin.');
×
239
                        }
240
                }
241
                return new JSONResponse(
1✔
242
                        [
1✔
243
                                'success' => false,
1✔
244
                                'action' => $action,
1✔
245
                                'errors' => [$message]
1✔
246
                        ],
1✔
247
                        Http::STATUS_UNPROCESSABLE_ENTITY
1✔
248
                );
1✔
249
        }
250

251
        /**
252
         * @NoAdminRequired
253
         * @NoCSRFRequired
254
         *
255
         * @param integer $fileId
256
         * @param integer $fileUserId
257
         * @return JSONResponse
258
         */
259
        public function deleteOneSignRequestUsingFileId(int $fileId, int $fileUserId) {
260
                try {
261
                        $data = [
2✔
262
                                'userManager' => $this->userSession->getUser(),
2✔
263
                                'file' => [
2✔
264
                                        'fileId' => $fileId
2✔
265
                                ]
2✔
266
                        ];
2✔
267
                        $this->signFileService->validateUserManager($data);
2✔
268
                        $this->validateHelper->validateExistingFile($data);
1✔
269
                        $this->validateHelper->validateIsSignerOfFile($fileUserId, $fileId);
1✔
270
                        $this->signFileService->unassociateToUser($fileId, $fileUserId);
1✔
271
                } catch (\Throwable $th) {
1✔
272
                        return new JSONResponse(
1✔
273
                                [
1✔
274
                                        'message' => $th->getMessage(),
1✔
275
                                ],
1✔
276
                                Http::STATUS_UNAUTHORIZED
1✔
277
                        );
1✔
278
                }
279
                return new JSONResponse(
1✔
280
                        [
1✔
281
                                'success' => true,
1✔
282
                                'message' => $this->l10n->t('Success')
1✔
283
                        ],
1✔
284
                        Http::STATUS_OK
1✔
285
                );
1✔
286
        }
287

288
        /**
289
         * @NoAdminRequired
290
         * @NoCSRFRequired
291
         *
292
         * @param integer $fileId
293
         * @return JSONResponse
294
         */
295
        public function deleteAllSignRequestUsingFileId(int $fileId) {
296
                try {
297
                        $data = [
2✔
298
                                'userManager' => $this->userSession->getUser(),
2✔
299
                                'file' => [
2✔
300
                                        'fileId' => $fileId
2✔
301
                                ]
2✔
302
                        ];
2✔
303
                        $this->signFileService->validateUserManager($data);
2✔
304
                        $this->validateHelper->validateExistingFile($data);
1✔
305
                        $this->signFileService->deleteSignRequest($data);
1✔
306
                } catch (\Throwable $th) {
1✔
307
                        return new JSONResponse(
1✔
308
                                [
1✔
309
                                        'message' => $th->getMessage(),
1✔
310
                                ],
1✔
311
                                Http::STATUS_UNAUTHORIZED
1✔
312
                        );
1✔
313
                }
314
                return new JSONResponse(
1✔
315
                        [
1✔
316
                                'success' => true,
1✔
317
                                'message' => $this->l10n->t('Success')
1✔
318
                        ],
1✔
319
                        Http::STATUS_OK
1✔
320
                );
1✔
321
        }
322

323
        /**
324
         * @NoAdminRequired
325
         * @NoCSRFRequired
326
         *
327
         * @param string $uuid
328
         * @param array $users
329
         * @return JSONResponse
330
         */
331
        public function getCodeUsingUuid(string $uuid): JSONResponse {
332
                return $this->getCode($uuid);
×
333
        }
334

335
        /**
336
         * @NoAdminRequired
337
         * @NoCSRFRequired
338
         *
339
         * @param string $uuid
340
         * @param array $users
341
         * @return JSONResponse
342
         */
343
        public function getCodeUsingFileId(string $fileId): JSONResponse {
344
                return $this->getCode(null, $fileId);
×
345
        }
346

347
        private function getCode(string $uuid = null, int $fileId = null): JSONResponse {
348
                $statusCode = null;
×
349
                try {
350
                        try {
351
                                $user = $this->userSession->getUser();
×
352
                                if ($fileId) {
×
353
                                        $fileUser = $this->fileUserMapper->getByFileIdAndUserId($fileId, $user->getUID());
×
354
                                } else {
355
                                        $fileUser = $this->fileUserMapper->getByUuidAndUserId($uuid, $user->getUID());
×
356
                                }
357
                        } catch (\Throwable $th) {
×
358
                                throw new LibresignException($this->l10n->t('Invalid data to sign file'), 1);
×
359
                        }
360
                        $this->validateHelper->canRequestCode();
×
361
                        $libreSignFile = $this->fileMapper->getById($fileUser->getFileId());
×
362
                        $this->validateHelper->fileCanBeSigned($libreSignFile);
×
363
                        $this->signFileService->requestCode($fileUser, $user);
×
364
                        $success = true;
×
365
                        $message = $this->l10n->t('The code to sign file was successfully requested.');
×
366
                } catch (SmsTransmissionException $e) {
×
367
                        $success = false;
×
368
                        $message = $this->l10n->t('Failed to send code.');
×
369
                        $statusCode = Http::STATUS_UNPROCESSABLE_ENTITY;
×
370
                } catch (\Throwable $th) {
×
371
                        $success = false;
×
372
                        $message = $th->getMessage();
×
373
                        $statusCode = Http::STATUS_UNPROCESSABLE_ENTITY;
×
374
                }
375
                return new JSONResponse(
×
376
                        [
×
377
                                'success' => $success,
×
378
                                'message' => [$message],
×
379
                        ],
×
380
                        $statusCode,
×
381
                );
×
382
        }
383
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc