forked from symfony/symfony
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[Security][SecurityBundle] User authorization checker
- Loading branch information
1 parent
0c89120
commit e5ae6e2
Showing
13 changed files
with
308 additions
and
1 deletion.
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
21 changes: 21 additions & 0 deletions
21
src/Symfony/Component/Security/Core/Authentication/Token/OfflineTokenInterface.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,21 @@ | ||
<?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\Authentication\Token; | ||
|
||
/** | ||
* Interface used for marking tokens that do not represent the currently logged-in user. | ||
* | ||
* @author Nate Wiebe <nate@northern.co> | ||
*/ | ||
interface OfflineTokenInterface extends TokenInterface | ||
{ | ||
} |
31 changes: 31 additions & 0 deletions
31
src/Symfony/Component/Security/Core/Authentication/Token/UserAuthorizationCheckerToken.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,31 @@ | ||
<?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\Authentication\Token; | ||
|
||
use Symfony\Component\Security\Core\User\UserInterface; | ||
|
||
/** | ||
* UserAuthorizationCheckerToken implements a token used for checking authorization. | ||
* | ||
* @author Nate Wiebe <nate@northern.co> | ||
* | ||
* @internal | ||
*/ | ||
final class UserAuthorizationCheckerToken extends AbstractToken implements OfflineTokenInterface | ||
{ | ||
public function __construct(UserInterface $user) | ||
{ | ||
parent::__construct($user->getRoles()); | ||
|
||
$this->setUser($user); | ||
} | ||
} |
31 changes: 31 additions & 0 deletions
31
src/Symfony/Component/Security/Core/Authorization/UserAuthorizationChecker.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,31 @@ | ||
<?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\Authorization; | ||
|
||
use Symfony\Component\Security\Core\Authentication\Token\UserAuthorizationCheckerToken; | ||
use Symfony\Component\Security\Core\User\UserInterface; | ||
|
||
/** | ||
* @author Nate Wiebe <nate@northern.co> | ||
*/ | ||
final class UserAuthorizationChecker implements UserAuthorizationCheckerInterface | ||
{ | ||
public function __construct( | ||
private readonly AccessDecisionManagerInterface $accessDecisionManager, | ||
) { | ||
} | ||
|
||
public function userIsGranted(UserInterface $user, mixed $attribute, mixed $subject = null): bool | ||
{ | ||
return $this->accessDecisionManager->decide(new UserAuthorizationCheckerToken($user), [$attribute], $subject); | ||
} | ||
} |
29 changes: 29 additions & 0 deletions
29
src/Symfony/Component/Security/Core/Authorization/UserAuthorizationCheckerInterface.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,29 @@ | ||
<?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\Authorization; | ||
|
||
use Symfony\Component\Security\Core\User\UserInterface; | ||
|
||
/** | ||
* Interface is used to check user authorization without a session. | ||
* | ||
* @author Nate Wiebe <nate@northern.co> | ||
*/ | ||
interface UserAuthorizationCheckerInterface | ||
{ | ||
/** | ||
* Checks if the attribute is granted against the user and optionally supplied subject. | ||
* | ||
* @param mixed $attribute A single attribute to vote on (can be of any type, string and instance of Expression are supported by the core) | ||
*/ | ||
public function userIsGranted(UserInterface $user, mixed $attribute, mixed $subject = null): bool; | ||
} |
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
26 changes: 26 additions & 0 deletions
26
.../Component/Security/Core/Tests/Authentication/Token/UserAuthorizationCheckerTokenTest.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,26 @@ | ||
<?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\Authentication\Token; | ||
|
||
use PHPUnit\Framework\TestCase; | ||
use Symfony\Component\Security\Core\Authentication\Token\UserAuthorizationCheckerToken; | ||
use Symfony\Component\Security\Core\User\InMemoryUser; | ||
|
||
class UserAuthorizationCheckerTokenTest extends TestCase | ||
{ | ||
public function testConstructor() | ||
{ | ||
$token = new UserAuthorizationCheckerToken($user = new InMemoryUser('foo', 'bar', ['ROLE_FOO'])); | ||
$this->assertSame(['ROLE_FOO'], $token->getRoleNames()); | ||
$this->assertSame($user, $token->getUser()); | ||
} | ||
} |
70 changes: 70 additions & 0 deletions
70
src/Symfony/Component/Security/Core/Tests/Authorization/UserAuthorizationCheckerTest.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,70 @@ | ||
<?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\Authorization; | ||
|
||
use PHPUnit\Framework\MockObject\MockObject; | ||
use PHPUnit\Framework\TestCase; | ||
use Symfony\Component\Security\Core\Authentication\Token\UserAuthorizationCheckerToken; | ||
use Symfony\Component\Security\Core\Authorization\AccessDecisionManagerInterface; | ||
use Symfony\Component\Security\Core\Authorization\UserAuthorizationChecker; | ||
use Symfony\Component\Security\Core\User\InMemoryUser; | ||
|
||
class UserAuthorizationCheckerTest extends TestCase | ||
{ | ||
private AccessDecisionManagerInterface&MockObject $accessDecisionManager; | ||
private UserAuthorizationChecker $authorizationChecker; | ||
|
||
protected function setUp(): void | ||
{ | ||
$this->accessDecisionManager = $this->createMock(AccessDecisionManagerInterface::class); | ||
|
||
$this->authorizationChecker = new UserAuthorizationChecker($this->accessDecisionManager); | ||
} | ||
|
||
/** | ||
* @dataProvider isGrantedProvider | ||
*/ | ||
public function testIsGranted(bool $decide, array $roles) | ||
{ | ||
$user = new InMemoryUser('username', 'password', $roles); | ||
|
||
$this->accessDecisionManager | ||
->expects($this->once()) | ||
->method('decide') | ||
->with($this->callback(fn (UserAuthorizationCheckerToken $token): bool => $user === $token->getUser()), $this->identicalTo(['ROLE_FOO'])) | ||
->willReturn($decide); | ||
|
||
$this->assertSame($decide, $this->authorizationChecker->userIsGranted($user, 'ROLE_FOO')); | ||
} | ||
|
||
public static function isGrantedProvider(): array | ||
{ | ||
return [ | ||
[false, ['ROLE_USER']], | ||
[true, ['ROLE_USER', 'ROLE_FOO']], | ||
]; | ||
} | ||
|
||
public function testIsGrantedWithObjectAttribute() | ||
{ | ||
$attribute = new \stdClass(); | ||
|
||
$token = new UserAuthorizationCheckerToken(new InMemoryUser('username', 'password', ['ROLE_USER'])); | ||
|
||
$this->accessDecisionManager | ||
->expects($this->once()) | ||
->method('decide') | ||
->with($this->isInstanceOf($token::class), $this->identicalTo([$attribute])) | ||
->willReturn(true); | ||
$this->assertTrue($this->authorizationChecker->userIsGranted($token->getUser(), $attribute)); | ||
} | ||
} |
Oops, something went wrong.