Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
100.00% |
29 / 29 |
|
100.00% |
4 / 4 |
CRAP | |
100.00% |
1 / 1 |
| UserDeleted | |
100.00% |
29 / 29 |
|
100.00% |
4 / 4 |
7 | |
100.00% |
1 / 1 |
| __construct | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| run | |
100.00% |
9 / 9 |
|
100.00% |
1 / 1 |
2 | |||
| revokeCertificates | |
100.00% |
16 / 16 |
|
100.00% |
1 / 1 |
3 | |||
| neutralizeUserData | |
100.00% |
3 / 3 |
|
100.00% |
1 / 1 |
1 | |||
| 1 | <?php |
| 2 | |
| 3 | declare(strict_types=1); |
| 4 | /** |
| 5 | * SPDX-FileCopyrightText: 2024-2024 LibreCode coop and contributors |
| 6 | * SPDX-License-Identifier: AGPL-3.0-or-later |
| 7 | */ |
| 8 | |
| 9 | namespace OCA\Libresign\BackgroundJob; |
| 10 | |
| 11 | use OCA\Libresign\Db\FileMapper; |
| 12 | use OCA\Libresign\Db\IdentifyMethodMapper; |
| 13 | use OCA\Libresign\Db\UserElementMapper; |
| 14 | use OCA\Libresign\Enum\CRLReason; |
| 15 | use OCA\Libresign\Service\Crl\CrlService; |
| 16 | use OCP\AppFramework\Utility\ITimeFactory; |
| 17 | use OCP\BackgroundJob\QueuedJob; |
| 18 | use Psr\Log\LoggerInterface; |
| 19 | |
| 20 | class UserDeleted extends QueuedJob { |
| 21 | public function __construct( |
| 22 | protected FileMapper $fileMapper, |
| 23 | protected IdentifyMethodMapper $identifyMethodMapper, |
| 24 | protected UserElementMapper $userElementMapper, |
| 25 | protected CrlService $crlService, |
| 26 | protected ITimeFactory $time, |
| 27 | protected LoggerInterface $logger, |
| 28 | ) { |
| 29 | parent::__construct($time); |
| 30 | } |
| 31 | |
| 32 | /** |
| 33 | * @param array $argument |
| 34 | */ |
| 35 | #[\Override] |
| 36 | public function run($argument): void { |
| 37 | if (!isset($argument['user_id'])) { |
| 38 | return; |
| 39 | } |
| 40 | $userId = $argument['user_id']; |
| 41 | $displayName = $argument['display_name']; |
| 42 | $this->logger->info('Neutralizing data for deleted user {user}', [ |
| 43 | 'user' => $userId |
| 44 | ]); |
| 45 | |
| 46 | $this->revokeCertificates($userId); |
| 47 | $this->neutralizeUserData($userId, $displayName); |
| 48 | } |
| 49 | |
| 50 | private function revokeCertificates(string $userId): void { |
| 51 | try { |
| 52 | $revokedCount = $this->crlService->revokeUserCertificates( |
| 53 | $userId, |
| 54 | CRLReason::CESSATION_OF_OPERATION, |
| 55 | 'User account deleted', |
| 56 | 'system' |
| 57 | ); |
| 58 | |
| 59 | if ($revokedCount > 0) { |
| 60 | $this->logger->info('Revoked {count} certificate(s) for deleted user {user}', [ |
| 61 | 'count' => $revokedCount, |
| 62 | 'user' => $userId |
| 63 | ]); |
| 64 | } |
| 65 | } catch (\Exception $e) { |
| 66 | $this->logger->error('Failed to revoke certificates for deleted user {user}: {error}', [ |
| 67 | 'user' => $userId, |
| 68 | 'error' => $e->getMessage() |
| 69 | ]); |
| 70 | } |
| 71 | } |
| 72 | |
| 73 | private function neutralizeUserData(string $userId, string $displayName): void { |
| 74 | $this->fileMapper->neutralizeDeletedUser($userId, $displayName); |
| 75 | $this->identifyMethodMapper->neutralizeDeletedUser($userId, $displayName); |
| 76 | $this->userElementMapper->neutralizeDeletedUser($userId, $displayName); |
| 77 | } |
| 78 | } |