-
-
Notifications
You must be signed in to change notification settings - Fork 484
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
Add commands from FOSUserBundle #1484
Merged
jordisala1991
merged 14 commits into
sonata-project:5.x
from
jordisala1991:feature/add-commands
Jan 30, 2022
Merged
Changes from all commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
ec3444c
Add commands from FOSUserBundle
jordisala1991 7c377b9
Refactor
jordisala1991 5199af4
Fix phpstan
jordisala1991 fe296be
Make argument optional to have interact working
jordisala1991 12c182c
Fix psalm
jordisala1991 1227ed2
Add more commands
jordisala1991 082523d
Add more tests
jordisala1991 cffb456
Remove interactive
jordisala1991 ef89715
Fix cs-fixer
jordisala1991 d057781
Fix phpstan
jordisala1991 a048a91
Add missing test
jordisala1991 dee6cac
Add missing commands
jordisala1991 989ead8
Fix change password
jordisala1991 fe726dd
Fix command name
jordisala1991 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 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 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,13 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<files psalm-version="4.19.0@a2ad69ae4f5ab1f7d225a8dc4e2ec2d9415ed599"> | ||
<file src="src/Action/LoginAction.php"> | ||
<UndefinedInterfaceMethod occurrences="1"> | ||
<code>getFlashBag</code> | ||
</UndefinedInterfaceMethod> | ||
</file> | ||
<file src="src/Action/ResetAction.php"> | ||
<UndefinedInterfaceMethod occurrences="1"> | ||
<code>getFlashBag</code> | ||
</UndefinedInterfaceMethod> | ||
</file> | ||
</files> |
This file contains 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 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 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 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,75 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
/* | ||
* This file is part of the Sonata Project package. | ||
* | ||
* (c) Thomas Rabaix <thomas.rabaix@sonata-project.org> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Sonata\UserBundle\Command; | ||
|
||
use Sonata\UserBundle\Model\UserManagerInterface; | ||
use Symfony\Component\Console\Command\Command; | ||
use Symfony\Component\Console\Input\InputArgument; | ||
use Symfony\Component\Console\Input\InputInterface; | ||
use Symfony\Component\Console\Output\OutputInterface; | ||
|
||
/** | ||
* @internal | ||
*/ | ||
final class ActivateUserCommand extends Command | ||
{ | ||
protected static $defaultName = 'sonata:user:activate'; | ||
protected static $defaultDescription = 'Activate a user'; | ||
|
||
private UserManagerInterface $userManager; | ||
|
||
public function __construct(UserManagerInterface $userManager) | ||
{ | ||
parent::__construct(); | ||
|
||
$this->userManager = $userManager; | ||
} | ||
|
||
protected function configure(): void | ||
{ | ||
\assert(null !== static::$defaultDescription); | ||
|
||
$this | ||
->setDescription(static::$defaultDescription) | ||
->setDefinition([ | ||
new InputArgument('username', InputArgument::REQUIRED, 'The username'), | ||
]) | ||
->setHelp( | ||
<<<'EOT' | ||
The <info>%command.full_name%</info> command activates a user (so they will be able to log in): | ||
|
||
<info>php %command.full_name% matthieu</info> | ||
EOT | ||
); | ||
} | ||
|
||
protected function execute(InputInterface $input, OutputInterface $output): int | ||
{ | ||
$username = $input->getArgument('username'); | ||
|
||
$user = $this->userManager->findUserByUsername($username); | ||
|
||
if (null === $user) { | ||
throw new \InvalidArgumentException(sprintf('User identified by "%s" username does not exist.', $username)); | ||
} | ||
|
||
$user->setEnabled(true); | ||
|
||
$this->userManager->save($user); | ||
|
||
$output->writeln(sprintf('User "%s" has been activated.', $username)); | ||
|
||
return 0; | ||
} | ||
} |
This file contains 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,79 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
/* | ||
* This file is part of the Sonata Project package. | ||
* | ||
* (c) Thomas Rabaix <thomas.rabaix@sonata-project.org> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Sonata\UserBundle\Command; | ||
|
||
use Sonata\UserBundle\Model\UserManagerInterface; | ||
use Symfony\Component\Console\Command\Command; | ||
use Symfony\Component\Console\Input\InputArgument; | ||
use Symfony\Component\Console\Input\InputInterface; | ||
use Symfony\Component\Console\Output\OutputInterface; | ||
|
||
/** | ||
* @internal | ||
*/ | ||
final class ChangePasswordCommand extends Command | ||
{ | ||
protected static $defaultName = 'sonata:user:change-password'; | ||
protected static $defaultDescription = 'Change the password of a user'; | ||
|
||
private UserManagerInterface $userManager; | ||
|
||
public function __construct(UserManagerInterface $userManager) | ||
{ | ||
parent::__construct(); | ||
|
||
$this->userManager = $userManager; | ||
} | ||
|
||
protected function configure(): void | ||
{ | ||
\assert(null !== static::$defaultDescription); | ||
|
||
$this | ||
->setDescription(static::$defaultDescription) | ||
->setDefinition([ | ||
new InputArgument('username', InputArgument::REQUIRED, 'The username'), | ||
new InputArgument('password', InputArgument::REQUIRED, 'The password'), | ||
]) | ||
->setHelp( | ||
<<<'EOT' | ||
The <info>%command.full_name%</info> command changes the password of a user: | ||
|
||
<info>php %command.full_name% matthieu mypassword</info> | ||
|
||
EOT | ||
); | ||
} | ||
|
||
protected function execute(InputInterface $input, OutputInterface $output): int | ||
{ | ||
$username = $input->getArgument('username'); | ||
$password = $input->getArgument('password'); | ||
|
||
$user = $this->userManager->findUserByUsername($username); | ||
|
||
if (null === $user) { | ||
throw new \InvalidArgumentException(sprintf('User identified by "%s" username does not exist.', $username)); | ||
} | ||
|
||
$user->setPlainPassword($password); | ||
|
||
$this->userManager->updatePassword($user); | ||
$this->userManager->save($user); | ||
|
||
$output->writeln(sprintf('Changed password for user "%s".', $username)); | ||
|
||
return 0; | ||
} | ||
} |
This file contains 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,92 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
/* | ||
* This file is part of the Sonata Project package. | ||
* | ||
* (c) Thomas Rabaix <thomas.rabaix@sonata-project.org> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Sonata\UserBundle\Command; | ||
|
||
use Sonata\UserBundle\Model\UserManagerInterface; | ||
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; | ||
|
||
/** | ||
* @internal | ||
*/ | ||
final class CreateUserCommand extends Command | ||
{ | ||
protected static $defaultName = 'sonata:user:create'; | ||
protected static $defaultDescription = 'Create a user'; | ||
|
||
private UserManagerInterface $userManager; | ||
|
||
public function __construct(UserManagerInterface $userManager) | ||
{ | ||
parent::__construct(); | ||
|
||
$this->userManager = $userManager; | ||
} | ||
|
||
protected function configure(): void | ||
{ | ||
\assert(null !== static::$defaultDescription); | ||
|
||
$this | ||
->setDescription(static::$defaultDescription) | ||
->setDefinition([ | ||
new InputArgument('username', InputArgument::REQUIRED, 'The username'), | ||
new InputArgument('email', InputArgument::REQUIRED, 'The email'), | ||
new InputArgument('password', InputArgument::REQUIRED, 'The password'), | ||
new InputOption('super-admin', null, InputOption::VALUE_NONE, 'Set the user as super admin'), | ||
new InputOption('inactive', null, InputOption::VALUE_NONE, 'Set the user as inactive'), | ||
]) | ||
->setHelp( | ||
<<<'EOT' | ||
The <info>%command.full_name%</info> command creates a user: | ||
|
||
<info>php %command.full_name% matthieu matthieu@example.com mypassword</info> | ||
|
||
You can create a super admin via the super-admin flag: | ||
|
||
<info>php %command.full_name% admin admi@example.com mypassword --super-admin</info> | ||
|
||
You can create an inactive user (will not be able to log in): | ||
|
||
<info>php %command.full_name% user user@example.com mypassword --inactive</info> | ||
|
||
EOT | ||
); | ||
} | ||
|
||
protected function execute(InputInterface $input, OutputInterface $output): int | ||
{ | ||
$username = $input->getArgument('username'); | ||
$email = $input->getArgument('email'); | ||
$password = $input->getArgument('password'); | ||
$inactive = $input->getOption('inactive'); | ||
$superAdmin = $input->getOption('super-admin'); | ||
|
||
$user = $this->userManager->create(); | ||
$user->setUsername($username); | ||
$user->setEmail($email); | ||
$user->setPlainPassword($password); | ||
$user->setEnabled(!$inactive); | ||
$user->setSuperAdmin($superAdmin); | ||
|
||
$this->userManager->save($user); | ||
|
||
$output->writeln(sprintf('Created user "%s".', $username)); | ||
|
||
return 0; | ||
} | ||
} |
This file contains 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,75 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
/* | ||
* This file is part of the Sonata Project package. | ||
* | ||
* (c) Thomas Rabaix <thomas.rabaix@sonata-project.org> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Sonata\UserBundle\Command; | ||
|
||
use Sonata\UserBundle\Model\UserManagerInterface; | ||
use Symfony\Component\Console\Command\Command; | ||
use Symfony\Component\Console\Input\InputArgument; | ||
use Symfony\Component\Console\Input\InputInterface; | ||
use Symfony\Component\Console\Output\OutputInterface; | ||
|
||
/** | ||
* @internal | ||
*/ | ||
final class DeactivateUserCommand extends Command | ||
{ | ||
protected static $defaultName = 'sonata:user:deactivate'; | ||
protected static $defaultDescription = 'Deactivate a user'; | ||
|
||
private UserManagerInterface $userManager; | ||
|
||
public function __construct(UserManagerInterface $userManager) | ||
{ | ||
parent::__construct(); | ||
|
||
$this->userManager = $userManager; | ||
} | ||
|
||
protected function configure(): void | ||
{ | ||
\assert(null !== static::$defaultDescription); | ||
|
||
$this | ||
->setDescription(static::$defaultDescription) | ||
->setDefinition([ | ||
new InputArgument('username', InputArgument::REQUIRED, 'The username'), | ||
]) | ||
->setHelp( | ||
<<<'EOT' | ||
The <info>%command.full_name%</info> command deactivates a user (so they will be unable to log in): | ||
|
||
<info>php %command.full_name% matthieu</info> | ||
EOT | ||
); | ||
} | ||
|
||
protected function execute(InputInterface $input, OutputInterface $output): int | ||
{ | ||
$username = $input->getArgument('username'); | ||
|
||
$user = $this->userManager->findUserByUsername($username); | ||
|
||
if (null === $user) { | ||
throw new \InvalidArgumentException(sprintf('User identified by "%s" username does not exist.', $username)); | ||
} | ||
|
||
$user->setEnabled(false); | ||
|
||
$this->userManager->save($user); | ||
|
||
$output->writeln(sprintf('User "%s" has been activated.', $username)); | ||
|
||
return 0; | ||
} | ||
} |
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Soon, it won't be needed psalm/psalm-plugin-symfony#242 ;)