From 90f8a459bbd37a66d8934ef191bb4c7107e425f4 Mon Sep 17 00:00:00 2001 From: Semih Serhat Karakaya Date: Fri, 6 Dec 2019 11:39:43 +0300 Subject: [PATCH] Do not change the old behavior of user creation --- core/Command/User/Add.php | 25 +++++++++++++------ .../User/Service/CreateUserService.php | 4 +-- .../User/Service/CreateUserServiceTest.php | 4 +-- 3 files changed, 22 insertions(+), 11 deletions(-) diff --git a/core/Command/User/Add.php b/core/Command/User/Add.php index 4a3d9665fc1f..34b07b0627c4 100644 --- a/core/Command/User/Add.php +++ b/core/Command/User/Add.php @@ -32,6 +32,7 @@ use Symfony\Component\Console\Command\Command; use Symfony\Component\Console\Input\InputInterface; use Symfony\Component\Console\Input\InputOption; +use Symfony\Component\Console\Output\ConsoleOutputInterface; use Symfony\Component\Console\Output\OutputInterface; use Symfony\Component\Console\Input\InputArgument; use Symfony\Component\Console\Question\Question; @@ -91,7 +92,17 @@ protected function configure() { ); } + /** + * @param InputInterface $input + * @param OutputInterface $output + * @return int + */ protected function execute(InputInterface $input, OutputInterface $output) { + $stdErr = $output; + if ($output instanceof ConsoleOutputInterface) { + // If it's available, get stdErr output + $stdErr = $output->getErrorOutput(); + } $uid = $input->getArgument('uid'); $email = $input->getOption('email'); $displayName = $input->getOption('display-name'); @@ -99,7 +110,7 @@ protected function execute(InputInterface $input, OutputInterface $output) { $groupInput = $input->getOption('group'); if ($this->createUserService->userExists($uid)) { - $output->writeln('The user "' . $uid . '" already exists.'); + $stdErr->writeln('The user "' . $uid . '" already exists.'); return 1; } @@ -111,7 +122,7 @@ protected function execute(InputInterface $input, OutputInterface $output) { if ($passwordFromEnv) { $password = \getenv('OC_PASS'); if (!$password) { - $output->writeln('--password-from-env given, but OC_PASS is empty!'); + $stdErr->writeln('--password-from-env given, but OC_PASS is empty!'); return 1; } } elseif (($email === '') && $input->isInteractive()) { @@ -126,7 +137,7 @@ protected function execute(InputInterface $input, OutputInterface $output) { $confirm = $dialog->ask($input, $output, $q); if ($password !== $confirm) { - $output->writeln("Passwords did not match!"); + $stdErr->writeln("Passwords did not match!"); return 1; } } @@ -134,20 +145,20 @@ protected function execute(InputInterface $input, OutputInterface $output) { try { $user = $this->createUserService->createUser(['username' => $uid, 'password' => $password, 'email' => $email]); } catch (InvalidEmailException $e) { - $output->writeln('Invalid email address supplied'); + $stdErr->writeln('Invalid email address supplied'); return 1; } catch (CannotCreateUserException $e) { - $output->writeln("" . $e->getMessage() . ""); + $stdErr->writeln("" . $e->getMessage() . ""); return 1; } catch (UserAlreadyExistsException $e) { - $output->writeln("" . $e->getMessage() . ""); + $stdErr->writeln("" . $e->getMessage() . ""); return 1; } if ($user instanceof IUser) { $output->writeln('The user "' . $user->getUID() . '" was created successfully'); } else { - $output->writeln('An error occurred while creating the user'); + $stdErr->writeln('An error occurred while creating the user'); return 1; } diff --git a/lib/private/User/Service/CreateUserService.php b/lib/private/User/Service/CreateUserService.php index 918edbe084bb..5f98b7206079 100644 --- a/lib/private/User/Service/CreateUserService.php +++ b/lib/private/User/Service/CreateUserService.php @@ -92,7 +92,7 @@ public function __construct(IUserSession $userSession, IGroupManager $groupManag * @throws UserAlreadyExistsException */ public function createUser($arguments) { - $username = $password = $email = ''; + $password = $email = ''; if (\array_key_exists('username', $arguments)) { $username = $arguments['username']; } else { @@ -125,7 +125,7 @@ public function createUser($arguments) { } $user = $this->userManager->createUser($username, $password); } catch (\Exception $exception) { - throw new CannotCreateUserException("Unable to create user due to exception: {$exception->getMessage()}"); + throw new CannotCreateUserException($exception->getMessage()); } if ($user === false) { diff --git a/tests/lib/User/Service/CreateUserServiceTest.php b/tests/lib/User/Service/CreateUserServiceTest.php index 17998f96c8f4..4a6cfb481f70 100644 --- a/tests/lib/User/Service/CreateUserServiceTest.php +++ b/tests/lib/User/Service/CreateUserServiceTest.php @@ -140,7 +140,7 @@ public function testAlreadyExistingUser() { } /** - * @expectedExceptionMessage Unable to create user due to exception: + * @expectedExceptionMessage Exception Message * @expectedException \OCP\User\Exceptions\CannotCreateUserException */ public function testUserCreateException() { @@ -151,7 +151,7 @@ public function testUserCreateException() { ->willReturn(false); $this->userManager->method('createUser') - ->willThrowException(new \Exception()); + ->willThrowException(new \Exception("Exception Message")); $this->createUserService->createUser(['username' => 'foo', 'password' => '', 'email' => 'foo@bar.com']); }