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

LibreSign / libresign / 21453337228

28 Jan 2026 07:55PM UTC coverage: 46.319%. First build
21453337228

Pull #6595

github

web-flow
Merge 118ea4828 into 8448333b8
Pull Request #6595: feat: persist sorting preferences

0 of 4 new or added lines in 2 files covered. (0.0%)

7765 of 16764 relevant lines covered (46.32%)

5.04 hits per line

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

18.32
/lib/Service/AccountService.php
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;
10

11
use InvalidArgumentException;
12
use OCA\Libresign\AppInfo\Application;
13
use OCA\Libresign\Db\FileMapper;
14
use OCA\Libresign\Db\FileTypeMapper;
15
use OCA\Libresign\Db\IdentifyMethodMapper;
16
use OCA\Libresign\Db\SignRequest;
17
use OCA\Libresign\Db\SignRequestMapper;
18
use OCA\Libresign\Db\UserElement;
19
use OCA\Libresign\Db\UserElementMapper;
20
use OCA\Libresign\Enum\FileStatus;
21
use OCA\Libresign\Exception\InvalidPasswordException;
22
use OCA\Libresign\Exception\LibresignException;
23
use OCA\Libresign\Handler\CertificateEngine\CertificateEngineFactory;
24
use OCA\Libresign\Handler\SignEngine\Pkcs12Handler;
25
use OCA\Libresign\Helper\FileUploadHelper;
26
use OCA\Libresign\Helper\ValidateHelper;
27
use OCA\Settings\Mailer\NewUserMailHelper;
28
use OCP\Accounts\IAccountManager;
29
use OCP\AppFramework\Db\DoesNotExistException;
30
use OCP\AppFramework\Utility\ITimeFactory;
31
use OCP\Files\Config\IMountProviderCollection;
32
use OCP\Files\File;
33
use OCP\Files\IMimeTypeDetector;
34
use OCP\Files\IRootFolder;
35
use OCP\Files\NotFoundException;
36
use OCP\Http\Client\IClientService;
37
use OCP\IAppConfig;
38
use OCP\IConfig;
39
use OCP\IGroupManager;
40
use OCP\IL10N;
41
use OCP\IURLGenerator;
42
use OCP\IUser;
43
use OCP\IUserManager;
44
use Sabre\DAV\UUIDUtil;
45
use Throwable;
46

