Skip to content

Commit f96c51e

Browse files
committed
Add logout method to already existing SecurityController
1 parent 0bfd028 commit f96c51e

File tree

4 files changed

+20
-2
lines changed

4 files changed

+20
-2
lines changed

src/Maker/MakeAuthenticator.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,7 @@ public function interact(InputInterface $input, ConsoleStyle $io, Command $comma
105105
$questionAuthenticatorClass->setValidator(
106106
function ($answer) {
107107
Validator::notBlank($answer);
108+
108109
return Validator::validateClassDoesNotExist(
109110
$this->generator->createClassNameDetails(
110111
$answer,
@@ -187,6 +188,12 @@ public function generate(InputInterface $input, ConsoleStyle $io, Generator $gen
187188
);
188189
$securityControllerBuilder = new SecurityControllerBuilder();
189190
$securityControllerBuilder->addLoginMethod($manipulator);
191+
192+
if (method_exists($controllerClassNameDetails->getFullName(), 'logout')) {
193+
throw new RuntimeCommandException(sprintf('Method "logout" already exists on class %s', $controllerClassNameDetails->getFullName()));
194+
}
195+
$securityControllerBuilder->addLogoutMethod($manipulator);
196+
190197
$this->generator->dumpFile($controllerPath, $manipulator->getSourceCode());
191198
} else {
192199
// otherwise, create security controller

src/Resources/skeleton/authenticator/LoginFormNotEntityAuthenticator.tpl.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ public function getUser($credentials, UserProviderInterface $userProvider)
5959
throw new InvalidCsrfTokenException();
6060
}
6161

62-
return $userProvider->loadUserByUsername($credentials['email']);A
62+
return $userProvider->loadUserByUsername($credentials['email']);
6363
}
6464

6565
public function checkCredentials($credentials, UserInterface $user)

src/Security/SecurityControllerBuilder.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,4 +52,16 @@ public function addLoginMethod(ClassSourceManipulator $manipulator)
5252
$manipulator->addUseStatementIfNecessary('Symfony\\Component\\Routing\\Annotation\\Route');
5353
$manipulator->addUseStatementIfNecessary('Symfony\\Component\\Security\\Http\\Authentication\\AuthenticationUtils');
5454
}
55+
56+
public function addLogoutMethod(ClassSourceManipulator $manipulator)
57+
{
58+
$loginMethodBuilder = $manipulator->createMethodBuilder('logout', null, false, ['@Route("/logout", name="app_logout")']);
59+
60+
$manipulator->addMethodBody($loginMethodBuilder, <<<'CODE'
61+
<?php
62+
throw new \Exception('will be intercepted before getting here');
63+
CODE
64+
);
65+
$manipulator->addMethodBuilder($loginMethodBuilder);
66+
}
5567
}

src/Util/ClassSourceManipulator.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -777,7 +777,6 @@ private function setSourceCode(string $sourceCode)
777777
{
778778
$this->sourceCode = $sourceCode;
779779
$this->oldStmts = $this->parser->parse($sourceCode);
780-
dump($this->oldStmts);
781780
$this->oldTokens = $this->lexer->getTokens();
782781

783782
$traverser = new NodeTraverser();

0 commit comments

Comments
 (0)