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

[make:security:form-login] no interaction for make:security:form-login #1595

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/Maker/Common/CanGenerateTestsTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ public function interactSetGenerateTests(InputInterface $input, ConsoleStyle $io

if (!$this->generateTests) {
$this->generateTests = $io->confirm('Do you want to generate PHPUnit tests? [Experimental]', false);
$input->setOption('with-tests', $this->generateTests);
}
}

Expand Down
66 changes: 45 additions & 21 deletions src/Maker/Security/MakeFormLogin.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,9 @@
use Symfony\Bundle\SecurityBundle\SecurityBundle;
use Symfony\Bundle\TwigBundle\TwigBundle;
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\HttpFoundation\Response;
use Symfony\Component\PasswordHasher\Hasher\UserPasswordHasherInterface;
use Symfony\Component\Routing\Attribute\Route;
Expand All @@ -57,11 +59,11 @@ final class MakeFormLogin extends AbstractMaker

private const SECURITY_CONFIG_PATH = 'config/packages/security.yaml';
private YamlSourceManipulator $ysm;
private string $controllerName;
private string $firewallToUpdate;
private string $userClass;
private string $userNameField;
private bool $willLogout;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We need to keep both of these properties. MakerBundle is getting away from "abusing" setArgument and getArgument for the purposes of passing user input from one method to the next within a maker command. e.g. separation of concerns.

tl;dr interact() configures and sets any values onto their respective properties that will later be needed in generate()

interact() should get, validate, and set input.
generate() should only use values that are set via class properties when possible.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ok so we don't have to use --no-interaction option for all makers ?

/** @var ?array<string, mixed> */
private ?array $securityData = null;

public function __construct(
private FileManager $fileManager,
Expand All @@ -77,9 +79,12 @@ public static function getCommandName(): string

public function configureCommand(Command $command, InputConfiguration $inputConfig): void
{
$command->setHelp(file_get_contents(\dirname(__DIR__, 2).'/Resources/help/security/MakeFormLogin.txt'));
$command->addArgument('controllerName', InputArgument::OPTIONAL, 'The class name of the Controller (e.g. <fg=yellow>SecurityController</>)')
->addOption('will-logout', null, InputOption::VALUE_NONE, 'Will generate a \'/logout\' URL? ')
->setHelp(file_get_contents(\dirname(__DIR__, 2).'/Resources/help/security/MakeFormLogin.txt'));

$this->configureCommandWithTestsOption($command);
$inputConfig->setArgumentAsNonInteractive('controllerName');
}

public static function getCommandDescription(): string
Expand Down Expand Up @@ -111,38 +116,44 @@ public function interact(InputInterface $input, ConsoleStyle $io, Command $comma
throw new RuntimeCommandException(\sprintf('The file "%s" does not exist. PHP & XML configuration formats are currently not supported.', self::SECURITY_CONFIG_PATH));
}

$this->ysm = new YamlSourceManipulator($this->fileManager->getFileContents(self::SECURITY_CONFIG_PATH));
$securityData = $this->ysm->getData();
$securityData = $this->getSecurityData();

if (!isset($securityData['security']['providers']) || !$securityData['security']['providers']) {
throw new RuntimeCommandException('To generate a form login authentication, you must configure at least one entry under "providers" in "security.yaml".');
}

$this->controllerName = $io->ask(
'Choose a name for the controller class (e.g. <fg=yellow>SecurityController</>)',
'SecurityController',
Validator::validateClassName(...)
);
if (null === $input->getArgument('controllerName')) {
$input->setArgument(
'controllerName', $io->ask(
'Choose a name for the controller class (e.g. <fg=yellow>SecurityController</>)',
'SecurityController',
Validator::validateClassName(...)
));
}

$securityHelper = new InteractiveSecurityHelper();
$this->firewallToUpdate = $securityHelper->guessFirewallName($io, $securityData);
$this->userClass = $securityHelper->guessUserClass($io, $securityData['security']['providers']);
$this->userNameField = $securityHelper->guessUserNameField($io, $this->userClass, $securityData['security']['providers']);
$this->willLogout = $io->confirm('Do you want to generate a \'/logout\' URL?');
eltharin marked this conversation as resolved.
Show resolved Hide resolved
if (false === $input->getOption('will-logout')) {
$input->setOption('will-logout', $io->confirm('Do you want to generate a \'/logout\' URL?'));
}

$this->interactSetGenerateTests($input, $io);
}

public function generate(InputInterface $input, ConsoleStyle $io, Generator $generator): void
{
$securityData = $this->getSecurityData();
$securityHelper = new InteractiveSecurityHelper();
$this->firewallToUpdate = $securityHelper->guessFirewallName($io, $securityData);
$this->userClass = $securityHelper->guessUserClass($io, $securityData['security']['providers']);
$this->userNameField = $securityHelper->guessUserNameField($io, $this->userClass, $securityData['security']['providers']);

$useStatements = new UseStatementGenerator([
AbstractController::class,
Response::class,
Route::class,
AuthenticationUtils::class,
]);

$controllerNameDetails = $generator->createClassNameDetails($this->controllerName, 'Controller\\', 'Controller');
$controllerNameDetails = $generator->createClassNameDetails($input->getArgument('controllerName'), 'Controller\\', 'Controller');
$templatePath = strtolower($controllerNameDetails->getRelativeNameWithoutSuffix());

$controllerPath = $generator->generateController(
Expand All @@ -155,7 +166,7 @@ public function generate(InputInterface $input, ConsoleStyle $io, Generator $gen
]
);

if ($this->willLogout) {
if ($input->getOption('will-logout')) {
$manipulator = new ClassSourceManipulator($generator->getFileContentsForPendingOperation($controllerPath));

$this->securityControllerBuilder->addLogoutMethod($manipulator);
Expand All @@ -167,26 +178,26 @@ public function generate(InputInterface $input, ConsoleStyle $io, Generator $gen
\sprintf('%s/login.html.twig', $templatePath),
'security/formLogin/login_form.tpl.php',
[
'logout_setup' => $this->willLogout,
'logout_setup' => $input->getOption('will-logout'),
'username_label' => Str::asHumanWords($this->userNameField),
'username_is_email' => false !== stripos($this->userNameField, 'email'),
]
);

$securityData = $this->securityConfigUpdater->updateForFormLogin($this->ysm->getContents(), $this->firewallToUpdate, 'app_login', 'app_login');

if ($this->willLogout) {
if ($input->getOption('will-logout')) {
$securityData = $this->securityConfigUpdater->updateForLogout($securityData, $this->firewallToUpdate);
}

if ($this->shouldGenerateTests()) {
if ($input->getOption('with-tests')) {
$userClassNameDetails = $generator->createClassNameDetails(
'\\'.$this->userClass,
'Entity\\'
);

$testClassDetails = $generator->createClassNameDetails(
'LoginControllerTest',
$controllerNameDetails->getShortName().'Test',
'Test\\',
);

Expand Down Expand Up @@ -223,4 +234,17 @@ public function generate(InputInterface $input, ConsoleStyle $io, Generator $gen
\sprintf('Next: Review and adapt the login template: <info>%s/login.html.twig</info> to suit your needs.', $templatePath),
]);
}

/**
* @return array<string, mixed> $items
*/
private function getSecurityData(): array
{
if (null === $this->securityData) {
$this->ysm = new YamlSourceManipulator($this->fileManager->getFileContents(self::SECURITY_CONFIG_PATH));
$this->securityData = $this->ysm->getData();
}

return $this->securityData;
}
}
Loading