Skip to content
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
merged 14 commits into from
Jan 30, 2022
1 change: 1 addition & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
"sonata-project/form-extensions": "^1.4",
"sonata-project/twig-extensions": "^1.3",
"symfony/config": "^4.4.11 || ^5.3 || ^6.0",
"symfony/console": "^4.4 || ^5.3 || ^6.0",
"symfony/dependency-injection": "^4.4 || ^5.3 || ^6.0",
"symfony/event-dispatcher": "^4.4 || ^5.3 || ^6.0",
"symfony/form": "^4.4.20 || ^5.3 || ^6.0",
Expand Down
13 changes: 13 additions & 0 deletions psalm-baseline.xml
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>
Copy link
Member

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 ;)

</UndefinedInterfaceMethod>
</file>
<file src="src/Action/ResetAction.php">
<UndefinedInterfaceMethod occurrences="1">
<code>getFlashBag</code>
</UndefinedInterfaceMethod>
</file>
</files>
2 changes: 1 addition & 1 deletion psalm.xml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<?xml version="1.0" encoding="UTF-8"?>
<psalm xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="https://getpsalm.org/schema/config" errorLevel="2" findUnusedPsalmSuppress="true" resolveFromConfigFile="true" xsi:schemaLocation="https://getpsalm.org/schema/config vendor/vimeo/psalm/config.xsd">
<psalm xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="https://getpsalm.org/schema/config" errorLevel="2" findUnusedPsalmSuppress="true" resolveFromConfigFile="true" xsi:schemaLocation="https://getpsalm.org/schema/config vendor/vimeo/psalm/config.xsd" errorBaseline="psalm-baseline.xml">
<projectFiles>
<directory name="src"/>
<directory name="tests"/>
Expand Down
6 changes: 1 addition & 5 deletions src/Action/LoginAction.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@
use Symfony\Component\HttpFoundation\RedirectResponse;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpFoundation\Session\Session;
use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface;
use Symfony\Component\Security\Csrf\CsrfTokenManagerInterface;
Expand Down Expand Up @@ -68,10 +67,7 @@ public function __construct(
public function __invoke(Request $request): Response
{
if ($this->isAuthenticated()) {
$session = $request->getSession();
\assert($session instanceof Session);

$session->getFlashBag()->add(
$request->getSession()->getFlashBag()->add(
'sonata_user_error',
$this->translator->trans('sonata_user_already_authenticated', [], 'SonataUserBundle')
);
Expand Down
6 changes: 1 addition & 5 deletions src/Action/ResetAction.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@
use Symfony\Component\HttpFoundation\RedirectResponse;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpFoundation\Session\Session;
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
use Symfony\Component\Security\Core\Authorization\AuthorizationCheckerInterface;
Expand Down Expand Up @@ -96,10 +95,7 @@ public function __invoke(Request $request, string $token): Response
$user->setPasswordRequestedAt(null);
$user->setEnabled(true);

$session = $request->getSession();
\assert($session instanceof Session);

$session->getFlashBag()->add(
$request->getSession()->getFlashBag()->add(
'success',
$this->translator->trans('resetting.flash.success', [], 'SonataUserBundle')
);
Expand Down
75 changes: 75 additions & 0 deletions src/Command/ActivateUserCommand.php
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;
}
}
79 changes: 79 additions & 0 deletions src/Command/ChangePasswordCommand.php
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;
}
}
92 changes: 92 additions & 0 deletions src/Command/CreateUserCommand.php
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;
}
}
75 changes: 75 additions & 0 deletions src/Command/DeactivateUserCommand.php
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;
}
}
Loading