|
13 | 13 |
|
14 | 14 | use PHPUnit\Framework\TestCase;
|
15 | 15 | use Symfony\Component\ExpressionLanguage\Expression;
|
16 |
| -use Symfony\Component\ExpressionLanguage\ExpressionLanguage; |
17 | 16 | use Symfony\Component\HttpFoundation\Request;
|
18 | 17 | use Symfony\Component\HttpKernel\Event\ControllerArgumentsEvent;
|
19 | 18 | use Symfony\Component\HttpKernel\Exception\HttpException;
|
20 | 19 | use Symfony\Component\HttpKernel\HttpKernelInterface;
|
| 20 | +use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorage; |
| 21 | +use Symfony\Component\Security\Core\Authentication\Token\TokenInterface; |
| 22 | +use Symfony\Component\Security\Core\Authorization\AccessDecisionManager; |
| 23 | +use Symfony\Component\Security\Core\Authorization\AuthorizationChecker; |
21 | 24 | use Symfony\Component\Security\Core\Authorization\AuthorizationCheckerInterface;
|
| 25 | +use Symfony\Component\Security\Core\Authorization\ExpressionLanguage; |
| 26 | +use Symfony\Component\Security\Core\Authorization\Voter\ExpressionVoter; |
| 27 | +use Symfony\Component\Security\Core\Authorization\Voter\RoleVoter; |
| 28 | +use Symfony\Component\Security\Core\Authorization\Voter\Vote; |
| 29 | +use Symfony\Component\Security\Core\Authorization\Voter\Voter; |
22 | 30 | use Symfony\Component\Security\Core\Exception\AccessDeniedException;
|
23 | 31 | use Symfony\Component\Security\Http\EventListener\IsGrantedAttributeListener;
|
24 | 32 | use Symfony\Component\Security\Http\Tests\Fixtures\IsGrantedAttributeController;
|
@@ -213,10 +221,23 @@ public function testExceptionWhenMissingSubjectAttribute()
|
213 | 221 | */
|
214 | 222 | public function testAccessDeniedMessages(string|Expression $attribute, string|array|null $subject, string $method, int $numOfArguments, string $expectedMessage)
|
215 | 223 | {
|
216 |
| - $authChecker = $this->createMock(AuthorizationCheckerInterface::class); |
217 |
| - $authChecker->expects($this->any()) |
218 |
| - ->method('isGranted') |
219 |
| - ->willReturn(false); |
| 224 | + $authChecker = new AuthorizationChecker(new TokenStorage(), new AccessDecisionManager((function () use (&$authChecker) { |
| 225 | + yield new ExpressionVoter(new ExpressionLanguage(), null, $authChecker); |
| 226 | + yield new RoleVoter(); |
| 227 | + yield new class() extends Voter { |
| 228 | + protected function supports(string $attribute, mixed $subject): bool |
| 229 | + { |
| 230 | + return 'POST_VIEW' === $attribute; |
| 231 | + } |
| 232 | + |
| 233 | + protected function voteOnAttribute(string $attribute, mixed $subject, TokenInterface $token, ?Vote $vote = null): bool |
| 234 | + { |
| 235 | + $vote->reasons[] = 'Because I can 😈.'; |
| 236 | + |
| 237 | + return false; |
| 238 | + } |
| 239 | + }; |
| 240 | + })())); |
220 | 241 |
|
221 | 242 | $expressionLanguage = $this->createMock(ExpressionLanguage::class);
|
222 | 243 | $expressionLanguage->expects($this->any())
|
@@ -252,12 +273,12 @@ public function testAccessDeniedMessages(string|Expression $attribute, string|ar
|
252 | 273 |
|
253 | 274 | public static function getAccessDeniedMessageTests()
|
254 | 275 | {
|
255 |
| - yield ['ROLE_ADMIN', null, 'admin', 0, 'Access Denied by #[IsGranted("ROLE_ADMIN")] on controller']; |
256 |
| - yield ['ROLE_ADMIN', 'bar', 'withSubject', 2, 'Access Denied by #[IsGranted("ROLE_ADMIN", "arg2Name")] on controller']; |
257 |
| - yield ['ROLE_ADMIN', ['arg1Name' => 'bar', 'arg2Name' => 'bar'], 'withSubjectArray', 2, 'Access Denied by #[IsGranted("ROLE_ADMIN", ["arg1Name", "arg2Name"])] on controller']; |
258 |
| - yield [new Expression('"ROLE_ADMIN" in role_names or is_granted("POST_VIEW", subject)'), 'bar', 'withExpressionInAttribute', 1, 'Access Denied by #[IsGranted(new Expression(""ROLE_ADMIN" in role_names or is_granted("POST_VIEW", subject)"), "post")] on controller']; |
259 |
| - yield [new Expression('user === subject'), 'bar', 'withExpressionInSubject', 1, 'Access Denied by #[IsGranted(new Expression("user === subject"), new Expression("args["post"].getAuthor()"))] on controller']; |
260 |
| - yield [new Expression('user === subject["author"]'), ['author' => 'bar', 'alias' => 'bar'], 'withNestedExpressionInSubject', 2, 'Access Denied by #[IsGranted(new Expression("user === subject["author"]"), ["author" => new Expression("args["post"].getAuthor()"), "alias" => "arg2Name"])] on controller']; |
| 276 | + yield ['ROLE_ADMIN', null, 'admin', 0, 'Access Denied. The user doesn\'t have ROLE_ADMIN.']; |
| 277 | + yield ['ROLE_ADMIN', 'bar', 'withSubject', 2, 'Access Denied. The user doesn\'t have ROLE_ADMIN.']; |
| 278 | + yield ['ROLE_ADMIN', ['arg1Name' => 'bar', 'arg2Name' => 'bar'], 'withSubjectArray', 2, 'Access Denied. The user doesn\'t have ROLE_ADMIN.']; |
| 279 | + yield [new Expression('"ROLE_ADMIN" in role_names or is_granted("POST_VIEW", subject)'), 'bar', 'withExpressionInAttribute', 1, 'Access Denied. Because I can 😈. Expression ("ROLE_ADMIN" in role_names or is_granted("POST_VIEW", subject)) is false.']; |
| 280 | + yield [new Expression('user === subject'), 'bar', 'withExpressionInSubject', 1, 'Access Denied. Expression (user === subject) is false.']; |
| 281 | + yield [new Expression('user === subject["author"]'), ['author' => 'bar', 'alias' => 'bar'], 'withNestedExpressionInSubject', 2, 'Access Denied. Expression (user === subject["author"]) is false.']; |
261 | 282 | }
|
262 | 283 |
|
263 | 284 | public function testNotFoundHttpException()
|
|
0 commit comments