-
-
Notifications
You must be signed in to change notification settings - Fork 4k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[stable28] fix: Make user removal more resilient #48587
Open
backportbot
wants to merge
3
commits into
stable28
Choose a base branch
from
backport/47896/stable28
base: stable28
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+341
−78
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
30 changes: 30 additions & 0 deletions
30
lib/private/Repair/AddCleanupDeletedUsersBackgroundJob.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
/** | ||
* SPDX-FileCopyrightText: 2024 Nextcloud GmbH and Nextcloud contributors | ||
* SPDX-License-Identifier: AGPL-3.0-or-later | ||
*/ | ||
namespace OC\Repair; | ||
|
||
use OC\User\BackgroundJobs\CleanupDeletedUsers; | ||
use OCP\BackgroundJob\IJobList; | ||
use OCP\Migration\IOutput; | ||
use OCP\Migration\IRepairStep; | ||
|
||
class AddCleanupDeletedUsersBackgroundJob implements IRepairStep { | ||
private IJobList $jobList; | ||
|
||
public function __construct(IJobList $jobList) { | ||
$this->jobList = $jobList; | ||
} | ||
|
||
public function getName(): string { | ||
return 'Add cleanup-deleted-users background job'; | ||
} | ||
|
||
public function run(IOutput $output) { | ||
$this->jobList->add(CleanupDeletedUsers::class); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
<?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\IJob; | ||
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(IJob::TIME_INSENSITIVE); | ||
$this->setInterval(24 * 3600); | ||
} | ||
|
||
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]); | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
<?php | ||
|
||
/** | ||
* SPDX-FileCopyrightText: 2024 Nextcloud GmbH and Nextcloud contributors | ||
* SPDX-License-Identifier: AGPL-3.0-or-later | ||
*/ | ||
namespace OC\User; | ||
|
||
use OCP\IConfig; | ||
use OCP\IUserBackend; | ||
use OCP\User\Backend\IGetHomeBackend; | ||
|
||
/** | ||
* This is a "fake" backend for users that were deleted, | ||
* but not properly removed from Nextcloud (e.g. an exception occurred). | ||
* This backend is only needed because some APIs in user-deleted-events require a "real" user with backend. | ||
*/ | ||
class PartiallyDeletedUsersBackend extends Backend implements IGetHomeBackend, IUserBackend { | ||
|
||
public function __construct( | ||
private IConfig $config, | ||
) { | ||
} | ||
|
||
public function deleteUser($uid): bool { | ||
// fake true, deleting failed users is automatically handled by User::delete() | ||
return true; | ||
} | ||
|
||
public function getBackendName(): string { | ||
return 'deleted users'; | ||
} | ||
|
||
public function userExists($uid) { | ||
return $this->config->getUserValue($uid, 'core', 'deleted') === 'true'; | ||
} | ||
|
||
public function getHome(string $uid): string|false { | ||
return $this->config->getUserValue($uid, 'core', 'deleted.home-path') ?: false; | ||
} | ||
|
||
public function getUsers($search = '', $limit = null, $offset = null) { | ||
return $this->config->getUsersForUserValue('core', 'deleted', 'true'); | ||
} | ||
|
||
/** | ||
* Unmark a user as deleted. | ||
* This typically the case if the user deletion failed in the backend but before the backend deleted the user, | ||
* meaning the user still exists so we unmark them as it still can be accessed (and deleted) normally. | ||
*/ | ||
public function unmarkUser(string $userId): void { | ||
$this->config->deleteUserValue($userId, 'core', 'deleted'); | ||
$this->config->deleteUserValue($userId, 'core', 'deleted.home-path'); | ||
} | ||
|
||
} |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Check failure
Code scanning / Psalm
UndefinedMethod Error