Merge pull request #497 from pyrech/security-annotation-support

Added support for Security annotation
This commit is contained in:
William Durand 2014-09-02 08:55:54 +02:00
commit ca0dd69752
8 changed files with 93 additions and 5 deletions

View File

@ -15,6 +15,7 @@ use Nelmio\ApiDocBundle\Extractor\HandlerInterface;
use Nelmio\ApiDocBundle\Annotation\ApiDoc; use Nelmio\ApiDocBundle\Annotation\ApiDoc;
use Symfony\Component\Routing\Route; use Symfony\Component\Routing\Route;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Cache; use Sensio\Bundle\FrameworkExtraBundle\Configuration\Cache;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Security;
class SensioFrameworkExtraHandler implements HandlerInterface class SensioFrameworkExtraHandler implements HandlerInterface
{ {
@ -23,6 +24,8 @@ class SensioFrameworkExtraHandler implements HandlerInterface
foreach ($annotations as $annot) { foreach ($annotations as $annot) {
if ($annot instanceof Cache) { if ($annot instanceof Cache) {
$annotation->setCache($annot->getMaxAge()); $annotation->setCache($annot->getMaxAge());
} elseif ($annot instanceof Security) {
$annotation->setAuthentication(true);
} }
} }
} }

View File

@ -15,7 +15,7 @@ use Nelmio\ApiDocBundle\Tests\WebTestCase;
class ApiDocExtractorTest extends WebTestCase class ApiDocExtractorTest extends WebTestCase
{ {
const ROUTES_QUANTITY = 31; const ROUTES_QUANTITY = 33;
public function testAll() public function testAll()
{ {
@ -199,7 +199,7 @@ class ApiDocExtractorTest extends WebTestCase
{ {
$container = $this->getContainer(); $container = $this->getContainer();
$extractor = $container->get('nelmio_api_doc.extractor.api_doc_extractor'); $extractor = $container->get('nelmio_api_doc.extractor.api_doc_extractor');
$annotation = $extractor->get('Nelmio\ApiDocBundle\Tests\Fixtures\Controller\TestController::CachedAction', 'test_route_14'); $annotation = $extractor->get('Nelmio\ApiDocBundle\Tests\Fixtures\Controller\TestController::zCachedAction', 'test_route_23');
$this->assertNotNull($annotation); $this->assertNotNull($annotation);
$this->assertEquals( $this->assertEquals(

View File

@ -0,0 +1,38 @@
<?php
/*
* This file is part of the NelmioApiDocBundle.
*
* (c) Nelmio <hello@nelm.io>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Nelmio\ApiDocBundle\Tests\Extractor;
use Nelmio\ApiDocBundle\Tests\WebTestCase;
class SensioFrameworkExtraHandlerTest extends WebTestCase
{
public function testCacheAnnotation()
{
$container = $this->getContainer();
$extractor = $container->get('nelmio_api_doc.extractor.api_doc_extractor');
$annotation = $extractor->get('Nelmio\ApiDocBundle\Tests\Fixtures\Controller\TestController::zCachedAction', 'test_route_23');
$this->assertNotNull($annotation);
$this->assertSame(60, $annotation->getCache());
}
public function testSecurityAnnotation()
{
$container = $this->getContainer();
$extractor = $container->get('nelmio_api_doc.extractor.api_doc_extractor');
$annotation = $extractor->get('Nelmio\ApiDocBundle\Tests\Fixtures\Controller\TestController::zSecuredAction', 'test_route_24');
$this->assertNotNull($annotation);
$this->assertTrue($annotation->getAuthentication());
}
}

View File

@ -17,6 +17,7 @@ use Nelmio\ApiDocBundle\Annotation\ApiDoc;
use Symfony\Component\HttpFoundation\Response; use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Validator\Constraints\Email; use Symfony\Component\Validator\Constraints\Email;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Cache; use Sensio\Bundle\FrameworkExtraBundle\Configuration\Cache;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Security;
class TestController class TestController
{ {
@ -196,7 +197,15 @@ class TestController
* @ApiDoc() * @ApiDoc()
* @Cache(maxage=60, public=1) * @Cache(maxage=60, public=1)
*/ */
public function cachedAction() public function zCachedAction()
{
}
/**
* @ApiDoc()
* @Security("has_role('ROLE_USER')")
*/
public function zSecuredAction()
{ {
} }

View File

@ -207,3 +207,15 @@ test_route_update_another_resource:
swagger_doc: swagger_doc:
resource: @NelmioApiDocBundle/Resources/config/swagger_routing.yml resource: @NelmioApiDocBundle/Resources/config/swagger_routing.yml
prefix: /api-docs prefix: /api-docs
test_route_23:
pattern: /zcached
defaults: { _controller: NelmioApiDocTestBundle:Test:zCached }
requirements:
_method: POST
test_route_24:
pattern: /zsecured
defaults: { _controller: NelmioApiDocTestBundle:Test:zSecured }
requirements:
_method: POST

View File

@ -835,6 +835,13 @@ related[a]:
related[b]: related[b]:
* type: DateTime * type: DateTime
### `POST` /zcached ###
### `POST` /zsecured ###
MARKDOWN; MARKDOWN;
$this->assertEquals($expected, $result); $this->assertEquals($expected, $result);

View File

@ -1237,6 +1237,25 @@ With multiple lines.',
) )
), ),
'authenticationRoles' => array(), 'authenticationRoles' => array(),
),
19 =>
array(
'cache' => 60,
'method' => 'POST',
'uri' => '/zcached',
'https' => false,
'authentication' => false,
'authenticationRoles' => array(),
'deprecated' => false
),
20 =>
array(
'authentication' => true,
'method' => 'POST',
'uri' => '/zsecured',
'https' => false,
'authenticationRoles' => array(),
'deprecated' => false
) )
), ),
'/tests2' => '/tests2' =>

View File

@ -32,7 +32,7 @@
"symfony/form": "~2.1", "symfony/form": "~2.1",
"friendsofsymfony/rest-bundle": "~1.0", "friendsofsymfony/rest-bundle": "~1.0",
"jms/serializer-bundle": ">=0.11", "jms/serializer-bundle": ">=0.11",
"sensio/framework-extra-bundle": "~2.1" "sensio/framework-extra-bundle": "~3.0"
}, },
"suggest": { "suggest": {
"symfony/form": "For using form definitions as input.", "symfony/form": "For using form definitions as input.",