From 0d324f26eb8678e0b588cf23a5976ca9a264c2d1 Mon Sep 17 00:00:00 2001 From: stellarpower Date: Tue, 19 Mar 2024 16:34:37 +0000 Subject: [PATCH] Add occ command for getting a (the) User ID(s) for a given email address. --- core/Command/User/GetUserFromEmail.php | 89 +++++++++++++++++++++ core/register_command.php | 1 + lib/composer/composer/autoload_classmap.php | 1 + lib/composer/composer/autoload_static.php | 1 + 4 files changed, 92 insertions(+) create mode 100644 core/Command/User/GetUserFromEmail.php diff --git a/core/Command/User/GetUserFromEmail.php b/core/Command/User/GetUserFromEmail.php new file mode 100644 index 0000000000000..068ed0295d7ed --- /dev/null +++ b/core/Command/User/GetUserFromEmail.php @@ -0,0 +1,89 @@ + + * @author Christopher Schäpers + * @author Clark Tomlinson + * @author Joas Schilling + * @author Laurens Post + * @author Roeland Jago Douma + * @author Sujith H + * + * @license AGPL-3.0 + * + * This code is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License, version 3, + * as published by the Free Software Foundation. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License, version 3, + * along with this program. If not, see + * + */ +namespace OC\Core\Command\User; + +use OC\Core\Command\Base; +use OCP\App\IAppManager; +use OCP\IUser; +use OCP\IUserManager; +use Stecman\Component\Symfony\Console\BashCompletion\CompletionContext; +use Symfony\Component\Console\Helper\QuestionHelper; +use Symfony\Component\Console\Input\InputArgument; +use Symfony\Component\Console\Input\InputInterface; +use Symfony\Component\Console\Input\InputOption; +use Symfony\Component\Console\Output\OutputInterface; +use Symfony\Component\Console\Question\ConfirmationQuestion; +use Symfony\Component\Console\Question\Question; + +class GetUserFromEmail extends Base { + public function __construct( + protected IUserManager $userManager, + private IAppManager $appManager, + ) { + parent::__construct(); + } + + protected function configure() { + $this + ->setName('user:getfromemail') + ->setDescription('Find the user ID for the user with a given email') + ->addArgument( + 'email', + InputArgument::REQUIRED, + 'Email address' + ) + ; + } + + protected function execute(InputInterface $input, OutputInterface $output): int { + $email = $input->getArgument('email'); + + $users = $this->userManager->getByEmail($email); + if (count($users) == 0) { + $output->writeln('User with given email not found'); + return 1; + + } + foreach ($users as $user) { + $output->writeln($user->getUID()); + } + return 0; + } + + /** + * @param string $argumentName + * @param CompletionContext $context + * @return string[] + */ + public function completeArgumentValues($argumentName, CompletionContext $context) { + if ($argumentName === 'email') { + return array_map(static fn (IUser $user) => $user->getEmail(), $this->userManager->search($context->getCurrentWord())); + } + return []; + } +} diff --git a/core/register_command.php b/core/register_command.php index 4a84e551ce0ce..7e8a0dee5ed28 100644 --- a/core/register_command.php +++ b/core/register_command.php @@ -151,6 +151,7 @@ $application->add(Server::get(Command\User\Delete::class)); $application->add(Server::get(Command\User\Disable::class)); $application->add(Server::get(Command\User\Enable::class)); + $application->add(Server::get(Command\User\GetUserFromEmail::class)); // Tested with $application->add(new OC\Core\Command\User\GetUserFromEmail(\OC::$server->getUserManager())); $application->add(Server::get(Command\User\LastSeen::class)); $application->add(Server::get(Command\User\Report::class)); $application->add(Server::get(Command\User\ResetPassword::class)); diff --git a/lib/composer/composer/autoload_classmap.php b/lib/composer/composer/autoload_classmap.php index 3aa5c6a2b8cf9..fe7e2d9f82f84 100644 --- a/lib/composer/composer/autoload_classmap.php +++ b/lib/composer/composer/autoload_classmap.php @@ -1136,6 +1136,7 @@ 'OC\\Core\\Command\\User\\Delete' => $baseDir . '/core/Command/User/Delete.php', 'OC\\Core\\Command\\User\\Disable' => $baseDir . '/core/Command/User/Disable.php', 'OC\\Core\\Command\\User\\Enable' => $baseDir . '/core/Command/User/Enable.php', + 'OC\\Core\\Command\\User\\GetUserFromEmail' => $baseDir . '/core/Command/User/GetUserFromEmail.php', 'OC\\Core\\Command\\User\\Info' => $baseDir . '/core/Command/User/Info.php', 'OC\\Core\\Command\\User\\Keys\\Verify' => $baseDir . '/core/Command/User/Keys/Verify.php', 'OC\\Core\\Command\\User\\LastSeen' => $baseDir . '/core/Command/User/LastSeen.php', diff --git a/lib/composer/composer/autoload_static.php b/lib/composer/composer/autoload_static.php index 21d6778432210..a4da94a6c9774 100644 --- a/lib/composer/composer/autoload_static.php +++ b/lib/composer/composer/autoload_static.php @@ -1169,6 +1169,7 @@ class ComposerStaticInit749170dad3f5e7f9ca158f5a9f04f6a2 'OC\\Core\\Command\\User\\Delete' => __DIR__ . '/../../..' . '/core/Command/User/Delete.php', 'OC\\Core\\Command\\User\\Disable' => __DIR__ . '/../../..' . '/core/Command/User/Disable.php', 'OC\\Core\\Command\\User\\Enable' => __DIR__ . '/../../..' . '/core/Command/User/Enable.php', + 'OC\\Core\\Command\\User\\GetUserFromEmail' => __DIR__ . '/../../..' . '/core/Command/User/GetUserFromEmail.php', 'OC\\Core\\Command\\User\\Info' => __DIR__ . '/../../..' . '/core/Command/User/Info.php', 'OC\\Core\\Command\\User\\Keys\\Verify' => __DIR__ . '/../../..' . '/core/Command/User/Keys/Verify.php', 'OC\\Core\\Command\\User\\LastSeen' => __DIR__ . '/../../..' . '/core/Command/User/LastSeen.php',