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:auth] Use LogoutRouteLoader for logout route when available #1376

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

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
14 changes: 8 additions & 6 deletions src/Maker/MakeAuthenticator.php
Original file line number Diff line number Diff line change
Expand Up @@ -275,7 +275,8 @@ public function generate(InputInterface $input, ConsoleStyle $io, Generator $gen
$input->hasArgument('user-class') ? $input->getArgument('user-class') : null,
$input->hasArgument('logout-setup') ? $input->getArgument('logout-setup') : false,
$supportRememberMe,
$alwaysRememberMe
$alwaysRememberMe,
!file_exists('config/routes/security.yaml'),
)
);
}
Expand Down Expand Up @@ -380,10 +381,6 @@ private function generateFormLoginFiles(string $controllerClass, string $userNam

$this->securityControllerBuilder->addLoginMethod($manipulator);

if ($logoutSetup) {
$this->securityControllerBuilder->addLogoutMethod($manipulator);
}

$this->generator->dumpFile($controllerPath, $manipulator->getSourceCode());

// create login form template
Expand All @@ -401,7 +398,7 @@ private function generateFormLoginFiles(string $controllerClass, string $userNam
);
}

private function generateNextMessage(bool $securityYamlUpdated, string $authenticatorType, string $authenticatorClass, $userClass, bool $logoutSetup, bool $supportRememberMe, bool $alwaysRememberMe): array
private function generateNextMessage(bool $securityYamlUpdated, string $authenticatorType, string $authenticatorClass, $userClass, bool $logoutSetup, bool $supportRememberMe, bool $alwaysRememberMe, bool $hasSecurityRouteFile): array
{
$nextTexts = ['Next:'];
$nextTexts[] = '- Customize your new authenticator.';
Expand Down Expand Up @@ -429,6 +426,11 @@ private function generateNextMessage(bool $securityYamlUpdated, string $authenti
$nextTexts[] = '- Review & adapt the login template: <info>'.$this->fileManager->getPathForTemplate('security/login.html.twig').'</info>.';
}

// If the security.logout_route_loader is not loaded from the default flex recipe
if (!$hasSecurityRouteFile) {
$nextTexts[] = '- Be sure to add the "logout" route to <info>config/routes/security.yaml</info> or upgrade the <info>symfony/security-bundle</info> recipe.';
}

return $nextTexts;
}

