diff --git a/src/Symfony/Bundle/FrameworkBundle/Controller/AbstractController.php b/src/Symfony/Bundle/FrameworkBundle/Controller/AbstractController.php index f6eff3f61c805..85220f1b568fc 100644 --- a/src/Symfony/Bundle/FrameworkBundle/Controller/AbstractController.php +++ b/src/Symfony/Bundle/FrameworkBundle/Controller/AbstractController.php @@ -239,7 +239,7 @@ protected function denyAccessUnlessGranted($attribute, $subject = null, string $ { if (!$this->isGranted($attribute, $subject)) { $exception = $this->createAccessDeniedException($message); - $exception->setAttributes($attribute); + $exception->setAttributes([$attribute]); $exception->setSubject($subject); throw $exception; diff --git a/src/Symfony/Bundle/FrameworkBundle/Tests/Controller/AbstractControllerTest.php b/src/Symfony/Bundle/FrameworkBundle/Tests/Controller/AbstractControllerTest.php index 9a5c5510ce14e..d8dc199d8ae4b 100644 --- a/src/Symfony/Bundle/FrameworkBundle/Tests/Controller/AbstractControllerTest.php +++ b/src/Symfony/Bundle/FrameworkBundle/Tests/Controller/AbstractControllerTest.php @@ -387,6 +387,40 @@ public function testdenyAccessUnlessGranted() $controller->denyAccessUnlessGranted('foo'); } + /** + * @dataProvider provideDenyAccessUnlessGrantedSetsAttributesAsArray + */ + public function testdenyAccessUnlessGrantedSetsAttributesAsArray($attribute, $exceptionAttributes) + { + $authorizationChecker = $this->createMock(AuthorizationCheckerInterface::class); + $authorizationChecker->method('isGranted')->willReturn(false); + + $container = new Container(); + $container->set('security.authorization_checker', $authorizationChecker); + + $controller = $this->createController(); + $controller->setContainer($container); + + try { + $controller->denyAccessUnlessGranted($attribute); + $this->fail('there was no exception to check'); + } catch (AccessDeniedException $e) { + $this->assertSame($exceptionAttributes, $e->getAttributes()); + } + } + + public static function provideDenyAccessUnlessGrantedSetsAttributesAsArray() + { + $obj = new \stdClass(); + $obj->foo = 'bar'; + + return [ + 'string attribute' => ['foo', ['foo']], + 'array attribute' => [[1, 3, 3, 7], [[1, 3, 3, 7]]], + 'object attribute' => [$obj, [$obj]], + ]; + } + public function testRenderViewTwig() { $twig = $this->createMock(Environment::class);