|
| 1 | +<?php |
| 2 | + |
| 3 | +/* |
| 4 | + * This file is part of the Symfony package. |
| 5 | + * |
| 6 | + * (c) Fabien Potencier <fabien@symfony.com> |
| 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\SecurityBundle\Tests\Functional\Bundle\AuthenticatorBundle; |
| 13 | + |
| 14 | +use Symfony\Component\HttpFoundation\RedirectResponse; |
| 15 | +use Symfony\Component\HttpFoundation\Request; |
| 16 | +use Symfony\Component\HttpFoundation\Response; |
| 17 | +use Symfony\Component\Routing\Generator\UrlGeneratorInterface; |
| 18 | +use Symfony\Component\Security\Core\Authentication\Token\TokenInterface; |
| 19 | +use Symfony\Component\Security\Core\Security; |
| 20 | +use Symfony\Component\Security\Http\Authenticator\AbstractLoginFormAuthenticator; |
| 21 | +use Symfony\Component\Security\Http\Authenticator\Passport\Badge\UserBadge; |
| 22 | +use Symfony\Component\Security\Http\Authenticator\Passport\Credentials\PasswordCredentials; |
| 23 | +use Symfony\Component\Security\Http\Authenticator\Passport\Passport; |
| 24 | +use Symfony\Component\Security\Http\Authenticator\Passport\PassportInterface; |
| 25 | +use Symfony\Component\Security\Http\Util\TargetPathTrait; |
| 26 | + |
| 27 | +class LoginFormAuthenticator extends AbstractLoginFormAuthenticator |
| 28 | +{ |
| 29 | + use TargetPathTrait; |
| 30 | + |
| 31 | + /** @var UrlGeneratorInterface */ |
| 32 | + private $urlGenerator; |
| 33 | + |
| 34 | + public function __construct(UrlGeneratorInterface $urlGenerator) |
| 35 | + { |
| 36 | + $this->urlGenerator = $urlGenerator; |
| 37 | + } |
| 38 | + |
| 39 | + public function authenticate(Request $request): PassportInterface |
| 40 | + { |
| 41 | + $username = $request->request->get('_username', ''); |
| 42 | + |
| 43 | + $request->getSession()->set(Security::LAST_USERNAME, $username); |
| 44 | + |
| 45 | + return new Passport( |
| 46 | + new UserBadge($username), |
| 47 | + new PasswordCredentials($request->request->get('_password', '')), |
| 48 | + [] |
| 49 | + ); |
| 50 | + } |
| 51 | + |
| 52 | + public function onAuthenticationSuccess(Request $request, TokenInterface $token, $firewallName): ?Response |
| 53 | + { |
| 54 | + return new RedirectResponse($this->urlGenerator->generate('security_'.$firewallName.'_profile')); |
| 55 | + } |
| 56 | + |
| 57 | + protected function getLoginUrl(Request $request): string |
| 58 | + { |
| 59 | + return strpos($request->getUri(), 'main') ? |
| 60 | + $this->urlGenerator->generate('security_main_login') : |
| 61 | + $this->urlGenerator->generate('security_custom_login') |
| 62 | + ; |
| 63 | + } |
| 64 | +} |
0 commit comments