-
Notifications
You must be signed in to change notification settings - Fork 1
IBX-8823: Added CLI command to update user #86
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
Merged
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
ba23c09
IBX-8823: Added CLI command to update user
konradoboza fc94767
cr remarks
konradoboza e03e29d
cr remarks vol1
konradoboza 59503aa
cr remarks vol2
konradoboza 6932bf9
added test coverage
konradoboza 869ecca
cr remark vol3
konradoboza 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
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 |
---|---|---|
@@ -0,0 +1,135 @@ | ||
<?php | ||
|
||
/** | ||
* @copyright Copyright (C) Ibexa AS. All rights reserved. | ||
* @license For full copyright and license information view LICENSE file distributed with this source code. | ||
*/ | ||
declare(strict_types=1); | ||
|
||
namespace Ibexa\Bundle\User\Command; | ||
|
||
use Ibexa\Contracts\Core\Repository\Repository; | ||
use Ibexa\Contracts\Core\Repository\UserService; | ||
use Ibexa\Contracts\Core\Repository\Values\User\User; | ||
use Ibexa\Contracts\Core\Repository\Values\User\UserUpdateStruct; | ||
use Symfony\Component\Console\Attribute\AsCommand; | ||
use Symfony\Component\Console\Command\Command; | ||
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\Style\SymfonyStyle; | ||
|
||
#[AsCommand(name: 'ibexa:user:update-user', description: 'Updates basic user data.')] | ||
final class UpdateUserCommand extends Command | ||
{ | ||
public function __construct( | ||
private readonly UserService $userService, | ||
private readonly Repository $repository, | ||
) { | ||
parent::__construct(); | ||
} | ||
|
||
protected function configure(): void | ||
{ | ||
$this->addArgument( | ||
'user', | ||
InputArgument::REQUIRED, | ||
'User login', | ||
); | ||
$this->addOption( | ||
'password', | ||
null, | ||
InputOption::VALUE_OPTIONAL, | ||
'New plaintext password (input will be in a "hidden" mode)', | ||
false | ||
); | ||
$this->addOption( | ||
'email', | ||
null, | ||
InputOption::VALUE_REQUIRED, | ||
'New e-mail address', | ||
); | ||
$this->addOption( | ||
'enable', | ||
null, | ||
InputOption::VALUE_NONE, | ||
'Flag enabling the user being updated', | ||
); | ||
$this->addOption( | ||
'disable', | ||
null, | ||
InputOption::VALUE_NONE, | ||
'Flag disabling the user being updated', | ||
); | ||
} | ||
|
||
protected function interact(InputInterface $input, OutputInterface $output): void | ||
{ | ||
$io = new SymfonyStyle($input, $output); | ||
$password = $input->getOption('password'); | ||
|
||
if ($password !== null) { | ||
return; | ||
} | ||
|
||
$password = $io->askHidden('Password (your input will be hidden)'); | ||
$input->setOption('password', $password); | ||
} | ||
|
||
/** | ||
* @throws \Ibexa\Contracts\Core\Repository\Exceptions\InvalidArgumentException | ||
* @throws \Ibexa\Contracts\Core\Repository\Exceptions\NotFoundException | ||
*/ | ||
protected function execute(InputInterface $input, OutputInterface $output): int | ||
{ | ||
$io = new SymfonyStyle($input, $output); | ||
|
||
$userReference = $input->getArgument('user'); | ||
$password = $input->getOption('password'); | ||
$enable = $input->getOption('enable'); | ||
$disable = $input->getOption('disable'); | ||
$email = $input->getOption('email'); | ||
|
||
if (!$password && !$enable && !$disable && $email === null) { | ||
$io->error('No new user data specified, exiting.'); | ||
|
||
return Command::FAILURE; | ||
} | ||
|
||
$user = $this->userService->loadUserByLogin($userReference); | ||
|
||
if ($enable && $disable) { | ||
$io->error('--enable and --disable options cannot be used simultaneously.'); | ||
|
||
return Command::FAILURE; | ||
} | ||
|
||
$userUpdateStruct = new UserUpdateStruct(); | ||
$userUpdateStruct->password = $password; | ||
$userUpdateStruct->email = $email; | ||
$userUpdateStruct->enabled = $this->resolveEnabledFlag($enable, $disable); | ||
|
||
$this->repository->sudo( | ||
function () use ($user, $userUpdateStruct): User { | ||
return $this->userService->updateUser($user, $userUpdateStruct); | ||
} | ||
); | ||
|
||
$io->success(sprintf( | ||
'User "%s" was successfully updated.', | ||
$user->getLogin(), | ||
)); | ||
|
||
return Command::SUCCESS; | ||
} | ||
|
||
private function resolveEnabledFlag(bool $enable, bool $disable): ?bool | ||
{ | ||
if (!$enable && !$disable) { | ||
return null; | ||
} | ||
|
||
return $enable === true; | ||
} | ||
} |
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 |
---|---|---|
@@ -0,0 +1,87 @@ | ||
<?php | ||
|
||
/** | ||
* @copyright Copyright (C) Ibexa AS. All rights reserved. | ||
* @license For full copyright and license information view LICENSE file distributed with this source code. | ||
*/ | ||
declare(strict_types=1); | ||
|
||
namespace Ibexa\Tests\Bundle\User\Command; | ||
|
||
use Symfony\Bundle\FrameworkBundle\Console\Application; | ||
use Symfony\Bundle\FrameworkBundle\Test\KernelTestCase; | ||
use Symfony\Component\Console\Command\Command; | ||
use Symfony\Component\Console\Tester\CommandTester; | ||
|
||
final class UpdateUserCommandTest extends KernelTestCase | ||
{ | ||
private readonly CommandTester $commandTester; | ||
|
||
protected function setUp(): void | ||
{ | ||
self::bootKernel(); | ||
|
||
$application = new Application(self::$kernel); | ||
$application->setAutoExit(false); | ||
|
||
$command = $application->find('ibexa:user:update-user'); | ||
$this->commandTester = new CommandTester($command); | ||
} | ||
|
||
public function testExecuteWithoutOptionsReturnsFailure(): void | ||
{ | ||
$this->commandTester->execute([ | ||
'user' => 'anonymous', | ||
]); | ||
|
||
self::assertStringContainsString( | ||
'No new user data specified, exiting.', | ||
$this->commandTester->getDisplay() | ||
); | ||
|
||
self::assertSame( | ||
Command::FAILURE, | ||
$this->commandTester->getStatusCode() | ||
); | ||
} | ||
|
||
public function testExecuteWithEnableAndDisableOptionsReturnsFailure(): void | ||
{ | ||
$this->commandTester->execute( | ||
[ | ||
'user' => 'anonymous', | ||
'--enable' => true, | ||
'--disable' => true, | ||
], | ||
); | ||
|
||
self::assertStringContainsString( | ||
'--enable and --disable options cannot be used simultaneously.', | ||
$this->commandTester->getDisplay() | ||
); | ||
|
||
self::assertSame( | ||
Command::FAILURE, | ||
$this->commandTester->getStatusCode() | ||
); | ||
} | ||
|
||
public function testExecuteReturnsSuccess(): void | ||
{ | ||
$this->commandTester->execute( | ||
[ | ||
'user' => 'anonymous', | ||
'--password' => true, | ||
'--email' => '[email protected]', | ||
'--enable' => true, | ||
], | ||
); | ||
|
||
self::assertStringContainsString( | ||
'User "anonymous" was successfully updated.', | ||
$this->commandTester->getDisplay() | ||
); | ||
|
||
$this->commandTester->assertCommandIsSuccessful(); | ||
} | ||
} |
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.
Uh oh!
There was an error while loading. Please reload this page.