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