1
0

CleanupDeletedUsers.php 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * SPDX-FileCopyrightText: 2024 Nextcloud GmbH and Nextcloud contributors
  5. * SPDX-License-Identifier: AGPL-3.0-or-later
  6. */
  7. namespace OC\User\BackgroundJobs;
  8. use OC\User\Manager;
  9. use OC\User\PartiallyDeletedUsersBackend;
  10. use OC\User\User;
  11. use OCP\AppFramework\Utility\ITimeFactory;
  12. use OCP\BackgroundJob\TimedJob;
  13. use OCP\EventDispatcher\IEventDispatcher;
  14. use OCP\IConfig;
  15. use Psr\Log\LoggerInterface;
  16. class CleanupDeletedUsers extends TimedJob {
  17. public function __construct(
  18. ITimeFactory $time,
  19. private Manager $userManager,
  20. private IConfig $config,
  21. private LoggerInterface $logger,
  22. ) {
  23. parent::__construct($time);
  24. $this->setTimeSensitivity(self::TIME_INSENSITIVE);
  25. $this->setInterval(24 * 60 * 60);
  26. }
  27. protected function run($argument): void {
  28. $backend = new PartiallyDeletedUsersBackend($this->config);
  29. $users = $backend->getUsers();
  30. if (empty($users)) {
  31. $this->logger->debug('No failed deleted users found.');
  32. return;
  33. }
  34. foreach ($users as $userId) {
  35. if ($this->userManager->userExists($userId)) {
  36. $this->logger->info('Skipping user {userId}, marked as deleted, as they still exists in user backend.', ['userId' => $userId]);
  37. $backend->unmarkUser($userId);
  38. continue;
  39. }
  40. try {
  41. $user = new User(
  42. $userId,
  43. $backend,
  44. \OCP\Server::get(IEventDispatcher::class),
  45. $this->userManager,
  46. $this->config,
  47. );
  48. $user->delete();
  49. $this->logger->info('Cleaned up deleted user {userId}', ['userId' => $userId]);
  50. } catch (\Throwable $error) {
  51. $this->logger->warning('Could not cleanup deleted user {userId}', ['userId' => $userId, 'exception' => $error]);
  52. }
  53. }
  54. }
  55. }