diff --git a/Annotation/ApiDoc.php b/Annotation/ApiDoc.php index b2f527c..04115a9 100644 --- a/Annotation/ApiDoc.php +++ b/Annotation/ApiDoc.php @@ -115,6 +115,11 @@ class ApiDoc */ private $authentication = false; + /** + * @var array + */ + private $authenticationRoles = array(); + /** * @var int */ @@ -200,6 +205,12 @@ class ApiDoc $this->setAuthentication((bool) $data['authentication']); } + if (isset($data['authenticationRoles'])) { + foreach ($data['authenticationRoles'] as $key => $role) { + $this->authenticationRoles[] = $role; + } + } + if (isset($data['cache'])) { $this->setCache($data['cache']); } @@ -436,6 +447,22 @@ class ApiDoc $this->authentication = $authentication; } + /** + * @return array + */ + public function getAuthenticationRoles() + { + return $this->authenticationRoles; + } + + /** + * @param array $authenticationRoles + */ + public function setAuthenticationRoles($authenticationRoles) + { + $this->authenticationRoles = $authenticationRoles; + } + /** * @return int */ @@ -542,6 +569,7 @@ class ApiDoc $data['https'] = $this->https; $data['authentication'] = $this->authentication; + $data['authenticationRoles'] = $this->authenticationRoles; $data['deprecated'] = $this->deprecated; return $data; diff --git a/Extractor/Handler/JmsSecurityExtraHandler.php b/Extractor/Handler/JmsSecurityExtraHandler.php index 4302ee5..b1eeada 100644 --- a/Extractor/Handler/JmsSecurityExtraHandler.php +++ b/Extractor/Handler/JmsSecurityExtraHandler.php @@ -22,8 +22,11 @@ class JmsSecurityExtraHandler implements HandlerInterface public function handle(ApiDoc $annotation, array $annotations, Route $route, \ReflectionMethod $method) { foreach ($annotations as $annot) { - if ($annot instanceof Secure || $annot instanceof PreAuthorize) { + if ($annot instanceof PreAuthorize) { $annotation->setAuthentication(true); + } else if ($annot instanceof Secure) { + $annotation->setAuthentication(true); + $annotation->setAuthenticationRoles(is_array($annot->roles) ? $annot->roles : explode(',', $annot->roles)); } } } diff --git a/README.md b/README.md index 48b44be..993f1c2 100644 --- a/README.md +++ b/README.md @@ -200,7 +200,7 @@ Also bundle will get information from the other annotations: * @FOS\RestBundle\Controller\Annotations\QueryParam - use as `requirements` (when strict parameter is true), `filters` (when strict is false) -* @JMS\SecurityExtraBundle\Annotation\Secure - set `authentification` to true +* @JMS\SecurityExtraBundle\Annotation\Secure - set `authentification` to true, `authenticationRoles` to the given roles * @Sensio\Bundle\FrameworkExtraBundle\Configuration\Cache - set `cache` diff --git a/Resources/views/method.html.twig b/Resources/views/method.html.twig index cb12ed6..1279b72 100644 --- a/Resources/views/method.html.twig +++ b/Resources/views/method.html.twig @@ -15,7 +15,7 @@ {% endif %} {% if data.authentication %} - + {% endif %} diff --git a/Tests/Annotation/ApiDocTest.php b/Tests/Annotation/ApiDocTest.php index e18fb1f..d725dee 100644 --- a/Tests/Annotation/ApiDocTest.php +++ b/Tests/Annotation/ApiDocTest.php @@ -32,6 +32,7 @@ class ApiDocTest extends TestCase $this->assertFalse(isset($array['parameters'])); $this->assertNull($annot->getInput()); $this->assertFalse($array['authentication']); + $this->assertTrue(is_array($array['authenticationRoles'])); } public function testConstructWithInvalidData() diff --git a/Tests/Extractor/ApiDocExtractorTest.php b/Tests/Extractor/ApiDocExtractorTest.php index 2dc4f19..4a05b8a 100644 --- a/Tests/Extractor/ApiDocExtractorTest.php +++ b/Tests/Extractor/ApiDocExtractorTest.php @@ -186,6 +186,9 @@ class ApiDocExtractorTest extends WebTestCase $this->assertTrue( $annotation->getAuthentication() ); + $this->assertContains('ROLE_USER', $annotation->getAuthenticationRoles()); + $this->assertContains('ROLE_FOOBAR', $annotation->getAuthenticationRoles()); + $this->assertCount(2, $annotation->getAuthenticationRoles()); } public function testGetWithCache() diff --git a/Tests/Fixtures/Controller/TestController.php b/Tests/Fixtures/Controller/TestController.php index 3b109b0..72c544e 100644 --- a/Tests/Fixtures/Controller/TestController.php +++ b/Tests/Fixtures/Controller/TestController.php @@ -167,7 +167,8 @@ class TestController /** * @ApiDoc( - * authentication=true + * authentication=true, + * authenticationRoles={"ROLE_USER","ROLE_FOOBAR"} * ) */ public function authenticatedAction() diff --git a/Tests/Formatter/SimpleFormatterTest.php b/Tests/Formatter/SimpleFormatterTest.php index cf05031..ff74534 100644 --- a/Tests/Formatter/SimpleFormatterTest.php +++ b/Tests/Formatter/SimpleFormatterTest.php @@ -60,6 +60,7 @@ class SimpleFormatterTest extends WebTestCase ), 'https' => false, 'authentication' => false, + 'authenticationRoles' => array(), 'deprecated' => false, ), 1 => @@ -94,6 +95,7 @@ class SimpleFormatterTest extends WebTestCase ), 'https' => false, 'authentication' => false, + 'authenticationRoles' => array(), 'deprecated' => false, ), 2 => @@ -134,6 +136,7 @@ class SimpleFormatterTest extends WebTestCase ), 'https' => false, 'authentication' => false, + 'authenticationRoles' => array(), 'deprecated' => false, ), 3 => @@ -174,6 +177,7 @@ class SimpleFormatterTest extends WebTestCase ), 'https' => false, 'authentication' => false, + 'authenticationRoles' => array(), 'deprecated' => false, ), ), @@ -196,6 +200,7 @@ class SimpleFormatterTest extends WebTestCase ), 'https' => false, 'authentication' => false, + 'authenticationRoles' => array(), 'deprecated' => false, ), 1 => @@ -205,6 +210,7 @@ class SimpleFormatterTest extends WebTestCase 'description' => 'Action without HTTP verb', 'https' => false, 'authentication' => false, + 'authenticationRoles' => array(), 'deprecated' => false, ), 2 => @@ -223,6 +229,7 @@ class SimpleFormatterTest extends WebTestCase ), 'https' => false, 'authentication' => false, + 'authenticationRoles' => array(), 'deprecated' => false, ), 3 => @@ -231,6 +238,7 @@ class SimpleFormatterTest extends WebTestCase 'uri' => '/authenticated', 'https' => false, 'authentication' => true, + 'authenticationRoles' => array('ROLE_USER','ROLE_FOOBAR'), 'deprecated' => false, ), 4 => @@ -431,6 +439,7 @@ With multiple lines.', ), 'https' => false, 'authentication' => false, + 'authenticationRoles' => array(), 'deprecated' => false, ), 5 => @@ -450,6 +459,7 @@ With multiple lines.', ), 'https' => false, 'authentication' => false, + 'authenticationRoles' => array(), 'deprecated' => false, ), 6 => @@ -490,6 +500,7 @@ And, it supports multilines until the first \'@\' char.', 'description' => 'This method is useful to test if the getDocComment works.', 'documentation' => "This method is useful to test if the getDocComment works.\nAnd, it supports multilines until the first '@' char.", 'authentication' => false, + 'authenticationRoles' => array(), 'deprecated' => false, ), 7 => @@ -498,6 +509,7 @@ And, it supports multilines until the first \'@\' char.', 'uri' => '/return-nested-output', 'https' => false, 'authentication' => false, + 'authenticationRoles' => array(), 'deprecated' => false, 'response' => array ( @@ -706,6 +718,7 @@ With multiple lines.', ), 'https' => true, 'authentication' => false, + 'authenticationRoles' => array(), 'deprecated' => false, ), 9 => @@ -723,6 +736,7 @@ With multiple lines.', ), 'https' => false, 'authentication' => false, + 'authenticationRoles' => array(), 'deprecated' => false, ), 10 => @@ -731,6 +745,7 @@ With multiple lines.', 'uri' => '/z-action-with-deprecated-indicator', 'https' => false, 'authentication' => false, + 'authenticationRoles' => array(), 'deprecated' => true, ), 11 => @@ -748,6 +763,7 @@ With multiple lines.', ), 'https' => false, 'authentication' => false, + 'authenticationRoles' => array(), 'deprecated' => false, ), 12 => @@ -764,6 +780,7 @@ With multiple lines.', ), 'https' => false, 'authentication' => false, + 'authenticationRoles' => array(), 'deprecated' => false, ), 13 => @@ -781,6 +798,7 @@ With multiple lines.', ), 'https' => false, 'authentication' => false, + 'authenticationRoles' => array(), 'deprecated' => false, ), 14 => @@ -799,6 +817,7 @@ With multiple lines.', ), 'https' => false, 'authentication' => false, + 'authenticationRoles' => array(), 'deprecated' => false, ), ), @@ -820,6 +839,7 @@ With multiple lines.', ), 'https' => false, 'authentication' => false, + 'authenticationRoles' => array(), 'deprecated' => false, ), ), @@ -841,6 +861,7 @@ With multiple lines.', ), 'https' => false, 'authentication' => false, + 'authenticationRoles' => array(), 'deprecated' => false, ), ), @@ -889,7 +910,8 @@ With multiple lines.', ), 'https' => false, 'authentication' => false, - 'deprecated' => false, + 'authenticationRoles' => array(), + 'deprecated' => false, ); $this->assertEquals($expected, $result);