Skip to content

Commit

Permalink
Do not change the old behavior of user creation
Browse files Browse the repository at this point in the history
  • Loading branch information
karakayasemi committed Dec 6, 2019
1 parent cc59490 commit 90f8a45
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 11 deletions.
25 changes: 18 additions & 7 deletions core/Command/User/Add.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -91,15 +92,25 @@ 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');
$passwordFromEnv = $input->getOption('password-from-env');
$groupInput = $input->getOption('group');

if ($this->createUserService->userExists($uid)) {
$output->writeln('<error>The user "' . $uid . '" already exists.</error>');
$stdErr->writeln('<error>The user "' . $uid . '" already exists.</error>');
return 1;
}

Expand All @@ -111,7 +122,7 @@ protected function execute(InputInterface $input, OutputInterface $output) {
if ($passwordFromEnv) {
$password = \getenv('OC_PASS');
if (!$password) {
$output->writeln('<error>--password-from-env given, but OC_PASS is empty!</error>');
$stdErr->writeln('<error>--password-from-env given, but OC_PASS is empty!</error>');
return 1;
}
} elseif (($email === '') && $input->isInteractive()) {
Expand All @@ -126,28 +137,28 @@ protected function execute(InputInterface $input, OutputInterface $output) {
$confirm = $dialog->ask($input, $output, $q);

if ($password !== $confirm) {
$output->writeln("<error>Passwords did not match!</error>");
$stdErr->writeln("<error>Passwords did not match!</error>");
return 1;
}
}

try {
$user = $this->createUserService->createUser(['username' => $uid, 'password' => $password, 'email' => $email]);
} catch (InvalidEmailException $e) {
$output->writeln('<error>Invalid email address supplied</error>');
$stdErr->writeln('<error>Invalid email address supplied</error>');
return 1;
} catch (CannotCreateUserException $e) {
$output->writeln("<error>" . $e->getMessage() . "</error>");
$stdErr->writeln("<error>" . $e->getMessage() . "</error>");
return 1;
} catch (UserAlreadyExistsException $e) {
$output->writeln("<error>" . $e->getMessage() . "</error>");
$stdErr->writeln("<error>" . $e->getMessage() . "</error>");
return 1;
}

if ($user instanceof IUser) {
$output->writeln('<info>The user "' . $user->getUID() . '" was created successfully</info>');
} else {
$output->writeln('<error>An error occurred while creating the user</error>');
$stdErr->writeln('<error>An error occurred while creating the user</error>');
return 1;
}

Expand Down
4 changes: 2 additions & 2 deletions lib/private/User/Service/CreateUserService.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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) {
Expand Down
4 changes: 2 additions & 2 deletions tests/lib/User/Service/CreateUserServiceTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand All @@ -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' => '[email protected]']);
}

Expand Down

0 comments on commit 90f8a45

Please sign in to comment.