diff options
author | Ferdinand Thiessen <opensource@fthiessen.de> | 2024-09-10 22:50:16 +0200 |
---|---|---|
committer | Ferdinand Thiessen <opensource@fthiessen.de> | 2024-09-26 20:48:37 +0200 |
commit | 16833aff863290e4b298a2e69015d97cd230be47 (patch) | |
tree | fe2da4b61695b27ad56be71e335abc80ca97c9cf /lib/private/User/BackgroundJobs | |
parent | c8a907fc8c1cddad7de9e9e453ede52d392ee2bd (diff) | |
download | nextcloud-server-16833aff863290e4b298a2e69015d97cd230be47.tar.gz nextcloud-server-16833aff863290e4b298a2e69015d97cd230be47.zip |
fix: Make user removal more resilient
Currently there is a problem if an exception is thrown in `User::delete`,
because at that point the user is already removed from the backend,
but not all data is deleted.
There is no way to recover from this state, as the user is gone no information is available anymore.
This means the data is still available on the server but can not removed by any API anymore.
The solution here is to first set a flag and backup the user home,
this can be used to recover failed user deletions in a way the delete can be re-tried.
Signed-off-by: Ferdinand Thiessen <opensource@fthiessen.de>
Diffstat (limited to 'lib/private/User/BackgroundJobs')
-rw-r--r-- | lib/private/User/BackgroundJobs/CleanupDeletedUsers.php | 55 |
1 files changed, 55 insertions, 0 deletions
diff --git a/lib/private/User/BackgroundJobs/CleanupDeletedUsers.php b/lib/private/User/BackgroundJobs/CleanupDeletedUsers.php new file mode 100644 index 00000000000..46ca2175c16 --- /dev/null +++ b/lib/private/User/BackgroundJobs/CleanupDeletedUsers.php @@ -0,0 +1,55 @@ +<?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\FailedUsersBackend; +use OC\User\Manager; +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->setInterval(3600); + } + + protected function run($argument): void { + $backend = new FailedUsersBackend($this->config); + $users = $backend->getUsers(); + + if (empty($users)) { + $this->logger->debug('No failed deleted users found.'); + return; + } + + foreach ($users as $userId) { + try { + $user = new User( + $userId, + $backend, + \OCP\Server::get(IEventDispatcher::class), + config: $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]); + } + } + } +} |