-
Notifications
You must be signed in to change notification settings - Fork 74
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
enh(GroupMigration): add command to run member transfer
Signed-off-by: Arthur Schiwon <[email protected]>
- Loading branch information
Showing
6 changed files
with
241 additions
and
48 deletions.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,101 @@ | ||
<?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\Service; | ||
|
||
use OCA\User_SAML\GroupBackend; | ||
use OCP\AppFramework\Db\TTransactional; | ||
use OCP\DB\Exception; | ||
use OCP\DB\QueryBuilder\IQueryBuilder; | ||
use OCP\IDBConnection; | ||
use OCP\IGroupManager; | ||
use OCP\IUser; | ||
use Psr\Log\LoggerInterface; | ||
use Throwable; | ||
|
||
class GroupMigration { | ||
use TTransactional; | ||
|
||
protected const CHUNK_SIZE = 1000; | ||
|
||
public function __construct( | ||
protected GroupBackend $ownGroupBackend, | ||
protected IGroupManager $groupManager, | ||
protected IDBConnection $dbc, | ||
protected LoggerInterface $logger, | ||
) { | ||
} | ||
|
||
/** | ||
* @return string[] group ids | ||
*/ | ||
public function findGroupsWithLocalMembers(): array { | ||
$foundGroups = []; | ||
|
||
$qb = $this->dbc->getQueryBuilder(); | ||
$qb->selectDistinct('gid') | ||
->from('group_user') | ||
->where($qb->expr()->in('gid', $qb->createParameter('gidList'))); | ||
|
||
$allOwnedGroups = $this->ownGroupBackend->getGroups(); | ||
foreach (array_chunk($allOwnedGroups, self::CHUNK_SIZE) as $groupsChunk) { | ||
$qb->setParameter('gidList', $groupsChunk, IQueryBuilder::PARAM_STR_ARRAY); | ||
$result = $qb->executeQuery(); | ||
while ($gid = $result->fetchOne()) { | ||
$foundGroups[] = $gid; | ||
} | ||
$result->closeCursor(); | ||
} | ||
|
||
return $foundGroups; | ||
} | ||
|
||
/** | ||
* @returns bool true when all users were migrated, when they were only partly migrated | ||
* @throws Exception | ||
* @throws Throwable | ||
*/ | ||
public function migrateGroupUsers(string $gid): bool { | ||
$originalGroup = $this->groupManager->get($gid); | ||
$members = $originalGroup?->getUsers(); | ||
|
||
$areAllInserted = true; | ||
foreach (array_chunk($members ?? [], (int)floor(self::CHUNK_SIZE / 2)) as $userBatch) { | ||
$areAllInserted = ($this->atomic(function () use ($userBatch, $gid) { | ||
/** @var IUser $user */ | ||
foreach ($userBatch as $user) { | ||
$this->dbc->insertIgnoreConflict( | ||
GroupBackend::TABLE_MEMBERS, | ||
[ | ||
'gid' => $gid, | ||
'uid' => $user->getUID(), | ||
] | ||
); | ||
} | ||
return true; | ||
}, $this->dbc) === true) && $areAllInserted; | ||
} | ||
if (!$areAllInserted) { | ||
$this->logger->warning('Partial migration of users from local group {gid} to SAML.', [ | ||
'app' => 'user_saml', | ||
'gid' => $gid, | ||
]); | ||
} | ||
return $areAllInserted; | ||
} | ||
|
||
/** | ||
* @throws Exception | ||
*/ | ||
public function cleanUpOldGroupUsers(string $gid): void { | ||
$cleanup = $this->dbc->getQueryBuilder(); | ||
$cleanup->delete('group_user') | ||
->where($cleanup->expr()->eq('gid', $cleanup->createNamedParameter($gid))); | ||
$cleanup->executeStatement(); | ||
} | ||
|
||
} |
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