|
| 1 | +<?php |
| 2 | + |
| 3 | +/* |
| 4 | + * This file is part of the Symfony MakerBundle package. |
| 5 | + * |
| 6 | + * (c) Fabien Potencier <[email protected]> |
| 7 | + * |
| 8 | + * For the full copyright and license information, please view the LICENSE |
| 9 | + * file that was distributed with this source code. |
| 10 | + */ |
| 11 | + |
| 12 | +namespace Symfony\Bundle\MakerBundle\Maker\Security; |
| 13 | + |
| 14 | +use Doctrine\Bundle\DoctrineBundle\DoctrineBundle; |
| 15 | +use Symfony\Bundle\FrameworkBundle\Controller\AbstractController; |
| 16 | +use Symfony\Bundle\MakerBundle\ConsoleStyle; |
| 17 | +use Symfony\Bundle\MakerBundle\DependencyBuilder; |
| 18 | +use Symfony\Bundle\MakerBundle\Exception\RuntimeCommandException; |
| 19 | +use Symfony\Bundle\MakerBundle\FileManager; |
| 20 | +use Symfony\Bundle\MakerBundle\Generator; |
| 21 | +use Symfony\Bundle\MakerBundle\InputConfiguration; |
| 22 | +use Symfony\Bundle\MakerBundle\Maker\AbstractMaker; |
| 23 | +use Symfony\Bundle\MakerBundle\Security\InteractiveSecurityHelper; |
| 24 | +use Symfony\Bundle\MakerBundle\Security\SecurityConfigUpdater; |
| 25 | +use Symfony\Bundle\MakerBundle\Security\SecurityControllerBuilder; |
| 26 | +use Symfony\Bundle\MakerBundle\Str; |
| 27 | +use Symfony\Bundle\MakerBundle\Util\ClassSourceManipulator; |
| 28 | +use Symfony\Bundle\MakerBundle\Util\UseStatementGenerator; |
| 29 | +use Symfony\Bundle\MakerBundle\Util\YamlSourceManipulator; |
| 30 | +use Symfony\Bundle\MakerBundle\Validator; |
| 31 | +use Symfony\Bundle\SecurityBundle\SecurityBundle; |
| 32 | +use Symfony\Bundle\TwigBundle\TwigBundle; |
| 33 | +use Symfony\Component\Console\Command\Command; |
| 34 | +use Symfony\Component\Console\Input\InputInterface; |
| 35 | +use Symfony\Component\HttpFoundation\Response; |
| 36 | +use Symfony\Component\Routing\Annotation\Route; |
| 37 | +use Symfony\Component\Security\Http\Authentication\AuthenticationUtils; |
| 38 | +use Symfony\Component\Yaml\Yaml; |
| 39 | + |
| 40 | +/** |
| 41 | + * Generate Form Login Security using SecurityBundle's Authenticator. |
| 42 | + * |
| 43 | + * @see https://symfony.com/doc/current/security.html#form-login |
| 44 | + * |
| 45 | + * @author Jesse Rushlow <[email protected]> |
| 46 | + * |
| 47 | + * @internal |
| 48 | + */ |
| 49 | +final class MakeFormLogin extends AbstractMaker |
| 50 | +{ |
| 51 | + private const SECURITY_CONFIG_PATH = 'config/packages/security.yaml'; |
| 52 | + private YamlSourceManipulator $ysm; |
| 53 | + private string $controllerName; |
| 54 | + private string $firewallToUpdate; |
| 55 | + private string $userNameField; |
| 56 | + private bool $willLogout; |
| 57 | + |
| 58 | + public function __construct( |
| 59 | + private FileManager $fileManager, |
| 60 | + private SecurityConfigUpdater $securityConfigUpdater, |
| 61 | + private SecurityControllerBuilder $securityControllerBuilder, |
| 62 | + ) { |
| 63 | + } |
| 64 | + |
| 65 | + public static function getCommandName(): string |
| 66 | + { |
| 67 | + return 'make:security:form-login'; |
| 68 | + } |
| 69 | + |
| 70 | + public function configureCommand(Command $command, InputConfiguration $inputConfig): void |
| 71 | + { |
| 72 | + $command->setHelp(file_get_contents(\dirname(__DIR__, 2).'/Resources/help/security/MakeFormLogin.txt')); |
| 73 | + } |
| 74 | + |
| 75 | + public static function getCommandDescription(): string |
| 76 | + { |
| 77 | + return 'Generate the code needed for the form_login authenticator'; |
| 78 | + } |
| 79 | + |
| 80 | + public function configureDependencies(DependencyBuilder $dependencies): void |
| 81 | + { |
| 82 | + $dependencies->addClassDependency( |
| 83 | + SecurityBundle::class, |
| 84 | + 'security' |
| 85 | + ); |
| 86 | + |
| 87 | + $dependencies->addClassDependency(TwigBundle::class, 'twig'); |
| 88 | + |
| 89 | + // needed to update the YAML files |
| 90 | + $dependencies->addClassDependency( |
| 91 | + Yaml::class, |
| 92 | + 'yaml' |
| 93 | + ); |
| 94 | + |
| 95 | + $dependencies->addClassDependency(DoctrineBundle::class, 'orm'); |
| 96 | + } |
| 97 | + |
| 98 | + public function interact(InputInterface $input, ConsoleStyle $io, Command $command): void |
| 99 | + { |
| 100 | + if (!$this->fileManager->fileExists(self::SECURITY_CONFIG_PATH)) { |
| 101 | + throw new RuntimeCommandException(sprintf('The file "%s" does not exist. PHP & XML configuration formats are currently not supported.', self::SECURITY_CONFIG_PATH)); |
| 102 | + } |
| 103 | + |
| 104 | + $this->ysm = new YamlSourceManipulator($this->fileManager->getFileContents(self::SECURITY_CONFIG_PATH)); |
| 105 | + $securityData = $this->ysm->getData(); |
| 106 | + |
| 107 | + if (!isset($securityData['security']['providers']) || !$securityData['security']['providers']) { |
| 108 | + throw new RuntimeCommandException('To generate a form login authentication, you must configure at least one entry under "providers" in "security.yaml".'); |
| 109 | + } |
| 110 | + |
| 111 | + $this->controllerName = $io->ask( |
| 112 | + 'Choose a name for the controller class (e.g. <fg=yellow>SecurityController</>)', |
| 113 | + 'SecurityController', |
| 114 | + [Validator::class, 'validateClassName'] |
| 115 | + ); |
| 116 | + |
| 117 | + $securityHelper = new InteractiveSecurityHelper(); |
| 118 | + $this->firewallToUpdate = $securityHelper->guessFirewallName($io, $securityData); |
| 119 | + $userClass = $securityHelper->guessUserClass($io, $securityData['security']['providers']); |
| 120 | + $this->userNameField = $securityHelper->guessUserNameField($io, $userClass, $securityData['security']['providers']); |
| 121 | + $this->willLogout = $io->confirm('Do you want to generate a \'/logout\' URL?'); |
| 122 | + } |
| 123 | + |
| 124 | + public function generate(InputInterface $input, ConsoleStyle $io, Generator $generator): void |
| 125 | + { |
| 126 | + $useStatements = new UseStatementGenerator([ |
| 127 | + AbstractController::class, |
| 128 | + Response::class, |
| 129 | + Route::class, |
| 130 | + AuthenticationUtils::class, |
| 131 | + ]); |
| 132 | + |
| 133 | + $controllerNameDetails = $generator->createClassNameDetails($this->controllerName, 'Controller\\', 'Controller'); |
| 134 | + $templatePath = strtolower($controllerNameDetails->getRelativeNameWithoutSuffix()); |
| 135 | + |
| 136 | + $controllerPath = $generator->generateController( |
| 137 | + $controllerNameDetails->getFullName(), |
| 138 | + 'security/formLogin/LoginController.tpl.php', |
| 139 | + [ |
| 140 | + 'use_statements' => $useStatements, |
| 141 | + 'controller_name' => $controllerNameDetails->getShortName(), |
| 142 | + 'template_path' => $templatePath, |
| 143 | + ] |
| 144 | + ); |
| 145 | + |
| 146 | + if ($this->willLogout) { |
| 147 | + $manipulator = new ClassSourceManipulator($generator->getFileContentsForPendingOperation($controllerPath)); |
| 148 | + |
| 149 | + $this->securityControllerBuilder->addLogoutMethod($manipulator); |
| 150 | + |
| 151 | + $generator->dumpFile($controllerPath, $manipulator->getSourceCode()); |
| 152 | + } |
| 153 | + |
| 154 | + $generator->generateTemplate( |
| 155 | + sprintf('%s/login.html.twig', $templatePath), |
| 156 | + 'security/formLogin/login_form.tpl.php', |
| 157 | + [ |
| 158 | + 'logout_setup' => $this->willLogout, |
| 159 | + 'username_label' => Str::asHumanWords($this->userNameField), |
| 160 | + 'username_is_email' => false !== stripos($this->userNameField, 'email'), |
| 161 | + ] |
| 162 | + ); |
| 163 | + |
| 164 | + $securityData = $this->securityConfigUpdater->updateForFormLogin($this->ysm->getContents(), $this->firewallToUpdate, 'app_login', 'app_login'); |
| 165 | + |
| 166 | + if ($this->willLogout) { |
| 167 | + $securityData = $this->securityConfigUpdater->updateForLogout($securityData, $this->firewallToUpdate); |
| 168 | + } |
| 169 | + |
| 170 | + $generator->dumpFile(self::SECURITY_CONFIG_PATH, $securityData); |
| 171 | + |
| 172 | + $generator->writeChanges(); |
| 173 | + |
| 174 | + $this->writeSuccessMessage($io); |
| 175 | + |
| 176 | + $io->text([ |
| 177 | + sprintf('Next: Review and adapt the login template: <info>%s/login.html.twig</info> to suit your needs.', $templatePath), |
| 178 | + ]); |
| 179 | + } |
| 180 | +} |
0 commit comments