-
Notifications
You must be signed in to change notification settings - Fork 74
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
fix(Groups): take over members during migration #863
Merged
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
6063847
test(Behat): add scenario for takover of members during group migration
blizzz e2604db
fix(Groups): also migrate members from local to SAML group
blizzz 8f6eb98
enh(GroupMigration): add command to run member transfer
blizzz 9fde08d
enh(GroupMigration): repair step to catch up with unmigrated group me…
blizzz 69e423a
fix(GroupMigration): run job to remember old groups only once
blizzz 244abb4
test(Groups): integration test for the migration command
blizzz 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,93 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
/** | ||
* SPDX-FileCopyrightText: 2024 Nextcloud GmbH and Nextcloud contributors | ||
* SPDX-License-Identifier: AGPL-3.0-or-later | ||
*/ | ||
namespace OCA\User_SAML\Command; | ||
|
||
use OC\Core\Command\Base; | ||
use OCA\User_SAML\Service\GroupMigration; | ||
use Psr\Log\LoggerInterface; | ||
use Symfony\Component\Console\Input\InputInterface; | ||
use Symfony\Component\Console\Output\OutputInterface; | ||
use Throwable; | ||
|
||
class GroupMigrationCopyIncomplete extends Base { | ||
public function __construct( | ||
protected GroupMigration $groupMigration, | ||
protected LoggerInterface $logger, | ||
) { | ||
parent::__construct(); | ||
} | ||
protected function configure(): void { | ||
$this->setName('saml:group-migration:copy-incomplete-members'); | ||
$this->setDescription('Transfers remaining group members from old local to current SAML groups'); | ||
} | ||
|
||
protected function execute(InputInterface $input, OutputInterface $output): int { | ||
$groupsToTreat = $this->groupMigration->findGroupsWithLocalMembers(); | ||
if (empty($groupsToTreat)) { | ||
if ($output->isVerbose()) { | ||
$output->writeln('<info>No pending group member transfer</info>'); | ||
} | ||
return 0; | ||
} | ||
|
||
if (!$this->doMemberTransfer($groupsToTreat, $output)) { | ||
if (!$output->isQuiet()) { | ||
$output->writeln('<comment>Not all group members could be transferred completely. Rerun this command or check the Nextcloud log.</comment>'); | ||
} | ||
return 1; | ||
} | ||
|
||
if (!$output->isQuiet()) { | ||
$output->writeln('<info>All group members could be transferred completely.</info>'); | ||
} | ||
return 0; | ||
} | ||
|
||
/** | ||
* @param string[]|array<empty> $groups | ||
* @param OutputInterface $output | ||
* @return bool | ||
*/ | ||
protected function doMemberTransfer(array $groups, OutputInterface $output): bool { | ||
$errorOccurred = false; | ||
for ($i = 0; $i < 2; $i++) { | ||
$retry = []; | ||
foreach ($groups as $gid) { | ||
try { | ||
$isComplete = $this->groupMigration->migrateGroupUsers($gid); | ||
if (!$isComplete) { | ||
$retry[] = $gid; | ||
} else { | ||
$this->groupMigration->cleanUpOldGroupUsers($gid); | ||
if ($output->isVerbose()) { | ||
$output->writeln(sprintf('<info>Members transferred successfully for group %s</info>', $gid)); | ||
} | ||
} | ||
} catch (Throwable $e) { | ||
$errorOccurred = true; | ||
if (!$output->isQuiet()) { | ||
$output->writeln(sprintf('<error>Failed to transfer users from group %s: %s</error>', $gid, $e->getMessage())); | ||
} | ||
$this->logger->warning('Error while transferring group members of {gid}', ['gid' => $gid, 'exception' => $e]); | ||
} | ||
} | ||
if (empty($retry)) { | ||
return true; | ||
} | ||
/** @var string[]|array<empty> $groups */ | ||
$groups = $retry; | ||
} | ||
if (!empty($groups) && !$output->isQuiet()) { | ||
$output->writeln(sprintf( | ||
'<comment>Members not or incompletely transferred for groups: %s</comment>', | ||
implode(', ', $groups) | ||
)); | ||
} | ||
return empty($groups) && !$errorOccurred; | ||
} | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
/** | ||
* SPDX-FileCopyrightText: 2024 Nextcloud GmbH and Nextcloud contributors | ||
* SPDX-License-Identifier: AGPL-3.0-or-later | ||
*/ | ||
namespace OCA\User_SAML\Migration; | ||
|
||
use OCA\User_SAML\Service\GroupMigration; | ||
use OCP\Migration\IOutput; | ||
use OCP\Migration\IRepairStep; | ||
use Psr\Log\LoggerInterface; | ||
use Throwable; | ||
|
||
class TransferGroupMembers implements IRepairStep { | ||
|
||
public function __construct( | ||
private GroupMigration $groupMigration, | ||
private LoggerInterface $logger, | ||
) { | ||
} | ||
|
||
public function getName(): string { | ||
return 'Move potential left members from old local groups to SAML groups'; | ||
} | ||
|
||
public function run(IOutput $output): void { | ||
$groupsToTreat = $this->groupMigration->findGroupsWithLocalMembers(); | ||
if (empty($groupsToTreat)) { | ||
return; | ||
} | ||
$hasError = false; | ||
$output->startProgress(count($groupsToTreat)); | ||
foreach ($groupsToTreat as $gid) { | ||
try { | ||
if ($this->groupMigration->migrateGroupUsers($gid)) { | ||
$this->groupMigration->cleanUpOldGroupUsers($gid); | ||
} | ||
} catch (Throwable $e) { | ||
$hasError = true; | ||
$this->logger->warning('Error while transferring group members of {gid}', ['gid' => $gid, 'exception' => $e]); | ||
} finally { | ||
$output->advance(); | ||
} | ||
} | ||
$output->finishProgress(); | ||
if ($hasError) { | ||
$output->warning('There were errors while transferring group members to SAML groups. You may try later `occ saml:group-migration:copy-incomplete-members` later and check your nextcloud.log.'); | ||
} | ||
} | ||
} |
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Isn't the app already set when getting the LoggerInterface through DI?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I am never sure of that 🙈 Sometimes you see logs without an app set.