|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +/** |
| 6 | + * SPDX-FileCopyrightText: 2026 Nextcloud GmbH and Nextcloud contributors |
| 7 | + * SPDX-License-Identifier: AGPL-3.0-or-later |
| 8 | + */ |
| 9 | + |
| 10 | +namespace OCA\User_SAML\Command; |
| 11 | + |
| 12 | +use OC\Core\Command\Base; |
| 13 | +use OC\Group\Database; |
| 14 | +use OCA\User_SAML\Service\GroupMigration; |
| 15 | +use OCP\IGroupManager; |
| 16 | +use Psr\Log\LoggerInterface; |
| 17 | +use Psr\Log\LogLevel; |
| 18 | +use Symfony\Component\Console\Input\InputInterface; |
| 19 | +use Symfony\Component\Console\Output\OutputInterface; |
| 20 | + |
| 21 | +class GroupMigrationManual extends Base implements LoggerInterface { |
| 22 | + private ?OutputInterface $output = null; |
| 23 | + |
| 24 | + public function __construct( |
| 25 | + private readonly GroupMigration $groupMigration, |
| 26 | + private readonly IGroupManager $groupManager, |
| 27 | + ) { |
| 28 | + parent::__construct(); |
| 29 | + $this->groupMigration->setLogger($this); |
| 30 | + } |
| 31 | + |
| 32 | + #[\Override] |
| 33 | + protected function configure(): void { |
| 34 | + $this->setName('saml:group-migration:force'); |
| 35 | + $this->setDescription('Force migration of all groups from Database to SAML backend. Groups with non-saml members are skipped.'); |
| 36 | + } |
| 37 | + |
| 38 | + #[\Override] |
| 39 | + protected function execute(InputInterface $input, OutputInterface $output): int { |
| 40 | + $this->output = $output; |
| 41 | + try { |
| 42 | + $backend = $this->findBackend(); |
| 43 | + $groupsToTreat = $this->findGroupIds($backend); |
| 44 | + $groupsToTreat = $this->groupMigration->getGroupsToMigrate($groupsToTreat, $groupsToTreat); |
| 45 | + } catch (\UnexpectedValueException) { |
| 46 | + $output->writeln('<error>Failed to find database group backend</error>'); |
| 47 | + return self::FAILURE; |
| 48 | + } |
| 49 | + |
| 50 | + $failures = 0; |
| 51 | + foreach ($groupsToTreat as $group) { |
| 52 | + if (!$this->groupMigration->migrateGroup($group)) { |
| 53 | + $output->writeln('<error>Failed to migrate ' . $group . '</error>'); |
| 54 | + $failures++; |
| 55 | + } |
| 56 | + } |
| 57 | + if ($failures === 0) { |
| 58 | + $output->writeln('<info>All groups were successfully migrated</info>'); |
| 59 | + return self::SUCCESS; |
| 60 | + } |
| 61 | + |
| 62 | + return self::FAILURE; |
| 63 | + } |
| 64 | + |
| 65 | + private function findBackend(): Database { |
| 66 | + $groupBackends = $this->groupManager->getBackends(); |
| 67 | + foreach ($groupBackends as $backend) { |
| 68 | + if ($backend instanceof Database) { |
| 69 | + return $backend; |
| 70 | + } |
| 71 | + } |
| 72 | + throw new \UnexpectedValueException(); |
| 73 | + } |
| 74 | + |
| 75 | + private function findGroupIds(Database $backend): array { |
| 76 | + $groupIds = $backend->getGroups(); |
| 77 | + $adminGroupIndex = array_search('admin', $groupIds, true); |
| 78 | + if ($adminGroupIndex !== false) { |
| 79 | + unset($groupIds[$adminGroupIndex]); |
| 80 | + } |
| 81 | + return array_values($groupIds); |
| 82 | + } |
| 83 | + |
| 84 | + /* |
| 85 | + * Log functions to implement LoggerInterface so that information makes it to the cli output |
| 86 | + */ |
| 87 | + |
| 88 | + /** |
| 89 | + * @param string $message |
| 90 | + */ |
| 91 | + #[\Override] |
| 92 | + public function emergency($message, array $context = []): void { |
| 93 | + $this->log(LogLevel::EMERGENCY, $message, $context); |
| 94 | + } |
| 95 | + |
| 96 | + /** |
| 97 | + * @param string $message |
| 98 | + */ |
| 99 | + #[\Override] |
| 100 | + public function alert($message, array $context = []): void { |
| 101 | + $this->log(LogLevel::ALERT, $message, $context); |
| 102 | + } |
| 103 | + |
| 104 | + /** |
| 105 | + * @param string $message |
| 106 | + */ |
| 107 | + #[\Override] |
| 108 | + public function critical($message, array $context = []): void { |
| 109 | + $this->log(LogLevel::CRITICAL, $message, $context); |
| 110 | + } |
| 111 | + |
| 112 | + /** |
| 113 | + * @param string $message |
| 114 | + */ |
| 115 | + #[\Override] |
| 116 | + public function error($message, array $context = []): void { |
| 117 | + $this->log(LogLevel::ERROR, $message, $context); |
| 118 | + } |
| 119 | + |
| 120 | + /** |
| 121 | + * @param string $message |
| 122 | + */ |
| 123 | + #[\Override] |
| 124 | + public function warning($message, array $context = []): void { |
| 125 | + $this->log(LogLevel::WARNING, $message, $context); |
| 126 | + } |
| 127 | + |
| 128 | + /** |
| 129 | + * @param string $message |
| 130 | + */ |
| 131 | + #[\Override] |
| 132 | + public function notice($message, array $context = []): void { |
| 133 | + $this->log(LogLevel::NOTICE, $message, $context); |
| 134 | + } |
| 135 | + |
| 136 | + /** |
| 137 | + * @param string $message |
| 138 | + */ |
| 139 | + #[\Override] |
| 140 | + public function info($message, array $context = []): void { |
| 141 | + $this->log(LogLevel::INFO, $message, $context); |
| 142 | + } |
| 143 | + |
| 144 | + /** |
| 145 | + * @param string $message |
| 146 | + */ |
| 147 | + #[\Override] |
| 148 | + public function debug($message, array $context = []): void { |
| 149 | + $this->log(LogLevel::DEBUG, $message, $context); |
| 150 | + } |
| 151 | + |
| 152 | + /** |
| 153 | + * Logs with an arbitrary level. |
| 154 | + * |
| 155 | + * @param mixed $level |
| 156 | + * @param string $message |
| 157 | + */ |
| 158 | + #[\Override] |
| 159 | + public function log($level, $message, array $context = []): void { |
| 160 | + $tag = match($level) { |
| 161 | + LogLevel::EMERGENCY, |
| 162 | + LogLevel::ALERT, |
| 163 | + LogLevel::CRITICAL, |
| 164 | + LogLevel::ERROR => 'error', |
| 165 | + LogLevel::WARNING, |
| 166 | + LogLevel::NOTICE => 'warning', |
| 167 | + LogLevel::INFO => 'info', |
| 168 | + default => '', |
| 169 | + }; |
| 170 | + $flags = match($level) { |
| 171 | + LogLevel::DEBUG => OutputInterface::VERBOSITY_VERBOSE, |
| 172 | + default => 0, |
| 173 | + }; |
| 174 | + $message = $this->interpolateMessage($message, $context); |
| 175 | + if ($tag !== '') { |
| 176 | + $message = '<' . $tag . '>' . $message . '<' . $tag . '/>'; |
| 177 | + } |
| 178 | + $this->output?->writeln($message, $flags); |
| 179 | + } |
| 180 | + |
| 181 | + private function interpolateMessage(string $message, array $context): string { |
| 182 | + $replace = []; |
| 183 | + foreach ($context as $key => $val) { |
| 184 | + $replace['{' . $key . '}'] = $val; |
| 185 | + } |
| 186 | + return strtr($message, $replace); |
| 187 | + } |
| 188 | +} |
0 commit comments