Expand Down
11 changes: 1 addition & 10 deletions src/Maker/Security/MakeFormLogin.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@
use Symfony\Bundle\MakerBundle\Security\SecurityConfigUpdater;
use Symfony\Bundle\MakerBundle\Security\SecurityControllerBuilder;
use Symfony\Bundle\MakerBundle\Str;
use Symfony\Bundle\MakerBundle\Util\ClassSourceManipulator;
use Symfony\Bundle\MakerBundle\Util\UseStatementGenerator;
use Symfony\Bundle\MakerBundle\Util\YamlSourceManipulator;
use Symfony\Bundle\MakerBundle\Validator;
Expand Down Expand Up @@ -133,7 +132,7 @@ public function generate(InputInterface $input, ConsoleStyle $io, Generator $gen
$controllerNameDetails = $generator->createClassNameDetails($this->controllerName, 'Controller\\', 'Controller');
$templatePath = strtolower($controllerNameDetails->getRelativeNameWithoutSuffix());

$controllerPath = $generator->generateController(
$generator->generateController(
$controllerNameDetails->getFullName(),
'security/formLogin/LoginController.tpl.php',
[
Expand All @@ -143,14 +142,6 @@ public function generate(InputInterface $input, ConsoleStyle $io, Generator $gen
]
);

if ($this->willLogout) {
$manipulator = new ClassSourceManipulator($generator->getFileContentsForPendingOperation($controllerPath));

$this->securityControllerBuilder->addLogoutMethod($manipulator);

$generator->dumpFile($controllerPath, $manipulator->getSourceCode());
}

$generator->generateTemplate(
sprintf('%s/login.html.twig', $templatePath),
'security/formLogin/login_form.tpl.php',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
<?php if ($logout_setup): ?>
{% if app.user %}
<div class="mb-3">
You are logged in as {{ app.user.userIdentifier }}, <a href="{{ path('app_logout') }}">Logout</a>
You are logged in as {{ app.user.userIdentifier }}, <a href="{{ logout_path() }}">Logout</a>
</div>
{% endif %}

Expand Down
4 changes: 2 additions & 2 deletions src/Security/SecurityConfigUpdater.php
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ public function updateForAuthenticator(string $yamlSource, string $firewallName,
}

if (!isset($firewall['logout']) && $logoutSetup) {
$firewall['logout'] = ['path' => 'app_logout'];
$firewall['logout'] = ['path' => '/logout'];
$firewall['logout'][] = $this->manipulator->createCommentLine(
' where to redirect after logout'
);
Expand Down Expand Up @@ -172,7 +172,7 @@ public function updateForLogout(string $yamlSource, string $firewallName): strin
*/
private function configureLogout(array $securityData, string $firewallName): void
{
$securityData['security']['firewalls'][$firewallName]['logout'] = ['path' => 'app_logout'];
$securityData['security']['firewalls'][$firewallName]['logout'] = ['path' => '/logout'];
$securityData['security']['firewalls'][$firewallName]['logout'][] = $this->manipulator->createCommentLine(
' where to redirect after logout'
);
Expand Down
2 changes: 1 addition & 1 deletion tests/Maker/MakeAuthenticatorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -271,7 +271,7 @@ public function getTestDetails(): \Generator

$securityConfig = $runner->readYaml('config/packages/security.yaml');
$this->assertEquals(
'app_logout',
'/logout',
$securityConfig['security']['firewalls']['main']['logout']['path']
);
}),
Expand Down
6 changes: 3 additions & 3 deletions tests/Maker/Security/MakeFormLoginTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ public function getTestDetails(): \Generator
$this->assertSame('app_login', $securityConfig['security']['firewalls']['main']['form_login']['login_path']);
$this->assertSame('app_login', $securityConfig['security']['firewalls']['main']['form_login']['check_path']);
$this->assertTrue($securityConfig['security']['firewalls']['main']['form_login']['enable_csrf']);
$this->assertSame('app_logout', $securityConfig['security']['firewalls']['main']['logout']['path']);
$this->assertSame('/logout', $securityConfig['security']['firewalls']['main']['logout']['path']);

$this->runLoginTest($runner);
}),
Expand All @@ -66,7 +66,7 @@ public function getTestDetails(): \Generator
$this->assertStringContainsString('Success', $output);
$fixturePath = \dirname(__DIR__, 2).'/fixtures/security/make-form-login/expected';

$this->assertFileEquals($fixturePath.'/SecurityControllerWithoutLogout.php', $runner->getPath('src/Controller/SecurityController.php'));
$this->assertFileEquals($fixturePath.'/SecurityController.php', $runner->getPath('src/Controller/SecurityController.php'));
$this->assertFileEquals($fixturePath.'/login_no_logout.html.twig', $runner->getPath('templates/security/login.html.twig'));

$securityConfig = $runner->readYaml('config/packages/security.yaml');
Expand Down Expand Up @@ -96,7 +96,7 @@ public function getTestDetails(): \Generator

$this->assertSame('app_login', $securityConfig['security']['firewalls']['main']['form_login']['login_path']);
$this->assertSame('app_login', $securityConfig['security']['firewalls']['main']['form_login']['check_path']);
$this->assertSame('app_logout', $securityConfig['security']['firewalls']['main']['logout']['path']);
$this->assertSame('/logout', $securityConfig['security']['firewalls']['main']['logout']['path']);
}),
];
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,6 @@ security:
# the entry_point start() method determines what happens when an anonymous user accesses a protected page
entry_point: App\Security\AppCustomAuthenticator
logout:
path: app_logout
path: /logout
# where to redirect after logout
# target: app_any_route
2 changes: 1 addition & 1 deletion tests/Security/yaml_fixtures/expected_logout/logout.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,6 @@ security:
main:
lazy: true
logout:
path: app_logout
path: /logout
# where to redirect after logout
# target: app_any_route
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,4 @@ public function login(AuthenticationUtils $authenticationUtils): Response
'error' => $error,
]);
}

#[Route(path: '/logout', name: 'app_logout')]
public function logout(): void
{
throw new \LogicException('This method can be blank - it will be intercepted by the logout key on your firewall.');
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,4 @@ public function login(AuthenticationUtils $authenticationUtils): Response
'error' => $error,
]);
}

#[Route(path: '/logout', name: 'app_logout')]
public function logout(): void
{
throw new \LogicException('This method can be blank - it will be intercepted by the logout key on your firewall.');
}
}

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@

{% if app.user %}
<div class="mb-3">
You are logged in as {{ app.user.userIdentifier }}, <a href="{{ path('app_logout') }}">Logout</a>
You are logged in as {{ app.user.userIdentifier }}, <a href="{{ logout_path() }}">Logout</a>
</div>
{% endif %}

Expand Down