CleanupDeletedUsers.php 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  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\IJob;
  13. use OCP\BackgroundJob\TimedJob;
  14. use OCP\EventDispatcher\IEventDispatcher;
  15. use OCP\IConfig;
  16. use Psr\Log\LoggerInterface;
  17. class CleanupDeletedUsers extends TimedJob {
  18. public function __construct(
  19. ITimeFactory $time,
  20. private Manager $userManager,
  21. private IConfig $config,
  22. private LoggerInterface $logger,
  23. ) {
  24. parent::__construct($time);
  25. $this->setTimeSensitivity(IJob::TIME_INSENSITIVE);
  26. $this->setInterval(24 * 3600);
  27. }
  28. protected function run($argument): void {
  29. $backend = new PartiallyDeletedUsersBackend($this->config);
  30. $users = $backend->getUsers();
  31. if (empty($users)) {
  32. $this->logger->debug('No failed deleted users found.');
  33. return;
  34. }
  35. foreach ($users as $userId) {
  36. if ($this->userManager->userExists($userId)) {
  37. $this->logger->info('Skipping user {userId}, marked as deleted, as they still exists in user backend.', ['userId' => $userId]);
  38. $backend->unmarkUser($userId);
  39. continue;
  40. }
  41. try {
  42. $user = new User(
  43. $userId,
  44. $backend,
  45. \OCP\Server::get(IEventDispatcher::class),
  46. $this->userManager,
  47. $this->config,
  48. );
  49. $user->delete();
  50. $this->logger->info('Cleaned up deleted user {userId}', ['userId' => $userId]);
  51. } catch (\Throwable $error) {
  52. $this->logger->warning('Could not cleanup deleted user {userId}', ['userId' => $userId, 'exception' => $error]);
  53. }
  54. }
  55. }
  56. }