summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorRyan Weaver <ryan@thatsquality.com>2015-09-20 19:38:23 -0400
committerRyan Weaver <ryan@thatsquality.com>2015-09-20 19:38:23 -0400
commitfeea383a53c0bd09f50c22d8f6dccfd456185c9b (patch)
tree062cf07da909323fce540ffb2ea2bc518611f965
parent07e6e6f92ff935635aa290666b8801e41bd434bc (diff)
downloadsymfony-security-feea383a53c0bd09f50c22d8f6dccfd456185c9b.zip
symfony-security-feea383a53c0bd09f50c22d8f6dccfd456185c9b.tar.gz
symfony-security-feea383a53c0bd09f50c22d8f6dccfd456185c9b.tar.bz2
Adding a base class to assist with form login authentication
-rw-r--r--Guard/Authenticator/AbstractFormLoginAuthenticator.php104
1 files changed, 104 insertions, 0 deletions
diff --git a/Guard/Authenticator/AbstractFormLoginAuthenticator.php b/Guard/Authenticator/AbstractFormLoginAuthenticator.php
new file mode 100644
index 0000000..c972ed6
--- /dev/null
+++ b/Guard/Authenticator/AbstractFormLoginAuthenticator.php
@@ -0,0 +1,104 @@
+<?php
+
+/*
+ * This file is part of the Symfony package.
+ *
+ * (c) Fabien Potencier <fabien@symfony.com>
+ *
+ * For the full copyright and license information, please view the LICENSE
+ * file that was distributed with this source code.
+ */
+
+namespace Symfony\Component\Security\Guard\Authenticator;
+
+use Symfony\Component\Security\Guard\AbstractGuardAuthenticator;
+use Symfony\Component\HttpFoundation\RedirectResponse;
+use Symfony\Component\HttpFoundation\Request;
+use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
+use Symfony\Component\Security\Core\Encoder\UserPasswordEncoderInterface;
+use Symfony\Component\Security\Core\Exception\AuthenticationException;
+use Symfony\Component\Security\Core\Exception\BadCredentialsException;
+use Symfony\Component\Security\Core\Security;
+use Symfony\Component\Security\Core\User\UserInterface;
+use Symfony\Component\Security\Core\User\UserProviderInterface;
+
+/**
+ * A base class to make form login authentication easier!
+ *
+ * @author Ryan Weaver <ryan@knpuniversity.com>
+ */
+abstract class AbstractFormLoginAuthenticator extends AbstractGuardAuthenticator
+{
+ /**
+ * Return the URL to the login page
+ *
+ * @return string
+ */
+ abstract protected function getLoginUrl();
+
+ /**
+ * The user will be redirected to the secure page they originally tried
+ * to access. But if no such page exists (i.e. the user went to the
+ * login page directly), this returns the URL the user should be redirected
+ * to after logging in successfully (e.g. your homepage)
+ *
+ * @return string
+ */
+ abstract protected function getDefaultSuccessRedirectUrl();
+
+ /**
+ * Override to change what happens after a bad username/password is submitted
+ *
+ * @param Request $request
+ * @param AuthenticationException $exception
+ * @return RedirectResponse
+ */
+ public function onAuthenticationFailure(Request $request, AuthenticationException $exception)
+ {
+ $request->getSession()->set(Security::AUTHENTICATION_ERROR, $exception);
+ $url = $this->getLoginUrl();
+
+ return new RedirectResponse($url);
+ }
+
+ /**
+ * Override to change what happens after successful authentication
+ *
+ * @param Request $request
+ * @param TokenInterface $token
+ * @param string $providerKey
+ * @return RedirectResponse
+ */
+ public function onAuthenticationSuccess(Request $request, TokenInterface $token, $providerKey)
+ {
+ // if the user hit a secure page and start() was called, this was
+ // the URL they were on, and probably where you want to redirect to
+ $targetPath = $request->getSession()->get('_security.'.$providerKey.'.target_path');
+
+ if (!$targetPath) {
+ $targetPath = $this->getDefaultSuccessRedirectUrl();
+ }
+
+ return new RedirectResponse($targetPath);
+ }
+
+ public function supportsRememberMe()
+ {
+ return true;
+ }
+
+ /**
+ * Override to control what happens when the user hits a secure page
+ * but isn't logged in yet.
+ *
+ * @param Request $request
+ * @param AuthenticationException|null $authException
+ * @return RedirectResponse
+ */
+ public function start(Request $request, AuthenticationException $authException = null)
+ {
+ $url = $this->getLoginUrl();
+
+ return new RedirectResponse($url);
+ }
+}