[SecurityBundle] error helper added symfony/symfony#11147

This commit is contained in:
Boris Vujicic 2014-07-05 16:07:05 +02:00 committed by Fabien Potencier
parent 8b54211471
commit 1722f60d42
3 changed files with 98 additions and 0 deletions

View File

@ -47,6 +47,8 @@
<parameter key="security.validator.user_password.class">Symfony\Component\Security\Core\Validator\Constraints\UserPasswordValidator</parameter> <parameter key="security.validator.user_password.class">Symfony\Component\Security\Core\Validator\Constraints\UserPasswordValidator</parameter>
<parameter key="security.expression_language.class">Symfony\Component\Security\Core\Authorization\ExpressionLanguage</parameter> <parameter key="security.expression_language.class">Symfony\Component\Security\Core\Authorization\ExpressionLanguage</parameter>
<parameter key="security.authentication_utils.class">Symfony\Component\Security\Http\Authentication\AuthenticationUtils</parameter>
</parameters> </parameters>
<services> <services>
@ -84,6 +86,10 @@
<service id="security.expression_language" class="%security.expression_language.class%" public="false" /> <service id="security.expression_language" class="%security.expression_language.class%" public="false" />
<service id="security.authentication_utils" class="%security.authentication_utils.class%">
<argument type="service" id="request_stack" />
</service>
<!-- Authorization related services --> <!-- Authorization related services -->
<service id="security.access.decision_manager" class="%security.access.decision_manager.class%" public="false"> <service id="security.access.decision_manager" class="%security.access.decision_manager.class%" public="false">
<argument type="collection"></argument> <argument type="collection"></argument>

View File

@ -1,6 +1,11 @@
CHANGELOG CHANGELOG
========= =========
2.6.0
-----
* added Symfony\Component\Security\Http\Authentication\AuthenticationUtils
2.4.0 2.4.0
----- -----

View File

@ -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;
}
}