diff --git a/EventListener/IsGrantedListener.php b/EventListener/IsGrantedListener.php index 419e9d80..7cb8c461 100644 --- a/EventListener/IsGrantedListener.php +++ b/EventListener/IsGrantedListener.php @@ -97,6 +97,7 @@ private function getIsGrantedString(IsGranted $isGranted) $attributes = array_map(function ($attribute) { return sprintf('"%s"', $attribute); }, (array) $isGranted->getAttributes()); + if (1 === \count($attributes)) { $argsString = reset($attributes); } else { @@ -104,7 +105,15 @@ private function getIsGrantedString(IsGranted $isGranted) } if (null !== $isGranted->getSubject()) { - $argsString = sprintf('%s, %s', $argsString, $isGranted->getSubject()); + $subjects = array_map(function ($subject) { + return sprintf('%s', $subject); + }, (array) $isGranted->getSubject()); + + if (1 === \count($subjects)) { + $argsString = sprintf('%s, %s', $argsString, reset($subjects)); + } else { + $argsString = sprintf('%s, [%s]', $argsString, implode(', ', $subjects)); + } } return $argsString; diff --git a/Tests/EventListener/IsGrantedListenerTest.php b/Tests/EventListener/IsGrantedListenerTest.php index d41e4fd3..9a0fc274 100644 --- a/Tests/EventListener/IsGrantedListenerTest.php +++ b/Tests/EventListener/IsGrantedListenerTest.php @@ -125,7 +125,9 @@ public function testAccessDeniedMessages(array $attributes, $subject, $expectedM // avoid the error of the subject not being found in the request attributes $arguments = []; if (null !== $subject) { - $arguments[$subject] = 'bar'; + foreach ((array) $subject as $value) { + $arguments[$value] = 'bar'; + } } $listener = new IsGrantedListener($this->createArgumentNameConverter($arguments), $authChecker); @@ -146,6 +148,8 @@ public function getAccessDeniedMessageTests() yield [['ROLE_ADMIN'], null, 'Access Denied by controller annotation @IsGranted("ROLE_ADMIN")']; yield [['ROLE_ADMIN', 'ROLE_USER'], null, 'Access Denied by controller annotation @IsGranted(["ROLE_ADMIN", "ROLE_USER"])']; yield [['ROLE_ADMIN', 'ROLE_USER'], 'product', 'Access Denied by controller annotation @IsGranted(["ROLE_ADMIN", "ROLE_USER"], product)']; + yield [['ROLE_ADMIN', 'ROLE_USER'], ['product'], 'Access Denied by controller annotation @IsGranted(["ROLE_ADMIN", "ROLE_USER"], product)']; + yield [['ROLE_ADMIN', 'ROLE_USER'], ['product', 'feature'], 'Access Denied by controller annotation @IsGranted(["ROLE_ADMIN", "ROLE_USER"], [product, feature])']; } /**