aboutsummaryrefslogtreecommitdiffstats
path: root/lib/private/User/BackgroundJobs/CleanupDeletedUsers.php
blob: f999031a2a90a5cbb9b1bcb280b5201bd58d43dd (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
<?php

declare(strict_types=1);

/**
 * SPDX-FileCopyrightText: 2024 Nextcloud GmbH and Nextcloud contributors
 * SPDX-License-Identifier: AGPL-3.0-or-later
 */
namespace OC\User\BackgroundJobs;

use OC\User\Manager;
use OC\User\PartiallyDeletedUsersBackend;
use OC\User\User;
use OCP\AppFramework\Utility\ITimeFactory;
use OCP\BackgroundJob\TimedJob;
use OCP\EventDispatcher\IEventDispatcher;
use OCP\IConfig;
use Psr\Log\LoggerInterface;

class CleanupDeletedUsers extends TimedJob {
	public function __construct(
		ITimeFactory $time,
		private Manager $userManager,
		private IConfig $config,
		private LoggerInterface $logger,
	) {
		parent::__construct($time);
		$this->setTimeSensitivity(self::TIME_INSENSITIVE);
		$this->setInterval(24 * 60 * 60);
	}

	protected function run($argument): void {
		$backend = new PartiallyDeletedUsersBackend($this->config);
		$users = $backend->getUsers();

		if (empty($users)) {
			$this->logger->debug('No failed deleted users found.');
			return;
		}

		foreach ($users as $userId) {
			if ($this->userManager->userExists($userId)) {
				$this->logger->info('Skipping user {userId}, marked as deleted, as they still exists in user backend.', ['userId' => $userId]);
				$backend->unmarkUser($userId);
				continue;
			}

			try {
				$user = new User(
					$userId,
					$backend,
					\OCP\Server::get(IEventDispatcher::class),
					$this->userManager,
					$this->config,
				);
				$user->delete();
				$this->logger->info('Cleaned up deleted user {userId}', ['userId' => $userId]);
			} catch (\Throwable $error) {
				$this->logger->warning('Could not cleanup deleted user {userId}', ['userId' => $userId, 'exception' => $error]);
			}
		}
	}
}