authenticationRoles can be set to appear in the tooltip of the key icon for API calls that require authentication.

This commit is contained in:
Nils Wisiol 2013-06-24 14:27:22 +02:00
parent 0b17291084
commit f764773c89
8 changed files with 63 additions and 5 deletions

View File

@ -110,6 +110,11 @@ class ApiDoc
*/ */
private $authentication = false; private $authentication = false;
/**
* @var array
*/
private $authenticationRoles = array();
/** /**
* @var int * @var int
*/ */
@ -162,6 +167,12 @@ class ApiDoc
$this->setAuthentication((bool) $data['authentication']); $this->setAuthentication((bool) $data['authentication']);
} }
if (isset($data['authenticationRoles'])) {
foreach ($data['authenticationRoles'] as $key => $role) {
$this->authenticationRoles[] = $role;
}
}
if (isset($data['cache'])) { if (isset($data['cache'])) {
$this->setCache($data['cache']); $this->setCache($data['cache']);
} }
@ -374,6 +385,22 @@ class ApiDoc
$this->authentication = $authentication; $this->authentication = $authentication;
} }
/**
* @return array
*/
public function getAuthenticationRoles()
{
return $this->authenticationRoles;
}
/**
* @param array $authenticationRoles
*/
public function setAuthenticationRoles($authenticationRoles)
{
$this->authenticationRoles = $authenticationRoles;
}
/** /**
* @return int * @return int
*/ */
@ -475,6 +502,7 @@ class ApiDoc
$data['https'] = $this->https; $data['https'] = $this->https;
$data['authentication'] = $this->authentication; $data['authentication'] = $this->authentication;
$data['authenticationRoles'] = $this->authenticationRoles;
$data['deprecated'] = $this->deprecated; $data['deprecated'] = $this->deprecated;
return $data; return $data;

View File

@ -22,8 +22,11 @@ class JmsSecurityExtraHandler implements HandlerInterface
public function handle(ApiDoc $annotation, array $annotations, Route $route, \ReflectionMethod $method) public function handle(ApiDoc $annotation, array $annotations, Route $route, \ReflectionMethod $method)
{ {
foreach ($annotations as $annot) { foreach ($annotations as $annot) {
if ($annot instanceof Secure || $annot instanceof PreAuthorize) { if ($annot instanceof PreAuthorize) {
$annotation->setAuthentication(true); $annotation->setAuthentication(true);
} else if ($annot instanceof Secure) {
$annotation->setAuthentication(true);
$annotation->setAuthenticationRoles(is_array($annot->roles) ? $annot->roles : explode(',', $annot->roles));
} }
} }
} }

View File