47
class AccountService {
48
        private ?SignRequest $signRequest = null;
49
        private ?\OCA\Libresign\Db\File $fileData = null;
50
        private \OCP\Files\File $fileToSign;
51

52
        public function __construct(
53
                private IL10N $l10n,
54
                private SignRequestMapper $signRequestMapper,
55
                private IUserManager $userManager,
56
                private IAccountManager $accountManager,
57
                private IRootFolder $root,
58
                private IMimeTypeDetector $mimeTypeDetector,
59
                private FileMapper $fileMapper,
60
                private FileTypeMapper $fileTypeMapper,
61
                private SignFileService $signFileService,
62
                private RequestSignatureService $requestSignatureService,
63
                private CertificateEngineFactory $certificateEngineFactory,
64
                private IConfig $config,
65
                private IAppConfig $appConfig,
66
                private IMountProviderCollection $mountProviderCollection,
67
                private NewUserMailHelper $newUserMail,
68
                private IdentifyMethodService $identifyMethodService,
69
                private IdentifyMethodMapper $identifyMethodMapper,
70
                private ValidateHelper $validateHelper,
71
                private IURLGenerator $urlGenerator,
72
                private Pkcs12Handler $pkcs12Handler,
73
                private IGroupManager $groupManager,
74
                private IdDocsService $idDocsService,
75
                private SignerElementsService $signerElementsService,
76
                private UserElementMapper $userElementMapper,
77
                private FolderService $folderService,
78
                private IClientService $clientService,
79
                private ITimeFactory $timeFactory,
80
                private FileUploadHelper $uploadHelper,
81
        ) {
82
        }
41✔
83

84
        public function validateCreateToSign(array $data): void {
85
                if (!UUIDUtil::validateUUID($data['uuid'])) {
1✔
86
                        throw new LibresignException($this->l10n->t('Invalid UUID'), 1);
1✔
87
                }
88
                try {
89
                        $signRequest = $this->getSignRequestByUuid($data['uuid']);
×
90
                } catch (\Throwable) {
×
91
                        throw new LibresignException($this->l10n->t('UUID not found'), 1);
×
92
                }
93
                $identifyMethods = $this->identifyMethodService->getIdentifyMethodsFromSignRequestId($signRequest->getId());
×
94
                if (!array_key_exists('identify', $data['user'])) {
×
95
                        throw new LibresignException($this->l10n->t('Invalid identification method'), 1);
×
96
                }
97
                foreach ($data['user']['identify'] as $method => $value) {
×
98
                        if (!array_key_exists($method, $identifyMethods)) {
×
99
                                throw new LibresignException($this->l10n->t('Invalid identification method'), 1);
×
100
                        }
101
                        foreach ($identifyMethods[$method] as $identifyMethod) {
×
102
                                $identifyMethod->validateToCreateAccount($value);
×
103
                        }
104
                }
105
                if (empty($data['password'])) {
×
106
                        throw new LibresignException($this->l10n->t('Password is mandatory'), 1);
×
107
                }
108
                $file = $this->getFileByUuid($data['uuid']);
×
109
                if (empty($file['fileToSign'])) {
×
110
                        throw new LibresignException($this->l10n->t('File not found'));
×
111
                }
112
        }
113

114
        public function getFileByUuid(string $uuid): array {
115
                $signRequest = $this->getSignRequestByUuid($uuid);
×
116
                if (!$this->fileData instanceof \OCA\Libresign\Db\File) {
×
117
                        $this->fileData = $this->fileMapper->getById($signRequest->getFileId());
×
118

119
                        $nodeId = $this->fileData->getNodeId();
×
120

121
                        $fileToSign = $this->root->getUserFolder($this->fileData->getUserId())->getFirstNodeById($nodeId);
×
122
                        if ($fileToSign) {
×
123
                                $this->fileToSign = $fileToSign;
×
124
                        }
125
                }
126
                return [
×
127
                        'fileData' => $this->fileData,
×
128
                        'fileToSign' => $this->fileToSign
×
129
                ];
×
130
        }
131

132
        public function validateCertificateData(array $data): void {
133
                if (array_key_exists('email', $data['user']) && empty($data['user']['email'])) {
7✔
134
                        throw new LibresignException($this->l10n->t('You must have an email. You can define the email in your profile.'), 1);
2✔
135
                }
136
                if (!empty($data['user']['email']) && !filter_var($data['user']['email'], FILTER_VALIDATE_EMAIL)) {
5✔
137
                        throw new LibresignException($this->l10n->t('Invalid email'), 1);
2✔
138
                }
139
                if (empty($data['signPassword'])) {
3✔
140
                        throw new LibresignException($this->l10n->t('Password to sign is mandatory'), 1);
1✔
141
                }
142
        }
143

144
        /**
145
         * Get signRequest by Uuid
146
         */
147
        public function getSignRequestByUuid(string $uuid): SignRequest {
148
                if (!$this->signRequest instanceof SignRequest) {
2✔
149
                        $this->signRequest = $this->signRequestMapper->getByUuid($uuid);
2✔
150
                }
151
                return $this->signRequest;
2✔
152
        }
153

154
        public function createToSign(string $uuid, string $email, string $password, ?string $signPassword): void {
155
                $signRequest = $this->getSignRequestByUuid($uuid);
2✔
156

157
                $newUser = $this->userManager->createUser($email, $password);
2✔
158
                $newUser->setDisplayName($signRequest->getDisplayName());
2✔
159
                $newUser->setSystemEMailAddress($email);
2✔
160

161
                $this->updateIdentifyMethodToAccount($signRequest->getId(), $email, $newUser->getUID());
2✔
162

163
                if ($this->config->getAppValue('core', 'newUser.sendEmail', 'yes') === 'yes') {
2✔
164
                        try {
165
                                $emailTemplate = $this->newUserMail->generateTemplate($newUser, false);
2✔
166
                                $this->newUserMail->sendMail($newUser, $emailTemplate);
2✔
167
                        } catch (\Exception) {
2✔
168
                                throw new LibresignException('Unable to send the invitation', 1);
2✔
169
                        }
170
                }
171

172
                if ($signPassword) {
×
173
                        $certificate = $this->pkcs12Handler->generateCertificate(
×
174
                                [
×
175
                                        'host' => $newUser->getPrimaryEMailAddress(),
×
176
                                        'uid' => 'account:' . $newUser->getUID(),
×
177
                                        'name' => $newUser->getDisplayName()
×
178
                                ],
×
179
                                $signPassword,
×
180
                                $newUser->getDisplayName()
×
181
                        );
×
182
                        $this->pkcs12Handler->savePfx($newUser->getPrimaryEMailAddress(), $certificate);
×
183
                }
184
        }
185

186
        public function getCertificateEngineName(): string {
187
                return $this->certificateEngineFactory->getEngine()->getName();
×
188
        }
189

190
        /**
191
         * @return array<string, mixed>
192
         */
193
        public function getConfig(?IUser $user = null): array {
194
                $info['identificationDocumentsFlow'] = $this->appConfig->getValueBool(Application::APP_ID, 'identification_documents', false);
×
195
                $info['hasSignatureFile'] = $this->hasSignatureFile($user);
×
196
                $info['phoneNumber'] = $this->getPhoneNumber($user);
×
197
                $info['isApprover'] = $this->validateHelper->userCanApproveValidationDocuments($user, false);
×
198
                $info['id_docs_filters'] = $this->getUserConfigIdDocsFilters($user);
×
199
                $info['id_docs_sort'] = $this->getUserConfigIdDocsSort($user);
×
200
                $info['crl_filters'] = $this->getUserConfigCrlFilters($user);
×
201
                $info['crl_sort'] = $this->getUserConfigCrlSort($user);
×
202
                $info['grid_view'] = $this->getUserConfigByKey('grid_view', $user) === '1';
×
203
                $info['signer_identify_tab'] = $this->getUserConfigByKey('signer_identify_tab', $user);
×
204

205
                return array_filter($info);
×
206
        }
207

208
        public function getConfigFilters(?IUser $user = null): array {
209
                $info['filter_modified'] = $this->getUserConfigByKey('filter_modified', $user);
×
210
                $info['filter_status'] = $this->getUserConfigByKey('filter_status', $user);
×
211

212
                return $info;
×
213
        }
214

215
        public function getConfigSorting(?IUser $user = null): array {
NEW
216
                $info['sorting_mode'] = $this->getUserConfigByKey('sorting_mode', $user) ?: 'name';
×
NEW
217
                $info['sorting_direction'] = $this->getUserConfigByKey('sorting_direction', $user) ?: 'asc';
×
218

NEW
219
                return $info;
×
220
        }
221

222
        private function updateIdentifyMethodToAccount(int $signRequestId, string $email, string $uid): void {
223
                $identifyMethods = $this->identifyMethodService->getIdentifyMethodsFromSignRequestId($signRequestId);
2✔
224
                foreach ($identifyMethods as $name => $methods) {
2✔
225
                        if ($name === IdentifyMethodService::IDENTIFY_EMAIL) {
×
226
                                foreach ($methods as $identifyMethod) {
×
227
                                        $entity = $identifyMethod->getEntity();
×
228
                                        if ($entity->getIdentifierValue() === $email) {
×
229
                                                $entity->setIdentifierKey(IdentifyMethodService::IDENTIFY_ACCOUNT);
×
230
                                                $entity->setIdentifierValue($uid);
×
231
                                                $this->identifyMethodMapper->update($entity);
×
232
                                        }
233
                                }
234
                        }
235
                }
236
        }
237

238
        private function getPhoneNumber(?IUser $user): string {
239
                if (!$user) {
×
240
                        return '';
×
241
                }
242
                $userAccount = $this->accountManager->getAccount($user);
×
243
                return $userAccount->getProperty(IAccountManager::PROPERTY_PHONE)->getValue();
×
244
        }
245

246
        public function hasSignatureFile(?IUser $user = null): bool {
247
                if (!$user) {
1✔
248
                        return false;
×
249
                }
250
                try {
251
                        $this->pkcs12Handler->getPfxOfCurrentSigner($user->getUID());
1✔
252
                        return true;
×
253
                } catch (LibresignException) {
1✔
254
                        return false;
1✔
255
                }
256
        }
257

258
        private function getUserConfigByKey(string $key, ?IUser $user = null): string {
259
                if (!$user) {
×
260
                        return '';
×
261
                }
262

263
                return $this->config->getUserValue($user->getUID(), Application::APP_ID, $key);
×
264
        }
265

266
        private function getUserConfigIdDocsFilters(?IUser $user = null): array {
267
                if (!$user) {
×
268
                        return [];
×
269
                }
270

271
                $value = $this->config->getUserValue($user->getUID(), Application::APP_ID, 'id_docs_filters', '');
×
272
                if (empty($value)) {
×
273
                        return [];
×
274
                }
275

276
                $decoded = json_decode($value, true);
×
277
                return is_array($decoded) ? $decoded : [];
×
278
        }
279

280
        private function getUserConfigCrlFilters(?IUser $user = null): array {
281
                if (!$user || !$this->groupManager->isAdmin($user->getUID())) {
×
282
                        return [];
×
283
                }
284

285
                $value = $this->config->getUserValue($user->getUID(), Application::APP_ID, 'crl_filters', '');
×
286
                if (empty($value)) {
×
287
                        return [];
×
288
                }
289

290
                $decoded = json_decode($value, true);
×
291
                return is_array($decoded) ? $decoded : [];
×
292
        }
293

294
        private function getUserConfigCrlSort(?IUser $user): array {
295
                if (!$user || !$this->groupManager->isAdmin($user->getUID())) {
×
296
                        return ['sortBy' => 'revoked_at', 'sortOrder' => 'DESC'];
×
297
                }
298

299
                $value = $this->config->getUserValue($user->getUID(), Application::APP_ID, 'crl_sort', '');
×
300
                if (empty($value)) {
×
301
                        return ['sortBy' => 'revoked_at', 'sortOrder' => 'DESC'];
×
302
                }
303

304
                $decoded = json_decode($value, true);
×
305
                return is_array($decoded) ? $decoded : ['sortBy' => 'revoked_at', 'sortOrder' => 'DESC'];
×
306
        }
307

308
        private function getUserConfigIdDocsSort(?IUser $user): array {
309
                if (!$user || !$this->validateHelper->userCanApproveValidationDocuments($user, false)) {
×
310
                        return ['sortBy' => null, 'sortOrder' => null];
×
311
                }
312

313
                $value = $this->config->getUserValue($user->getUID(), Application::APP_ID, 'id_docs_sort', '');
×
314
                if (empty($value)) {
×
315
                        return ['sortBy' => null, 'sortOrder' => null];
×
316
                }
317

318
                $decoded = json_decode($value, true);
×
319
                return is_array($decoded) ? $decoded : ['sortBy' => null, 'sortOrder' => null];
×
320
        }
321

322
        /**
323
         * Get PDF node by UUID
324
         *
325
         * @psalm-suppress MixedReturnStatement
326
         * @throws Throwable
327
         * @return \OCP\Files\File
328
         */
329
        public function getPdfByUuid(string $uuid): File {
330
                $fileData = $this->fileMapper->getByUuid($uuid);
4✔
331

332
                if (in_array($fileData->getStatus(), [FileStatus::PARTIAL_SIGNED->value, FileStatus::SIGNED->value])) {
4✔
333
                        $nodeId = $fileData->getSignedNodeId();
3✔
334
                } else {
335
                        $nodeId = $fileData->getNodeId();
1✔
336
                }
337
                if ($nodeId === null) {
4✔
338
                        throw new DoesNotExistException('Not found');
×
339
                }
340
                $file = $this->root->getUserFolder($fileData->getUserId())->getFirstNodeById($nodeId);
4✔
341
                if (!$file instanceof File) {
4✔
342
                        throw new DoesNotExistException('Not found');
×
343
                }
344
                return $file;
4✔
345
        }
346

347
        public function getFileByNodeId(int $nodeId): File {
348
                try {
349
                        return $this->folderService->getFileByNodeId($nodeId);
×
350
                } catch (NotFoundException) {
×
351
                        throw new DoesNotExistException('Not found');
×
352
                }
353
        }
354

355
        public function canRequestSign(?IUser $user = null): bool {
356
                if (!$user) {
9✔
357
                        return false;
2✔
358
                }
359
                $authorized = $this->appConfig->getValueArray(Application::APP_ID, 'groups_request_sign', ['admin']);
7✔
360
                if (empty($authorized)) {
7✔
361
                        return false;
2✔
362
                }
363
                $userGroups = $this->groupManager->getUserGroupIds($user);
5✔
364
                if (!array_intersect($userGroups, $authorized)) {
5✔
365
                        return false;
3✔
366
                }
367
                return true;
2✔
368
        }
369

370
        public function getSettings(?IUser $user = null): array {
371
                $return['canRequestSign'] = $this->canRequestSign($user);
1✔
372
                $return['hasSignatureFile'] = $this->hasSignatureFile($user);
1✔
373
                return $return;
1✔
374
        }
375

376
        public function addFilesToAccount(array $files, IUser $user): void {
377
                $this->idDocsService->addIdDocs($files, $user);
×
378
        }
379

380
        public function deleteFileFromAccount(int $nodeId, IUser $user): void {
381
                $this->idDocsService->deleteIdDoc($nodeId, $user);
×
382
        }
383

384
        public function saveVisibleElements(array $elements, string $sessionId, ?IUser $user): void {
385
                foreach ($elements as $element) {
×
386
                        $this->saveVisibleElement($element, $sessionId, $user);
×
387
                }
388
        }
389

390
        public function saveVisibleElement(array $data, string $sessionId, ?IUser $user): void {
391
                if (isset($data['elementId'])) {
×
392
                        $this->updateFileOfVisibleElement($data);
×
393
                        $this->updateDataOfVisibleElement($data);
×
394
                } elseif ($user instanceof IUser) {
×
395
                        $file = $this->saveFileOfVisibleElementUsingUser($data, $user);
×
396
                        $this->insertVisibleElement($data, $user, $file);
×
397
                } else {
398
                        $file = $this->saveFileOfVisibleElementUsingSession($data, $sessionId);
×
399
                }
400
        }
401

402
        private function updateFileOfVisibleElement(array $data): void {
403
                if (!isset($data['file'])) {
×
404
                        return;
×
405
                }
406
                $userElement = $this->userElementMapper->findOne(['id' => $data['elementId']]);
×
407
                $file = $this->folderService->getFileByNodeId($userElement->getNodeId());
×
408
                $file->putContent($this->getFileRaw($data));
×
409
        }
410

411
        private function updateDataOfVisibleElement(array $data): void {
412
                if (!isset($data['starred'])) {
×
413
                        return;
×
414
                }
415
                $userElement = $this->userElementMapper->findOne(['id' => $data['elementId']]);
×
416
                $userElement->setStarred($data['starred'] ? 1 : 0);
×
417
                $this->userElementMapper->update($userElement);
×
418
        }
419

420
        private function saveFileOfVisibleElementUsingUser(array $data, IUser $user): File {
421
                $rootSignatureFolder = $this->folderService->getFolder();
×
422
                $folderName = $this->folderService->getFolderName($data, $user);
×
423
                $folderToFile = $rootSignatureFolder->newFolder($folderName);
×
424
                return $folderToFile->newFile(UUIDUtil::getUUID() . '.png', $this->getFileRaw($data));
×
425
        }
426

427
        private function saveFileOfVisibleElementUsingSession(array $data, string $sessionId): File {
428
                if (!empty($data['nodeId'])) {
×
429
                        return $this->updateFileOfVisibleElementUsingSession($data, $sessionId);
×
430
                }
431
                return $this->createFileOfVisibleElementUsingSession($data, $sessionId);
×
432
        }
433

434
        private function updateFileOfVisibleElementUsingSession(array $data, string $sessionId): File {
435
                $fileList = $this->signerElementsService->getElementsFromSession();
×
436
                $element = array_filter($fileList, fn (File $element) => $element->getId() === $data['nodeId']);
×
437
                $element = current($element);
×
438
                if (!$element instanceof File) {
×
439
                        throw new \Exception($this->l10n->t('File not found'));
×
440
                }
441
                $element->putContent($this->getFileRaw($data));
×
442
                return $element;
×
443
        }
444

445
        private function createFileOfVisibleElementUsingSession(array $data, string $sessionId): File {
446
                $rootSignatureFolder = $this->folderService->getFolder();
×
447
                $folderName = $sessionId;
×
448
                $folderToFile = $rootSignatureFolder->newFolder($folderName);
×
449
                $filename = implode(
×
450
                        '_',
×
451
                        [
×
452
                                $data['type'],
×
453
                                $this->timeFactory->getDateTime()->getTimestamp(),
×
454
                        ]
×
455
                ) . '.png';
×
456
                return $folderToFile->newFile($filename, $this->getFileRaw($data));
×
457
        }
458

459
        private function insertVisibleElement(array $data, IUser $user, File $file): void {
460
                $userElement = new UserElement();
×
461
                $userElement->setType($data['type']);
×
462
                $userElement->setNodeId($file->getId());
×
463
                $userElement->setUserId($user->getUID());
×
464
                $userElement->setStarred(isset($data['starred']) && $data['starred'] ? 1 : 0);
×
465
                $userElement->setCreatedAt($this->timeFactory->getDateTime());
×
466
                $this->userElementMapper->insert($userElement);
×
467
        }
468

469
        private function getFileRaw(array $data): string {
470
                if (!empty($data['file']['url'])) {
×
471
                        if (!filter_var($data['file']['url'], FILTER_VALIDATE_URL)) {
×
472
                                throw new \Exception($this->l10n->t('Invalid URL file'));
×
473
                        }
474
                        $response = $this->clientService->newClient()->get($data['file']['url']);
×
475
                        $contentType = $response->getHeader('Content-Type');
×
476
                        if ($contentType !== 'image/png') {
×
477
                                throw new \Exception($this->l10n->t('Visible element file must be png.'));
×
478
                        }
479
                        $content = (string)$response->getBody();
×
480
                        if (empty($content)) {
×
481
                                throw new \Exception($this->l10n->t('Empty file'));
×
482
                        }
483
                        $this->validateHelper->validateBase64($content, ValidateHelper::TYPE_VISIBLE_ELEMENT_USER);
×
484
                        return $content;
×
485
                }
486
                $this->validateHelper->validateBase64($data['file']['base64'], ValidateHelper::TYPE_VISIBLE_ELEMENT_USER);
×
487
                $withMime = explode(',', (string)$data['file']['base64']);
×
488
                if (count($withMime) === 2) {
×
489
                        $content = base64_decode($withMime[1]);
×
490
                } else {
491
                        $content = base64_decode((string)$data['file']['base64']);
×
492
                }
493
                if (!$content) {
×
494
                        return '';
×
495
                }
496
                return $content;
×
497
        }
498

499
        public function deleteSignatureElement(?IUser $user, string $sessionId, int $nodeId): void {
500
                if ($user instanceof IUser) {
×
501
                        $element = $this->userElementMapper->findOne([
×
502
                                'node_id' => $nodeId,
×
503
                                'user_id' => $user->getUID(),
×
504
                        ]);
×
505
                        $this->userElementMapper->delete($element);
×
506
                        try {
507
                                $file = $this->folderService->getFileByNodeId($element->getNodeId());
×
508
                                $file->delete();
×
509
                        } catch (NotFoundException) {
×
510
                        }
511
                } else {
512
                        $rootSignatureFolder = $this->folderService->getFolder();
×
513
                        $folderName = $sessionId;
×
514
                        $rootSignatureFolder->delete($folderName);
×
515
                }
516
        }
517

518
        /**
519
         * @throws LibresignException at savePfx
520
         * @throws InvalidArgumentException
521
         */
522
        public function uploadPfx(array $file, IUser $user): void {
523
                try {
524
                        $this->uploadHelper->validateUploadedFile($file);
×
525
                } catch (InvalidArgumentException) {
×
526
                        // TRANSLATORS Error when the uploaded certificate file is not valid
527
                        throw new InvalidArgumentException($this->l10n->t('Invalid file provided. Need to be a .pfx file.'));
×
528
                }
529

530
                if ($file['size'] > 10 * 1024) {
×
531
                        // TRANSLATORS Error when the certificate file is bigger than normal
532
                        throw new InvalidArgumentException($this->l10n->t('File is too big'));
×
533
                }
534
                $content = file_get_contents($file['tmp_name']);
×
535
                $mimetype = $this->mimeTypeDetector->detectString($content);
×
536
                if ($mimetype !== 'application/octet-stream') {
×
537
                        // TRANSLATORS Error when the mimetype of uploaded file is not valid
538
                        throw new InvalidArgumentException($this->l10n->t('Invalid file provided. Need to be a .pfx file.'));
×
539
                }
540
                $extension = strtolower(pathinfo((string)$file['name'], PATHINFO_EXTENSION));
×
541
                if ($extension !== 'pfx') {
×
542
                        // TRANSLATORS Error when the certificate file is not a pfx file
543
                        throw new InvalidArgumentException($this->l10n->t('Invalid file provided. Need to be a .pfx file.'));
×
544
                }
545
                unlink($file['tmp_name']);
×
546
                $this->pkcs12Handler->savePfx($user->getUID(), $content);
×
547
        }
548

549
        public function deletePfx(IUser $user): void {
550
                $this->pkcs12Handler->deletePfx($user->getUID());
×
551
        }
552

553
        /**
554
         * @throws LibresignException when have not a certificate file
555
         */
556
        public function updatePfxPassword(IUser $user, string $current, string $new): void {
557
                try {
558
                        $pfx = $this->pkcs12Handler->updatePassword($user->getUID(), $current, $new);
×
559
                } catch (InvalidPasswordException) {
×
560
                        throw new LibresignException($this->l10n->t('Invalid user or password'));
×
561
                }
562
        }
563

564
        /**
565
         * @throws LibresignException when have not a certificate file
566
         */
567
        public function readPfxData(IUser $user, string $password): array {
568
                try {
569
                        return $this->pkcs12Handler
×
570
                                ->setCertificate($this->pkcs12Handler->getPfxOfCurrentSigner($user->getUID()))
×
571
                                ->setPassword($password)
×
572
                                ->readCertificate();
×
573
                } catch (InvalidPasswordException) {
×
574
                        throw new LibresignException($this->l10n->t('Invalid user or password'));
×
575
                }
576
        }
577
}
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

© 2026 Coveralls, Inc