-
-
Notifications
You must be signed in to change notification settings - Fork 9.6k
[Security][SecurityBundle] Move the Security
helper to SecurityBundle
#46094
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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 |
---|---|---|
@@ -1,6 +1,11 @@ | ||
CHANGELOG | ||
========= | ||
|
||
6.2 | ||
--- | ||
|
||
* Add the `Security` helper class | ||
|
||
6.1 | ||
--- | ||
|
||
|
This file contains hidden or 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 hidden or 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,28 @@ | ||
<?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\Security; | ||
|
||
use Psr\Container\ContainerInterface; | ||
use Symfony\Component\Security\Core\Security as LegacySecurity; | ||
|
||
/** | ||
* Helper class for commonly-needed security tasks. | ||
* | ||
* @final | ||
*/ | ||
class Security extends LegacySecurity | ||
{ | ||
public function __construct(ContainerInterface $container) | ||
{ | ||
parent::__construct($container, false); | ||
} | ||
} |
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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
89 changes: 89 additions & 0 deletions
89
src/Symfony/Bundle/SecurityBundle/Tests/Security/SecurityTest.php
This file contains hidden or 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,89 @@ | ||
<?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\Security; | ||
|
||
use PHPUnit\Framework\TestCase; | ||
use Psr\Container\ContainerInterface; | ||
use Symfony\Bundle\SecurityBundle\Security\Security; | ||
use Symfony\Component\DependencyInjection\ServiceLocator; | ||
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\User\InMemoryUser; | ||
|
||
class SecurityTest extends TestCase | ||
{ | ||
public function testGetToken() | ||
{ | ||
$token = new UsernamePasswordToken(new InMemoryUser('foo', 'bar'), 'provider'); | ||
$tokenStorage = $this->createMock(TokenStorageInterface::class); | ||
|
||
$tokenStorage->expects($this->once()) | ||
->method('getToken') | ||
->willReturn($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->createMock(TokenInterface::class); | ||
$token->expects($this->any()) | ||
->method('getUser') | ||
->willReturn($userInToken); | ||
$tokenStorage = $this->createMock(TokenStorageInterface::class); | ||
|
||
$tokenStorage->expects($this->once()) | ||
->method('getToken') | ||
->willReturn($token); | ||
|
||
$container = $this->createContainer('security.token_storage', $tokenStorage); | ||
|
||
$security = new Security($container); | ||
$this->assertSame($expectedUser, $security->getUser()); | ||
} | ||
|
||
public function getUserTests() | ||
{ | ||
yield [null, null]; | ||
|
||
$user = new InMemoryUser('nice_user', 'foo'); | ||
yield [$user, $user]; | ||
} | ||
|
||
public function testIsGranted() | ||
{ | ||
$authorizationChecker = $this->createMock(AuthorizationCheckerInterface::class); | ||
|
||
$authorizationChecker->expects($this->once()) | ||
->method('isGranted') | ||
->with('SOME_ATTRIBUTE', 'SOME_SUBJECT') | ||
->willReturn(true); | ||
|
||
$container = $this->createContainer('security.authorization_checker', $authorizationChecker); | ||
|
||
$security = new Security($container); | ||
$this->assertTrue($security->isGranted('SOME_ATTRIBUTE', 'SOME_SUBJECT')); | ||
} | ||
|
||
private function createContainer(string $serviceId, object $serviceObject): ContainerInterface | ||
{ | ||
return new ServiceLocator([$serviceId => fn () => $serviceObject]); | ||
} | ||
} |
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.