diff options
author | Fabien Potencier <fabien.potencier@gmail.com> | 2014-08-31 06:19:40 +0200 |
---|---|---|
committer | Fabien Potencier <fabien.potencier@gmail.com> | 2014-08-31 06:19:40 +0200 |
commit | 8637c7dac164357effff563e0965a62c1e0aaa64 (patch) | |
tree | f299fc9906bd74940dd5f7c6d730d97017b8fe58 /Http | |
parent | 518f866e608bfa5198c5cc79491cec0dfd66363e (diff) | |
parent | 5c9bc9d8df6e9cc4321221c0ba77907779e99d1b (diff) | |
download | symfony-security-8637c7dac164357effff563e0965a62c1e0aaa64.zip symfony-security-8637c7dac164357effff563e0965a62c1e0aaa64.tar.gz symfony-security-8637c7dac164357effff563e0965a62c1e0aaa64.tar.bz2 |
feature #11324 [SecurityBundle] error helper added symfony/symfony#11147 (i3or1s)
This PR was squashed before being merged into the 2.6-dev branch (closes #11324).
Discussion
----------
[SecurityBundle] error helper added symfony/symfony#11147
Added helper that extracts last authentication error and username.
| Q | A
| ------------- | ---
| Bug fix? | no
| New feature? | yes
| BC breaks? | no
| Deprecations? | no
| Tests pass? | yes
| Fixed tickets | [ #11147 ]
| License | MIT
| Doc PR | symfony/symfony-docs#3996
Commits
-------
1722f60 [SecurityBundle] error helper added symfony/symfony#11147
Diffstat (limited to 'Http')
-rw-r--r-- | Http/Authentication/AuthenticationUtils.php | 87 |
1 files changed, 87 insertions, 0 deletions
diff --git a/Http/Authentication/AuthenticationUtils.php b/Http/Authentication/AuthenticationUtils.php new file mode 100644 index 0000000..ed4b0a1 --- /dev/null +++ b/Http/Authentication/AuthenticationUtils.php @@ -0,0 +1,87 @@ +<?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\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 <boris.vujicic@gmail.com> + */ +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; + } +} |