Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feature #24337 Adding a shortcuts for the main security functionality…
… (weaverryan, javiereguiluz) This PR was squashed before being merged into the 3.4 branch (closes #24337). Discussion ---------- Adding a shortcuts for the main security functionality | Q | A | ------------- | --- | Branch? | 3.4 | Bug fix? | no | New feature? | yes | BC breaks? | no | Deprecations? | no | Tests pass? | yes | Fixed tickets | none | License | MIT | Doc PR | Big ol' TODO I'd like one class that I can inject (especially with autowiring) to get access to the User and `isGranted()` methods. This is *really* important... because to get the User currently, you need to type-hint `TokenStorageInterface`... and there are *two*! That's really bad DX! Questions: A) I hi-jacked the existing `Security` class... I wanted a simple class called Security B) I called the service `security.helper`... for lack of a better id. C) I did not make `Security` implement the 2 other interfaces (`TokenStorageInterface`, `AuthorizationCheckerInterface`... but I suppose we could?) Cheers! Commits ------- 0851189 Adding a shortcuts for the main security functionality
- Loading branch information
Showing
10 changed files
with
240 additions
and
3 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
34 changes: 34 additions & 0 deletions
34
src/Symfony/Bundle/SecurityBundle/Tests/Functional/SecurityTest.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,34 @@ | ||
<?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\Bundle\SecurityBundle\Tests\Functional; | ||
|
||
use Symfony\Component\Security\Core\Authentication\Token\UsernamePasswordToken; | ||
use Symfony\Component\Security\Core\User\User; | ||
|
||
class SecurityTest extends WebTestCase | ||
{ | ||
public function testServiceIsFunctional() | ||
{ | ||
$kernel = self::createKernel(array('test_case' => 'SecurityHelper', 'root_config' => 'config.yml')); | ||
$kernel->boot(); | ||
$container = $kernel->getContainer(); | ||
|
||
// put a token into the storage so the final calls can function | ||
$user = new User('foo', 'pass'); | ||
$token = new UsernamePasswordToken($user, '', 'provider', array('ROLE_USER')); | ||
$container->get('security.token_storage')->setToken($token); | ||
|
||
$security = $container->get('functional_test.security.helper'); | ||
$this->assertTrue($security->isGranted('ROLE_USER')); | ||
$this->assertSame($token, $security->getToken()); | ||
} | ||
} |
20 changes: 20 additions & 0 deletions
20
src/Symfony/Bundle/SecurityBundle/Tests/Functional/app/SecurityHelper/bundles.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,20 @@ | ||
<?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. | ||
*/ | ||
|
||
use Symfony\Bundle\TwigBundle\TwigBundle; | ||
use Symfony\Bundle\SecurityBundle\SecurityBundle; | ||
use Symfony\Bundle\FrameworkBundle\FrameworkBundle; | ||
|
||
return array( | ||
new FrameworkBundle(), | ||
new SecurityBundle(), | ||
new TwigBundle(), | ||
); |
18 changes: 18 additions & 0 deletions
18
src/Symfony/Bundle/SecurityBundle/Tests/Functional/app/SecurityHelper/config.yml
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,18 @@ | ||
imports: | ||
- { resource: ./../config/default.yml } | ||
|
||
services: | ||
# alias the service so we can access it in the tests | ||
functional_test.security.helper: | ||
alias: security.helper | ||
public: true | ||
|
||
security: | ||
providers: | ||
in_memory: | ||
memory: | ||
users: [] | ||
|
||
firewalls: | ||
default: | ||
anonymous: ~ |
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
97 changes: 97 additions & 0 deletions
97
src/Symfony/Component/Security/Core/Tests/SecurityTest.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,97 @@ | ||
<?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\Core\Tests; | ||
|
||
use PHPUnit\Framework\TestCase; | ||
use Psr\Container\ContainerInterface; | ||
use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface; | ||
use Symfony\Component\Security\Core\Authentication\Token\TokenInterface; | ||
use Symfony\Component\Security\Core\Authentication\Token\UsernamePasswordToken; | ||
use Symfony\Component\Security\Core\Authorization\AuthorizationCheckerInterface; | ||
use Symfony\Component\Security\Core\Security; | ||
use Symfony\Component\Security\Core\User\User; | ||
|
||
class SecurityTest extends TestCase | ||
{ | ||
public function testGetToken() | ||
{ | ||
$token = new UsernamePasswordToken('foo', 'bar', 'provider'); | ||
$tokenStorage = $this->getMockBuilder(TokenStorageInterface::class)->getMock(); | ||
|
||
$tokenStorage->expects($this->once()) | ||
->method('getToken') | ||
->will($this->returnValue($token)); | ||
|
||
$container = $this->createContainer('security.token_storage', $tokenStorage); | ||
|
||
$security = new Security($container); | ||
$this->assertSame($token, $security->getToken()); | ||
} | ||
|
||
/** | ||
* @dataProvider getUserTests | ||
*/ | ||
public function testGetUser($userInToken, $expectedUser) | ||
{ | ||
$token = $this->getMockBuilder(TokenInterface::class)->getMock(); | ||
$token->expects($this->any()) | ||
->method('getUser') | ||
->will($this->returnValue($userInToken)); | ||
$tokenStorage = $this->getMockBuilder(TokenStorageInterface::class)->getMock(); | ||
|
||
$tokenStorage->expects($this->once()) | ||
->method('getToken') | ||
->will($this->returnValue($token)); | ||
|
||
$container = $this->createContainer('security.token_storage', $tokenStorage); | ||
|
||
$security = new Security($container); | ||
$this->assertSame($expectedUser, $security->getUser()); | ||
} | ||
|
||
public function getUserTests() | ||
{ | ||
yield array(null, null); | ||
|
||
yield array('string_username', null); | ||
|
||
$user = new User('nice_user', 'foo'); | ||
yield array($user, $user); | ||
} | ||
|
||
public function testIsGranted() | ||
{ | ||
$authorizationChecker = $this->getMockBuilder(AuthorizationCheckerInterface::class)->getMock(); | ||
|
||
$authorizationChecker->expects($this->once()) | ||
->method('isGranted') | ||
->with('SOME_ATTRIBUTE', 'SOME_SUBJECT') | ||
->will($this->returnValue(true)); | ||
|
||
$container = $this->createContainer('security.authorization_checker', $authorizationChecker); | ||
|
||
$security = new Security($container); | ||
$this->assertTrue($security->isGranted('SOME_ATTRIBUTE', 'SOME_SUBJECT')); | ||
} | ||
|
||
private function createContainer($serviceId, $serviceObject) | ||
{ | ||
$container = $this->getMockBuilder(ContainerInterface::class)->getMock(); | ||
|
||
$container->expects($this->atLeastOnce()) | ||
->method('get') | ||
->with($serviceId) | ||
->will($this->returnValue($serviceObject)); | ||
|
||
return $container; | ||
} | ||
} |
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