-
Notifications
You must be signed in to change notification settings - Fork 2.1k
allow syncing single users, add verbose and very verbose output #28212
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
Closed
Closed
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
f331474
allow syncing single users, add verbose and very verbose output
butonic 0d6f54b
fix-tests
butonic 3641d5f
use callback class to handle output
butonic 3136151
fix typo
butonic 2364374
remove lib/private/Migration/OutputAdapter.php, was a bad idea anyway
butonic 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 hidden or 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 |
|---|---|---|
| @@ -1,5 +1,6 @@ | ||
| <?php | ||
| /** | ||
| * @author Jörn Friedrich Dreyer <[email protected]> | ||
| * @author Thomas Müller <[email protected]> | ||
| * | ||
| * @copyright Copyright (c) 2017, ownCloud GmbH | ||
|
|
@@ -22,15 +23,17 @@ | |
| namespace OC\Core\Command\User; | ||
|
|
||
|
|
||
| use OC\Migration\ConsoleOutput; | ||
| use OC\User\AccountMapper; | ||
| use OC\User\SyncService; | ||
| use OC\User\SyncServiceCallback; | ||
| use OCP\IConfig; | ||
| use OCP\ILogger; | ||
| use OCP\IUser; | ||
| use OCP\IUserManager; | ||
| use OCP\UserInterface; | ||
| use OCP\Util; | ||
| use Symfony\Component\Console\Command\Command; | ||
| use Symfony\Component\Console\Helper\ProgressBar; | ||
| use Symfony\Component\Console\Question\ChoiceQuestion; | ||
| use Symfony\Component\Console\Input\InputInterface; | ||
| use Symfony\Component\Console\Input\InputOption; | ||
|
|
@@ -48,6 +51,13 @@ class SyncBackend extends Command { | |
| /** @var ILogger */ | ||
| private $logger; | ||
|
|
||
| private $verbosityLevelMap = array( | ||
| OutputInterface::VERBOSITY_QUIET => Util::ERROR, | ||
| OutputInterface::VERBOSITY_NORMAL => Util::WARN, | ||
| OutputInterface::VERBOSITY_VERBOSE => Util::INFO, | ||
| OutputInterface::VERBOSITY_VERY_VERBOSE => Util::DEBUG, | ||
| OutputInterface::VERBOSITY_DEBUG => Util::DEBUG, | ||
| ); | ||
| /** | ||
| * @param AccountMapper $accountMapper | ||
| * @param IConfig $config | ||
|
|
@@ -75,6 +85,7 @@ protected function configure() { | |
| 'The php class name - e.g. "OCA\User_LDAP\User_Proxy". Please wrap the class name into double quotes. You can use the option --list to list all known backend classes' | ||
| ) | ||
| ->addOption('list', 'l', InputOption::VALUE_NONE, 'list all known backend classes') | ||
| ->addOption('userid', 'u', InputOption::VALUE_REQUIRED, 'sync only the user with the given id') | ||
| ->addOption('missing-account-action', 'm', InputOption::VALUE_REQUIRED, 'action to do if the account isn\'t connected to a backend any longer. Options are "disable" and "remove". Use quotes. Note that removing the account will also remove the stored data and files for that account'); | ||
| } | ||
|
|
||
|
|
@@ -103,8 +114,8 @@ protected function execute(InputInterface $input, OutputInterface $output) { | |
|
|
||
| $validActions = ['disable', 'remove']; | ||
|
|
||
| if ($input->getOption('missing-account-action') !== null) { | ||
| $missingAccountsAction = $input->getOption('missing-account-action'); | ||
| $missingAccountsAction = $input->getOption('missing-account-action'); | ||
| if ($missingAccountsAction !== null) { | ||
| if (!in_array($missingAccountsAction, $validActions, true)) { | ||
| $output->writeln("<error>Unknown action. Choose between \"disable\" or \"remove\"</error>"); | ||
| return 1; | ||
|
|
@@ -122,27 +133,108 @@ protected function execute(InputInterface $input, OutputInterface $output) { | |
|
|
||
| $syncService = new SyncService($this->accountMapper, $backend, $this->config, $this->logger); | ||
|
|
||
| // analyse unknown users | ||
| $this->handleUnknownUsers($input, $output, $syncService, $missingAccountsAction, $validActions); | ||
|
|
||
| // insert/update known users | ||
| $output->writeln("Insert new and update existing users ..."); | ||
| $p = new ProgressBar($output); | ||
| $max = null; | ||
| if ($backend->implementsActions(\OC_User_Backend::COUNT_USERS)) { | ||
| $max = $backend->countUsers(); | ||
| $userid = $input->getOption('userid'); | ||
| $consoleOutput = new ConsoleOutput($output); | ||
|
|
||
| if ($userid) { | ||
| $this->syncSingleUser($userid, $input, $output, $consoleOutput, $syncService, $backend, $missingAccountsAction, $validActions); | ||
| } else { | ||
| $this->syncMultipleUsers($input, $output, $consoleOutput, $syncService, $backend, $missingAccountsAction, $validActions); | ||
| } | ||
| $p->start($max); | ||
| $syncService->run(function () use ($p) { | ||
| $p->advance(); | ||
| }); | ||
| $p->finish(); | ||
|
|
||
| $output->writeln(''); | ||
| $output->writeln(''); | ||
|
|
||
| return 0; | ||
| } | ||
|
|
||
| /** | ||
| * @param InputInterface $input | ||
| * @param OutputInterface $output | ||
| * @param ConsoleOutput $consoleOutput | ||
| * @param SyncService $syncService | ||
| * @param UserInterface $backend | ||
| * @param string $missingAccountsAction | ||
| * @param array $validActions | ||
| */ | ||
| private function syncMultipleUsers ( | ||
| InputInterface $input, | ||
| OutputInterface $output, | ||
| ConsoleOutput $consoleOutput, | ||
| SyncService $syncService, | ||
| UserInterface $backend, | ||
| $missingAccountsAction, | ||
| array $validActions | ||
| ) { | ||
|
|
||
| $output->writeln("Analyzing synced users ..."); | ||
| $consoleOutput->startProgress($this->accountMapper->getUserCount(false)); | ||
| $unknownUsers = $syncService->getNoLongerExistingUsers(function () use ($consoleOutput) { | ||
| $consoleOutput->advance(); | ||
| }); | ||
| $consoleOutput->finishProgress(); | ||
| $output->writeln(''); | ||
| $output->writeln(''); | ||
|
|
||
| $this->handleUnknownUsers($unknownUsers, $input, $output, $missingAccountsAction, $validActions); | ||
|
|
||
| // insert/update known users | ||
| if ($output->getVerbosity() === OutputInterface::VERBOSITY_NORMAL) { | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. only if the verbosity is normal, or if it's normal or greater? |
||
| if ($backend->implementsActions(\OC_User_Backend::COUNT_USERS)) { | ||
| $consoleOutput->startProgress($backend->countUsers()); | ||
| } else { | ||
| $consoleOutput->startProgress(); | ||
| } | ||
| } | ||
|
|
||
| $syncService->run(new SyncServiceCallback( | ||
| $consoleOutput, | ||
| $this->verbosityLevelMap[$output->getVerbosity()] | ||
| )); | ||
|
|
||
| if ($output->getVerbosity() === OutputInterface::VERBOSITY_NORMAL) { | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. same as above |
||
| $consoleOutput->finishProgress(); | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * @param InputInterface $input | ||
| * @param OutputInterface $output | ||
| * @param ConsoleOutput $consoleOutput | ||
| * @param SyncService $syncService | ||
| * @param UserInterface $backend | ||
| * @param string $missingAccountsAction | ||
| * @param array $validActions | ||
| */ | ||
| private function syncSingleUser( | ||
| $uid, | ||
| InputInterface $input, | ||
| OutputInterface $output, | ||
| ConsoleOutput $consoleOutput, | ||
| SyncService $syncService, | ||
| UserInterface $backend, | ||
| $missingAccountsAction, | ||
| array $validActions | ||
| ) { | ||
|
|
||
| $output->writeln("Analyzing {$uid} ..."); | ||
| if (!$backend->userExists($uid)) { | ||
| $this->handleUnknownUsers([$uid], $input, $output, $missingAccountsAction, $validActions); | ||
| } else { | ||
| // sync | ||
| // use at least Verbose output | ||
| if ($output->getVerbosity() === OutputInterface::VERBOSITY_NORMAL) { | ||
| $logLevel = $this->verbosityLevelMap[OutputInterface::VERBOSITY_VERBOSE]; | ||
| } else { | ||
| $logLevel = $this->verbosityLevelMap[$output->getVerbosity()]; | ||
| } | ||
| $syncService->syncUser($uid, new SyncServiceCallback( | ||
| $consoleOutput, $logLevel | ||
| )); | ||
| } | ||
| } | ||
| /** | ||
| * @param $backend | ||
| * @return null|UserInterface | ||
|
|
@@ -177,30 +269,21 @@ private function doActionForAccountUids(array $uids, callable $callbackExists, c | |
| } | ||
|
|
||
| /** | ||
| * @param string[] $userIds | ||
| * @param InputInterface $input | ||
| * @param OutputInterface $output | ||
| * @param $syncService | ||
| * @param $missingAccountsAction | ||
| * @param $validActions | ||
| * @param string $missingAccountsAction | ||
| * @param array $validActions | ||
| */ | ||
| private function handleUnknownUsers(InputInterface $input, OutputInterface $output, $syncService, $missingAccountsAction, $validActions) { | ||
| $output->writeln("Analyse unknown users ..."); | ||
| $p = new ProgressBar($output); | ||
| $toBeDeleted = $syncService->getNoLongerExistingUsers(function () use ($p) { | ||
| $p->advance(); | ||
| }); | ||
| $p->finish(); | ||
| $output->writeln(''); | ||
| $output->writeln(''); | ||
|
|
||
| if (empty($toBeDeleted)) { | ||
| private function handleUnknownUsers(array $userIds, InputInterface $input, OutputInterface $output, $missingAccountsAction, $validActions) { | ||
| if (empty($userIds)) { | ||
| $output->writeln("No unknown users have been detected."); | ||
| } else { | ||
| $output->writeln("Following users are no longer known with the connected backend."); | ||
| switch ($missingAccountsAction) { | ||
| case 'disable': | ||
| $output->writeln("Proceeding to disable the accounts"); | ||
| $this->doActionForAccountUids($toBeDeleted, | ||
| $this->doActionForAccountUids($userIds, | ||
| function ($uid, IUser $ac) use ($output) { | ||
| $ac->setEnabled(false); | ||
| $output->writeln($uid); | ||
|
|
@@ -211,7 +294,7 @@ function ($uid) use ($output) { | |
| break; | ||
| case 'remove': | ||
| $output->writeln("Proceeding to remove the accounts"); | ||
| $this->doActionForAccountUids($toBeDeleted, | ||
| $this->doActionForAccountUids($userIds, | ||
| function ($uid, IUser $ac) use ($output) { | ||
| $ac->delete(); | ||
| $output->writeln($uid); | ||
|
|
@@ -222,7 +305,7 @@ function ($uid) use ($output) { | |
| break; | ||
| case 'ask later': | ||
| $output->writeln("listing the unknown accounts"); | ||
| $this->doActionForAccountUids($toBeDeleted, | ||
| $this->doActionForAccountUids($userIds, | ||
| function ($uid) use ($output) { | ||
| $output->writeln($uid); | ||
| }, | ||
|
|
@@ -241,7 +324,7 @@ function ($uid) use ($output) { | |
| // if "nothing" is selected, just ignore and finish | ||
| case 'disable': | ||
| $output->writeln("Proceeding to disable the accounts"); | ||
| $this->doActionForAccountUids($toBeDeleted, | ||
| $this->doActionForAccountUids($userIds, | ||
| function ($uid, IUser $ac) { | ||
| $ac->setEnabled(false); | ||
| }, | ||
|
|
@@ -251,7 +334,7 @@ function ($uid) use ($output) { | |
| break; | ||
| case 'remove': | ||
| $output->writeln("Proceeding to remove the accounts"); | ||
| $this->doActionForAccountUids($toBeDeleted, | ||
| $this->doActionForAccountUids($userIds, | ||
| function ($uid, IUser $ac) { | ||
| $ac->delete(); | ||
| }, | ||
|
|
||
This file contains hidden or 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 hidden or 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 |
|---|---|---|
| @@ -1,5 +1,6 @@ | ||
| <?php | ||
| /** | ||
| * @author Jörn Friedrich Dreyer <[email protected]> | ||
| * @author Thomas Müller <[email protected]> | ||
| * | ||
| * @copyright Copyright (c) 2017, ownCloud GmbH | ||
|
|
@@ -49,16 +50,18 @@ public function __construct(OutputInterface $output) { | |
|
|
||
| /** | ||
| * @param string $message | ||
| * @param bool $newline | ||
| */ | ||
| public function info($message) { | ||
| $this->output->writeln("<info>$message</info>"); | ||
| public function info($message, $newline = true) { | ||
| $this->output->write("<info>$message</info>", $newline); | ||
| } | ||
|
|
||
| /** | ||
| * @param string $message | ||
| * @param bool $newline | ||
| */ | ||
| public function warning($message) { | ||
| $this->output->writeln("<comment>$message</comment>"); | ||
| public function warning($message, $newline = true) { | ||
| $this->output->write("<comment>$message</comment>", $newline); | ||
| } | ||
|
|
||
| /** | ||
|
|
@@ -77,13 +80,16 @@ public function startProgress($max = 0) { | |
| * @param string $description | ||
| */ | ||
| public function advance($step = 1, $description = '') { | ||
| if (!is_null($this->progressBar)) { | ||
| if (is_null($this->progressBar)) { | ||
| $this->progressBar = new ProgressBar($this->output); | ||
| $this->progressBar->start(); | ||
| } | ||
| $this->progressBar->advance($step); | ||
| } | ||
|
|
||
| /** | ||
| * @since 9.1.0 | ||
| */ | ||
| public function finishProgress() { | ||
| if (is_null($this->progressBar)) { | ||
| return; | ||
|
|
||
This file contains hidden or 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 |
|---|---|---|
| @@ -1,5 +1,6 @@ | ||
| <?php | ||
| /** | ||
| * @author Jörn Friedrich Dreyer <[email protected]> | ||
| * @author Thomas Müller <[email protected]> | ||
| * | ||
| * @copyright Copyright (c) 2017, ownCloud GmbH | ||
|
|
@@ -47,17 +48,19 @@ public function __construct(ILogger $logger, $appName) { | |
|
|
||
| /** | ||
| * @param string $message | ||
| * @param bool $newline always true because ILogger always uses newlines | ||
| * @since 9.1.0 | ||
| */ | ||
| public function info($message) { | ||
| public function info($message, $newline = true) { | ||
| $this->logger->info($message, ['app' => $this->appName]); | ||
| } | ||
|
|
||
| /** | ||
| * @param string $message | ||
| * @param bool $newline always true because ILogger always uses newlines | ||
| * @since 9.1.0 | ||
| */ | ||
| public function warning($message) { | ||
| public function warning($message, $newline = true) { | ||
| $this->logger->warning($message, ['app' => $this->appName]); | ||
| } | ||
|
|
||
|
|
||
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.
I'd rather get the OutputInterface from the ConsoleOutput instead of sending it raw here.
Ideally, we shouldn't need to get the OutputInterface, so we should use one or another, but if we need the ConsoleOutput, it should be possible to get the OutputInterface from it.