From 0532d495f456d0af982ce2cd227c88376ad93007 Mon Sep 17 00:00:00 2001 From: Tobias Nyholm Date: Sun, 19 Apr 2015 13:01:40 +0200 Subject: [PATCH] [DX] Added CurrentUserProvider service --- .../FrameworkBundle/Controller/Controller.php | 16 +--- .../Tests/Controller/ControllerTest.php | 73 +++++-------------- .../Resources/config/security.xml | 4 + .../Tests/User/CurrentUserProviderTest.php | 63 ++++++++++++++++ .../Core/User/CurrentUserProvider.php | 49 +++++++++++++ 5 files changed, 140 insertions(+), 65 deletions(-) create mode 100644 src/Symfony/Component/Security/Core/Tests/User/CurrentUserProviderTest.php create mode 100644 src/Symfony/Component/Security/Core/User/CurrentUserProvider.php diff --git a/src/Symfony/Bundle/FrameworkBundle/Controller/Controller.php b/src/Symfony/Bundle/FrameworkBundle/Controller/Controller.php index b78d4759272a9..c0ba9ed8c1947 100644 --- a/src/Symfony/Bundle/FrameworkBundle/Controller/Controller.php +++ b/src/Symfony/Bundle/FrameworkBundle/Controller/Controller.php @@ -120,6 +120,7 @@ protected function addFlash($type, $message) * @param mixed $object The object * * @throws \LogicException + * * @return bool */ protected function isGranted($attributes, $object = null) @@ -305,20 +306,11 @@ public function getDoctrine() */ public function getUser() { - if (!$this->container->has('security.token_storage')) { + if (!$this->container->has('security.current_user_provider')) { throw new \LogicException('The SecurityBundle is not registered in your application.'); } - if (null === $token = $this->container->get('security.token_storage')->getToken()) { - return; - } - - if (!is_object($user = $token->getUser())) { - // e.g. anonymous authentication - return; - } - - return $user; + return $this->container->get('security.current_user_provider')->getUser(); } /** @@ -362,7 +354,7 @@ protected function getParameter($name) } /** - * Checks the validity of a CSRF token + * Checks the validity of a CSRF token. * * @param string $id The id used when generating the token * @param string $token The actual token sent with the request that should be validated diff --git a/src/Symfony/Bundle/FrameworkBundle/Tests/Controller/ControllerTest.php b/src/Symfony/Bundle/FrameworkBundle/Tests/Controller/ControllerTest.php index 650316860cf6b..564f7b5a8f250 100644 --- a/src/Symfony/Bundle/FrameworkBundle/Tests/Controller/ControllerTest.php +++ b/src/Symfony/Bundle/FrameworkBundle/Tests/Controller/ControllerTest.php @@ -13,13 +13,9 @@ use Symfony\Bundle\FrameworkBundle\Tests\TestCase; use Symfony\Bundle\FrameworkBundle\Controller\Controller; -use Symfony\Component\DependencyInjection\ContainerInterface; use Symfony\Component\HttpFoundation\Request; use Symfony\Component\HttpFoundation\RequestStack; use Symfony\Component\HttpFoundation\Response; -use Symfony\Component\Security\Core\Authentication\Token\AnonymousToken; -use Symfony\Component\Security\Core\Authentication\Token\UsernamePasswordToken; -use Symfony\Component\Security\Core\User\User; class ControllerTest extends TestCase { @@ -50,31 +46,30 @@ public function testForward() public function testGetUser() { - $user = new User('user', 'pass'); - $token = new UsernamePasswordToken($user, 'pass', 'default', array('ROLE_USER')); - - $controller = new TestController(); - $controller->setContainer($this->getContainerWithTokenStorage($token)); - - $this->assertSame($controller->getUser(), $user); - } - - public function testGetUserAnonymousUserConvertedToNull() - { - $token = new AnonymousToken('default', 'anon.'); + $currentUserProvider = $this->getMockBuilder('Symfony\Component\Security\Core\User\CurrentUserProvider') + ->disableOriginalConstructor() + ->getMock(); + $currentUserProvider + ->expects($this->once()) + ->method('getUser'); - $controller = new TestController(); - $controller->setContainer($this->getContainerWithTokenStorage($token)); + $container = $this->getMock('Symfony\Component\DependencyInjection\ContainerInterface'); + $container + ->expects($this->once()) + ->method('has') + ->with('security.current_user_provider') + ->will($this->returnValue(true)); - $this->assertNull($controller->getUser()); - } + $container + ->expects($this->once()) + ->method('get') + ->with('security.current_user_provider') + ->will($this->returnValue($currentUserProvider)); - public function testGetUserWithEmptyTokenStorage() - { $controller = new TestController(); - $controller->setContainer($this->getContainerWithTokenStorage(null)); + $controller->setContainer($container); - $this->assertNull($controller->getUser()); + $controller->getUser(); } /** @@ -87,7 +82,7 @@ public function testGetUserWithEmptyContainer() $container ->expects($this->once()) ->method('has') - ->with('security.token_storage') + ->with('security.current_user_provider') ->will($this->returnValue(false)); $controller = new TestController(); @@ -95,34 +90,6 @@ public function testGetUserWithEmptyContainer() $controller->getUser(); } - - /** - * @param $token - * @return ContainerInterface - */ - private function getContainerWithTokenStorage($token = null) - { - $tokenStorage = $this->getMock('Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorage'); - $tokenStorage - ->expects($this->once()) - ->method('getToken') - ->will($this->returnValue($token)); - - $container = $this->getMock('Symfony\Component\DependencyInjection\ContainerInterface'); - $container - ->expects($this->once()) - ->method('has') - ->with('security.token_storage') - ->will($this->returnValue(true)); - - $container - ->expects($this->once()) - ->method('get') - ->with('security.token_storage') - ->will($this->returnValue($tokenStorage)); - - return $container; - } } class TestController extends Controller diff --git a/src/Symfony/Bundle/SecurityBundle/Resources/config/security.xml b/src/Symfony/Bundle/SecurityBundle/Resources/config/security.xml index b7c1407c1cc56..75fc5decd5cec 100644 --- a/src/Symfony/Bundle/SecurityBundle/Resources/config/security.xml +++ b/src/Symfony/Bundle/SecurityBundle/Resources/config/security.xml @@ -102,6 +102,10 @@ + + + + diff --git a/src/Symfony/Component/Security/Core/Tests/User/CurrentUserProviderTest.php b/src/Symfony/Component/Security/Core/Tests/User/CurrentUserProviderTest.php new file mode 100644 index 0000000000000..b46ea66e67321 --- /dev/null +++ b/src/Symfony/Component/Security/Core/Tests/User/CurrentUserProviderTest.php @@ -0,0 +1,63 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace Symfony\Component\Security\Core\Tests\User; + +use Symfony\Component\Security\Core\Authentication\Token\AnonymousToken; +use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface; +use Symfony\Component\Security\Core\Authentication\Token\UsernamePasswordToken; +use Symfony\Component\Security\Core\User\CurrentUserProvider; +use Symfony\Component\Security\Core\User\User; + +class CurrentUserProviderTest extends \PHPUnit_Framework_TestCase +{ + public function testGetUser() + { + $user = new User('user', 'pass'); + $token = new UsernamePasswordToken($user, 'pass', 'default', array('ROLE_USER')); + + $service = new CurrentUserProvider($this->getTokenStorage($token)); + + $this->assertSame($service->getUser(), $user); + } + + public function testGetUserAnonymousUserConvertedToNull() + { + $token = new AnonymousToken('default', 'anon.'); + + $service = new CurrentUserProvider($this->getTokenStorage($token)); + + $this->assertNull($service->getUser()); + } + + public function testGetUserWithEmptyTokenStorage() + { + $service = new CurrentUserProvider($this->getTokenStorage(null)); + + $this->assertNull($service->getUser()); + } + + /** + * @param $token + * + * @return TokenStorageInterface + */ + private function getTokenStorage($token = null) + { + $tokenStorage = $this->getMock('Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorage'); + $tokenStorage + ->expects($this->once()) + ->method('getToken') + ->will($this->returnValue($token)); + + return $tokenStorage; + } +} diff --git a/src/Symfony/Component/Security/Core/User/CurrentUserProvider.php b/src/Symfony/Component/Security/Core/User/CurrentUserProvider.php new file mode 100644 index 0000000000000..6b4f695f8ddad --- /dev/null +++ b/src/Symfony/Component/Security/Core/User/CurrentUserProvider.php @@ -0,0 +1,49 @@ + + */ +class CurrentUserProvider +{ + /** + * @var TokenStorageInterface tokenStorage + */ + private $tokenStorage; + + /** + * @param TokenStorageInterface $tokenStorage + */ + public function __construct(TokenStorageInterface $tokenStorage) + { + $this->tokenStorage = $tokenStorage; + } + + /** + * Get a user from the Security Token Storage. + * + * @return mixed + * + * @see TokenInterface::getUser() + */ + public function getUser() + { + if (null === $token = $this->tokenStorage->getToken()) { + return; + } + + if (!is_object($user = $token->getUser())) { + // e.g. anonymous authentication + return; + } + + return $user; + } +} pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy