From 1722f60d42bb8784ec6623fa22ae763de7ab5b09 Mon Sep 17 00:00:00 2001 From: Boris Vujicic Date: Sat, 5 Jul 2014 16:07:05 +0200 Subject: [PATCH] [SecurityBundle] error helper added symfony/symfony#11147 --- .../Resources/config/security.xml | 6 ++ src/Symfony/Component/Security/CHANGELOG.md | 5 ++ .../Authentication/AuthenticationUtils.php | 87 +++++++++++++++++++ 3 files changed, 98 insertions(+) create mode 100644 src/Symfony/Component/Security/Http/Authentication/AuthenticationUtils.php diff --git a/src/Symfony/Bundle/SecurityBundle/Resources/config/security.xml b/src/Symfony/Bundle/SecurityBundle/Resources/config/security.xml index d90f3206dba0..65b5fbfd2dc6 100644 --- a/src/Symfony/Bundle/SecurityBundle/Resources/config/security.xml +++ b/src/Symfony/Bundle/SecurityBundle/Resources/config/security.xml @@ -47,6 +47,8 @@ Symfony\Component\Security\Core\Validator\Constraints\UserPasswordValidator Symfony\Component\Security\Core\Authorization\ExpressionLanguage + + Symfony\Component\Security\Http\Authentication\AuthenticationUtils @@ -84,6 +86,10 @@ + + + + diff --git a/src/Symfony/Component/Security/CHANGELOG.md b/src/Symfony/Component/Security/CHANGELOG.md index 9ab61d0bb4a1..cdc5c44d93b0 100644 --- a/src/Symfony/Component/Security/CHANGELOG.md +++ b/src/Symfony/Component/Security/CHANGELOG.md @@ -1,6 +1,11 @@ CHANGELOG ========= +2.6.0 +----- + + * added Symfony\Component\Security\Http\Authentication\AuthenticationUtils + 2.4.0 ----- diff --git a/src/Symfony/Component/Security/Http/Authentication/AuthenticationUtils.php b/src/Symfony/Component/Security/Http/Authentication/AuthenticationUtils.php new file mode 100644 index 000000000000..ed4b0a165b66 --- /dev/null +++ b/src/Symfony/Component/Security/Http/Authentication/AuthenticationUtils.php @@ -0,0 +1,87 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace Symfony\Component\Security\Http\Authentication; + +use Symfony\Component\HttpFoundation\RequestStack; +use Symfony\Component\Security\Core\Exception\AuthenticationException; +use Symfony\Component\Security\Core\SecurityContextInterface; +use Symfony\Component\HttpFoundation\Request; + +/** + * Extracts Security Errors from Request + * + * @author Boris Vujicic + */ +class AuthenticationUtils +{ + /** + * @var RequestStack + */ + private $requestStack; + + /** + * @param RequestStack $requestStack + */ + public function __construct(RequestStack $requestStack) + { + $this->requestStack = $requestStack; + } + + /** + * @param bool $clearSession + * @return null|AuthenticationException + */ + public function getLastAuthenticationError($clearSession = true) + { + $request = $this->getRequest(); + $session = $request->getSession(); + $authenticationException = null; + + if ($request->attributes->has(SecurityContextInterface::AUTHENTICATION_ERROR)) { + $authenticationException = $request->attributes->get(SecurityContextInterface::AUTHENTICATION_ERROR); + } elseif ($session !== null && $session->has(SecurityContextInterface::AUTHENTICATION_ERROR)) { + $authenticationException = $session->get(SecurityContextInterface::AUTHENTICATION_ERROR); + + if ($clearSession) { + $session->remove(SecurityContextInterface::AUTHENTICATION_ERROR); + } + + } + + return $authenticationException; + } + + /** + * @return string + */ + public function getLastUsername() + { + $session = $this->getRequest()->getSession(); + + return null === $session ? '' : $session->get(SecurityContextInterface::LAST_USERNAME); + } + + /** + * @return Request + * @throws \LogicException + */ + private function getRequest() + { + $request = $this->requestStack->getCurrentRequest(); + + if (null === $request) { + throw new \LogicException('Request should exist so it can be processed for error.'); + } + + return $request; + } +}