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 authored and sharidas committed Dec 10, 2019
1 parent 46dcb47 commit 0441002
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 8 deletions.
19 changes: 15 additions & 4 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,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');
Expand Down Expand Up @@ -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('<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 0441002

Please sign in to comment.