@ -175,7 +175,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) * @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` * @Sensio\Bundle\FrameworkExtraBundle\Configuration\Cache - set `cache`

View File

@ -15,7 +15,7 @@
<span class="icon lock" title="HTTPS"></span> <span class="icon lock" title="HTTPS"></span>
{% endif %} {% endif %}
{% if data.authentication %} {% if data.authentication %}
<span class="icon keys" title="Needs authentication"></span> <span class="icon keys" title="Needs {{ data.authenticationRoles|length > 0 ? data.authenticationRoles|join(', ') : 'authentication' }}"></span>
{% endif %} {% endif %}
<span class="path"> <span class="path">

View File

@ -30,6 +30,7 @@ class ApiDocTest extends TestCase
$this->assertFalse(isset($array['description'])); $this->assertFalse(isset($array['description']));
$this->assertNull($annot->getInput()); $this->assertNull($annot->getInput());
$this->assertFalse($array['authentication']); $this->assertFalse($array['authentication']);
$this->assertTrue(is_array($array['authenticationRoles']));
} }
public function testConstructWithInvalidData() public function testConstructWithInvalidData()

View File

@ -181,6 +181,9 @@ class ApiDocExtractorTest extends WebTestCase
$this->assertTrue( $this->assertTrue(
$annotation->getAuthentication() $annotation->getAuthentication()
); );
$this->assertTrue(in_array('ROLE_USER', $annotation->getAuthenticationRoles()));
$this->assertTrue(in_array('ROLE_FOOBAR', $annotation->getAuthenticationRoles()));
$this->assertEquals(2, count($annotation->getAuthenticationRoles()));
} }
public function testGetWithCache() public function testGetWithCache()

View File

@ -158,7 +158,8 @@ class TestController
/** /**
* @ApiDoc( * @ApiDoc(
* authentication=true * authentication=true,
* authenticationRoles={"ROLE_USER","ROLE_FOOBAR"}
* ) * )
*/ */
public function authenticatedAction() public function authenticatedAction()

View File

@ -60,6 +60,7 @@ class SimpleFormatterTest extends WebTestCase
), ),
'https' => false, 'https' => false,
'authentication' => false, 'authentication' => false,
'authenticationRoles' => array(),
'deprecated' => false, 'deprecated' => false,
), ),
1 => 1 =>
@ -94,6 +95,7 @@ class SimpleFormatterTest extends WebTestCase
), ),
'https' => false, 'https' => false,
'authentication' => false, 'authentication' => false,
'authenticationRoles' => array(),
'deprecated' => false, 'deprecated' => false,
), ),
2 => 2 =>
@ -137,6 +139,7 @@ class SimpleFormatterTest extends WebTestCase
), ),
'https' => false, 'https' => false,
'authentication' => false, 'authentication' => false,
'authenticationRoles' => array(),
'deprecated' => false, 'deprecated' => false,
), ),
3 => 3 =>
@ -180,6 +183,7 @@ class SimpleFormatterTest extends WebTestCase
), ),
'https' => false, 'https' => false,
'authentication' => false, 'authentication' => false,
'authenticationRoles' => array(),
'deprecated' => false, 'deprecated' => false,
), ),
), ),
@ -202,6 +206,7 @@ class SimpleFormatterTest extends WebTestCase
), ),
'https' => false, 'https' => false,
'authentication' => false, 'authentication' => false,
'authenticationRoles' => array(),
'deprecated' => false, 'deprecated' => false,
), ),
1 => 1 =>
@ -211,6 +216,7 @@ class SimpleFormatterTest extends WebTestCase
'description' => 'Action without HTTP verb', 'description' => 'Action without HTTP verb',
'https' => false, 'https' => false,
'authentication' => false, 'authentication' => false,
'authenticationRoles' => array(),
'deprecated' => false, 'deprecated' => false,
), ),
2 => 2 =>
@ -229,6 +235,7 @@ class SimpleFormatterTest extends WebTestCase
), ),
'https' => false, 'https' => false,
'authentication' => false, 'authentication' => false,
'authenticationRoles' => array(),
'deprecated' => false, 'deprecated' => false,
), ),
3 => 3 =>
@ -237,6 +244,7 @@ class SimpleFormatterTest extends WebTestCase
'uri' => '/authenticated', 'uri' => '/authenticated',
'https' => false, 'https' => false,
'authentication' => true, 'authentication' => true,
'authenticationRoles' => array('ROLE_USER','ROLE_FOOBAR'),
'deprecated' => false, 'deprecated' => false,
), ),
4 => 4 =>
@ -437,6 +445,7 @@ With multiple lines.',
), ),
'https' => false, 'https' => false,
'authentication' => false, 'authentication' => false,
'authenticationRoles' => array(),
'deprecated' => false, 'deprecated' => false,
), ),
5 => 5 =>
@ -456,6 +465,7 @@ With multiple lines.',
), ),
'https' => false, 'https' => false,
'authentication' => false, 'authentication' => false,
'authenticationRoles' => array(),
'deprecated' => false, 'deprecated' => false,
), ),
6 => 6 =>
@ -496,6 +506,7 @@ And, it supports multilines until the first \'@\' char.',
'description' => 'This method is useful to test if the getDocComment works.', '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.", 'documentation' => "This method is useful to test if the getDocComment works.\nAnd, it supports multilines until the first '@' char.",
'authentication' => false, 'authentication' => false,
'authenticationRoles' => array(),
'deprecated' => false, 'deprecated' => false,
), ),
7 => 7 =>
@ -504,6 +515,7 @@ And, it supports multilines until the first \'@\' char.',
'uri' => '/return-nested-output', 'uri' => '/return-nested-output',
'https' => false, 'https' => false,
'authentication' => false, 'authentication' => false,
'authenticationRoles' => array(),
'deprecated' => false, 'deprecated' => false,
'response' => 'response' =>
array ( array (
@ -712,6 +724,7 @@ With multiple lines.',
), ),
'https' => true, 'https' => true,
'authentication' => false, 'authentication' => false,
'authenticationRoles' => array(),
'deprecated' => false, 'deprecated' => false,
), ),
9 => 9 =>
@ -729,6 +742,7 @@ With multiple lines.',
), ),
'https' => false, 'https' => false,
'authentication' => false, 'authentication' => false,
'authenticationRoles' => array(),
'deprecated' => false, 'deprecated' => false,
), ),
10 => 10 =>
@ -737,6 +751,7 @@ With multiple lines.',
'uri' => '/z-action-with-deprecated-indicator', 'uri' => '/z-action-with-deprecated-indicator',
'https' => false, 'https' => false,
'authentication' => false, 'authentication' => false,
'authenticationRoles' => array(),
'deprecated' => true, 'deprecated' => true,
), ),
11 => 11 =>
@ -754,6 +769,7 @@ With multiple lines.',
), ),
'https' => false, 'https' => false,
'authentication' => false, 'authentication' => false,
'authenticationRoles' => array(),
'deprecated' => false, 'deprecated' => false,
), ),
12 => 12 =>
@ -770,6 +786,7 @@ With multiple lines.',
), ),
'https' => false, 'https' => false,
'authentication' => false, 'authentication' => false,
'authenticationRoles' => array(),
'deprecated' => false, 'deprecated' => false,
), ),
13 => 13 =>
@ -787,6 +804,7 @@ With multiple lines.',
), ),
'https' => false, 'https' => false,
'authentication' => false, 'authentication' => false,
'authenticationRoles' => array(),
'deprecated' => false, 'deprecated' => false,
), ),
14 => 14 =>
@ -805,6 +823,7 @@ With multiple lines.',
), ),
'https' => false, 'https' => false,
'authentication' => false, 'authentication' => false,
'authenticationRoles' => array(),
'deprecated' => false, 'deprecated' => false,
), ),
), ),
@ -826,6 +845,7 @@ With multiple lines.',
), ),
'https' => false, 'https' => false,
'authentication' => false, 'authentication' => false,
'authenticationRoles' => array(),
'deprecated' => false, 'deprecated' => false,
), ),
), ),
@ -847,6 +867,7 @@ With multiple lines.',
), ),
'https' => false, 'https' => false,
'authentication' => false, 'authentication' => false,
'authenticationRoles' => array(),
'deprecated' => false, 'deprecated' => false,
), ),
), ),
@ -884,7 +905,8 @@ With multiple lines.',
), ),
'https' => false, 'https' => false,
'authentication' => false, 'authentication' => false,
'deprecated' => false, 'authenticationRoles' => array(),
'deprecated' => false,
); );
$this->assertEquals($expected, $result); $this->assertEquals($expected, $result);