|
| 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\Component\Security\Guard\Authenticator; |
| 13 | + |
| 14 | +use Symfony\Component\Security\Guard\AbstractGuardAuthenticator; |
| 15 | +use Symfony\Component\HttpFoundation\RedirectResponse; |
| 16 | +use Symfony\Component\HttpFoundation\Request; |
| 17 | +use Symfony\Component\Security\Core\Authentication\Token\TokenInterface; |
| 18 | +use Symfony\Component\Security\Core\Encoder\UserPasswordEncoderInterface; |
| 19 | +use Symfony\Component\Security\Core\Exception\AuthenticationException; |
| 20 | +use Symfony\Component\Security\Core\Exception\BadCredentialsException; |
| 21 | +use Symfony\Component\Security\Core\Security; |
| 22 | +use Symfony\Component\Security\Core\User\UserInterface; |
| 23 | +use Symfony\Component\Security\Core\User\UserProviderInterface; |
| 24 | + |
| 25 | +/** |
| 26 | + * A base class to make form login authentication easier! |
| 27 | + * |
| 28 | + * @author Ryan Weaver <ryan@knpuniversity.com> |
| 29 | + */ |
| 30 | +abstract class AbstractFormLoginAuthenticator extends AbstractGuardAuthenticator |
| 31 | +{ |
| 32 | + /** |
| 33 | + * Return the URL to the login page |
| 34 | + * |
| 35 | + * @return string |
| 36 | + */ |
| 37 | + abstract protected function getLoginUrl(); |
| 38 | + |
| 39 | + /** |
| 40 | + * The user will be redirected to the secure page they originally tried |
| 41 | + * to access. But if no such page exists (i.e. the user went to the |
| 42 | + * login page directly), this returns the URL the user should be redirected |
| 43 | + * to after logging in successfully (e.g. your homepage) |
| 44 | + * |
| 45 | + * @return string |
| 46 | + */ |
| 47 | + abstract protected function getDefaultSuccessRedirectUrl(); |
| 48 | + |
| 49 | + /** |
| 50 | + * Override to change what happens after a bad username/password is submitted |
| 51 | + * |
| 52 | + * @param Request $request |
| 53 | + * @param AuthenticationException $exception |
| 54 | + * @return RedirectResponse |
| 55 | + */ |
| 56 | + public function onAuthenticationFailure(Request $request, AuthenticationException $exception) |
| 57 | + { |
| 58 | + $request->getSession()->set(Security::AUTHENTICATION_ERROR, $exception); |
| 59 | + $url = $this->getLoginUrl(); |
| 60 | + |
| 61 | + return new RedirectResponse($url); |
| 62 | + } |
| 63 | + |
| 64 | + /** |
| 65 | + * Override to change what happens after successful authentication |
| 66 | + * |
| 67 | + * @param Request $request |
| 68 | + * @param TokenInterface $token |
| 69 | + * @param string $providerKey |
| 70 | + * @return RedirectResponse |
| 71 | + */ |
| 72 | + public function onAuthenticationSuccess(Request $request, TokenInterface $token, $providerKey) |
| 73 | + { |
| 74 | + // if the user hit a secure page and start() was called, this was |
| 75 | + // the URL they were on, and probably where you want to redirect to |
| 76 | + $targetPath = $request->getSession()->get('_security.'.$providerKey.'.target_path'); |
| 77 | + |
| 78 | + if (!$targetPath) { |
| 79 | + $targetPath = $this->getDefaultSuccessRedirectUrl(); |
| 80 | + } |
| 81 | + |
| 82 | + return new RedirectResponse($targetPath); |
| 83 | + } |
| 84 | + |
| 85 | + public function supportsRememberMe() |
| 86 | + { |
| 87 | + return true; |
| 88 | + } |
| 89 | + |
| 90 | + /** |
| 91 | + * Override to control what happens when the user hits a secure page |
| 92 | + * but isn't logged in yet. |
| 93 | + * |
| 94 | + * @param Request $request |
| 95 | + * @param AuthenticationException|null $authException |
| 96 | + * @return RedirectResponse |
| 97 | + */ |
| 98 | + public function start(Request $request, AuthenticationException $authException = null) |
| 99 | + { |
| 100 | + $url = $this->getLoginUrl(); |
| 101 | + |
| 102 | + return new RedirectResponse($url); |
| 103 | + } |
| 104 | +} |
0 commit comments