-
-
Notifications
You must be signed in to change notification settings - Fork 9.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added a SecurityUserValueResolver for controllers
- Loading branch information
Iltar van der Berg
committed
Jul 1, 2016
1 parent
2e811cb
commit d341889
Showing
13 changed files
with
206 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
57 changes: 57 additions & 0 deletions
57
src/Symfony/Bundle/SecurityBundle/SecurityUserValueResolver.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Symfony package. | ||
* | ||
* (c) Fabien Potencier <[email protected]> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Symfony\Bundle\SecurityBundle; | ||
|
||
use Symfony\Component\HttpFoundation\Request; | ||
use Symfony\Component\HttpKernel\Controller\ArgumentValueResolverInterface; | ||
use Symfony\Component\HttpKernel\ControllerMetadata\ArgumentMetadata; | ||
use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface; | ||
use Symfony\Component\Security\Core\Authentication\Token\TokenInterface; | ||
use Symfony\Component\Security\Core\User\UserInterface; | ||
|
||
/** | ||
* Supports the argument type of {@see UserInterface}. | ||
* | ||
* @author Iltar van der Berg <[email protected]> | ||
*/ | ||
final class SecurityUserValueResolver implements ArgumentValueResolverInterface | ||
{ | ||
private $tokenStorage; | ||
|
||
public function __construct(TokenStorageInterface $tokenStorage) | ||
{ | ||
$this->tokenStorage = $tokenStorage; | ||
} | ||
|
||
public function supports(Request $request, ArgumentMetadata $argument) | ||
{ | ||
// only security user implementations are supported | ||
if (UserInterface::class !== $argument->getType()) { | ||
return false; | ||
} | ||
|
||
$token = $this->tokenStorage->getToken(); | ||
if (!$token instanceof TokenInterface) { | ||
return false; | ||
} | ||
|
||
$user = $token->getUser(); | ||
|
||
// in case it's not an object we cannot do anything with it; E.g. "anon." | ||
return $user instanceof UserInterface; | ||
} | ||
|
||
public function resolve(Request $request, ArgumentMetadata $argument) | ||
{ | ||
yield $this->tokenStorage->getToken()->getUser(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 1 addition & 1 deletion
2
...undle/Tests/Functional/Bundle/FormLoginBundle/Resources/views/Login/after_login.html.twig
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
100 changes: 100 additions & 0 deletions
100
src/Symfony/Bundle/SecurityBundle/Tests/SecurityUserValueResolverTest.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,100 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Symfony package. | ||
* | ||
* (c) Fabien Potencier <[email protected]> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Symfony\Bundle\SecurityBundle\Tests; | ||
|
||
use Symfony\Bundle\SecurityBundle\SecurityUserValueResolver; | ||
use Symfony\Component\HttpFoundation\Request; | ||
use Symfony\Component\HttpKernel\Controller\ArgumentResolver; | ||
use Symfony\Component\HttpKernel\Controller\ArgumentResolver\DefaultValueResolver; | ||
use Symfony\Component\HttpKernel\ControllerMetadata\ArgumentMetadata; | ||
use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorage; | ||
use Symfony\Component\Security\Core\Authentication\Token\TokenInterface; | ||
use Symfony\Component\Security\Core\User\UserInterface; | ||
|
||
class SecurityUserValueResolverTest extends \PHPUnit_Framework_TestCase | ||
{ | ||
public function testResolveNoToken() | ||
{ | ||
$tokenStorage = new TokenStorage(); | ||
$resolver = new SecurityUserValueResolver($tokenStorage); | ||
$metadata = new ArgumentMetadata('foo', UserInterface::class, false, false, null); | ||
|
||
$this->assertFalse($resolver->supports(Request::create('/'), $metadata)); | ||
} | ||
|
||
public function testResolveNoUser() | ||
{ | ||
$mock = $this->getMock(UserInterface::class); | ||
$token = $this->getMock(TokenInterface::class); | ||
$tokenStorage = new TokenStorage(); | ||
$tokenStorage->setToken($token); | ||
|
||
$resolver = new SecurityUserValueResolver($tokenStorage); | ||
$metadata = new ArgumentMetadata('foo', get_class($mock), false, false, null); | ||
|
||
$this->assertFalse($resolver->supports(Request::create('/'), $metadata)); | ||
} | ||
|
||
public function testResolveWrongType() | ||
{ | ||
$tokenStorage = new TokenStorage(); | ||
$resolver = new SecurityUserValueResolver($tokenStorage); | ||
$metadata = new ArgumentMetadata('foo', null, false, false, null); | ||
|
||
$this->assertFalse($resolver->supports(Request::create('/'), $metadata)); | ||
} | ||
|
||
public function testResolve() | ||
{ | ||
$user = $this->getMock(UserInterface::class); | ||
$token = $this->getMock(TokenInterface::class); | ||
$token->expects($this->any())->method('getUser')->willReturn($user); | ||
$tokenStorage = new TokenStorage(); | ||
$tokenStorage->setToken($token); | ||
|
||
$resolver = new SecurityUserValueResolver($tokenStorage); | ||
$metadata = new ArgumentMetadata('foo', UserInterface::class, false, false, null); | ||
|
||
$this->assertTrue($resolver->supports(Request::create('/'), $metadata)); | ||
$this->assertSame(array($user), iterator_to_array($resolver->resolve(Request::create('/'), $metadata))); | ||
} | ||
|
||
public function testIntegration() | ||
{ | ||
$user = $this->getMock(UserInterface::class); | ||
$token = $this->getMock(TokenInterface::class); | ||
$token->expects($this->any())->method('getUser')->willReturn($user); | ||
$tokenStorage = new TokenStorage(); | ||
$tokenStorage->setToken($token); | ||
|
||
$argumentResolver = new ArgumentResolver(null, array(new SecurityUserValueResolver($tokenStorage))); | ||
$this->assertSame(array($user), $argumentResolver->getArguments(Request::create('/'), function (UserInterface $user) {})); | ||
} | ||
|
||
public function testIntegrationNoUser() | ||
{ | ||
$token = $this->getMock(TokenInterface::class); | ||
$tokenStorage = new TokenStorage(); | ||
$tokenStorage->setToken($token); | ||
|
||
$argumentResolver = new ArgumentResolver(null, array(new SecurityUserValueResolver($tokenStorage), new DefaultValueResolver())); | ||
$this->assertSame(array(null), $argumentResolver->getArguments(Request::create('/'), function (UserInterface $user = null) {})); | ||
} | ||
} | ||
|
||
abstract class DummyUser implements UserInterface | ||
{ | ||
} | ||
|
||
abstract class DummySubUser extends DummyUser | ||
{ | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters