Skip to content

Instantly share code, notes, and snippets.

@cystbear
Created November 24, 2011 17:24
Show Gist options
  • Select an option

  • Save cystbear/1391850 to your computer and use it in GitHub Desktop.

Select an option

Save cystbear/1391850 to your computer and use it in GitHub Desktop.

Revisions

  1. cystbear revised this gist Nov 24, 2011. 3 changed files with 16 additions and 17 deletions.
    1 change: 0 additions & 1 deletion 1_Result.php
    Original file line number Diff line number Diff line change
    @@ -2,7 +2,6 @@

    class DefaultController extends Controller
    {

    /**
    * Dashboard page.
    * @Permissions(perm="dashboard_view")
    26 changes: 13 additions & 13 deletions 3_AnnotationDriver.php
    Original file line number Diff line number Diff line change
    @@ -2,11 +2,11 @@

    namespace SomeNamespace\SomeBundle\Annotations\Driver;

    use Doctrine\Common\Annotations\Reader;//Вот эта штука как раз и читает аннотации
    use Symfony\Component\HttpKernel\Event\FilterControllerEvent;//Подключаем нужный компонент ядра
    use SomeNamespace\SomeBundle\Annotations;//Юзаем свою аннотацию
    use SomeNamespace\SomeBundle\Security\Permission; //В этом классе я проверяю соответствие permission to user
    use Symfony\Component\HttpFoundation\Response; // В нашем примере я просто буду выводить 403, если нет доступа
    use Doctrine\Common\Annotations\Reader;//This thing read annotations
    use Symfony\Component\HttpKernel\Event\FilterControllerEvent;//Use essential kernel component
    use SomeNamespace\SomeBundle\Annotations;//Use our annotation
    use SomeNamespace\SomeBundle\Security\Permission;//In this class I check correspondence permission to user
    use Symfony\Component\HttpFoundation\Response;// For example I will throw 403, if access denied

    use Symfony\Component\HttpKernel\Exception\AccessDeniedHttpException;

    @@ -16,27 +16,27 @@ class AnnotationDriver{

    public function __construct($reader)
    {
    $this->reader = $reader;//Получаем читалку аннотаций
    $this->reader = $reader;//get annotations reader
    }
    /**
    * Это событие возникнет при вызове любого контроллера
    * This event will fire during any controller call
    */
    public function onKernelController(FilterControllerEvent $event)
    {

    if (!is_array($controller = $event->getController())) { //Выходим, если нет контроллера
    if (!is_array($controller = $event->getController())) { //return if no controller
    return;
    }

    $object = new \ReflectionObject($controller[0]);// Получаем контроллер
    $method = $object->getMethod($controller[1]);// Получаем метод
    $object = new \ReflectionObject($controller[0]);// get controller
    $method = $object->getMethod($controller[1]);// get method

    foreach ($this->reader->getMethodAnnotations($method) as $configuration) { //Начинаем читать аннотации
    if(isset($configuration->perm)){//Если прочитанная аннотация наша, то выполняем код ниже
    foreach ($this->reader->getMethodAnnotations($method) as $configuration) { //Start of annotations reading
    if(isset($configuration->perm)){//Found our annotation
    $perm = new Permission($controller[0]->get('doctrine.odm.mongodb.document_manager'));
    $userName = $controller[0]->get('security.context')->getToken()->getUser()->getUserName();
    if(!$perm->isAccess($userName,$configuration->perm)){
    //Если после проверки доступа нет, то выдаём 403
    //if any throw 403
    throw new AccessDeniedHttpException();

    }
    6 changes: 3 additions & 3 deletions 4_serviceDeclaration.yml
    Original file line number Diff line number Diff line change
    @@ -1,6 +1,6 @@
    # SomeBundle\config\services.yml
    services:
    some_annotation_driver:
    class: SomeNamespace\SomeBundle\Annotations\Driver\AnnotationDriver #Указываем класс
    tags: [{name: kernel.event_listener, event: kernel.controller, method: onKernelController}] #Указываем по какому событию вызывать этот сервис
    arguments: [@annotation_reader] # Передаём annotation_reader в конструктор нашего сервиса
    class: SomeNamespace\SomeBundle\Annotations\Driver\AnnotationDriver #Point class
    tags: [{name: kernel.event_listener, event: kernel.controller, method: onKernelController}] #Point event
    arguments: [@annotation_reader] # Pass annotation_reader into constructor of our service
  2. cystbear revised this gist Nov 24, 2011. 2 changed files with 30 additions and 0 deletions.
    6 changes: 6 additions & 0 deletions 4_serviceDeclaration.yml
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,6 @@
    # SomeBundle\config\services.yml
    services:
    some_annotation_driver:
    class: SomeNamespace\SomeBundle\Annotations\Driver\AnnotationDriver #Указываем класс
    tags: [{name: kernel.event_listener, event: kernel.controller, method: onKernelController}] #Указываем по какому событию вызывать этот сервис
    arguments: [@annotation_reader] # Передаём annotation_reader в конструктор нашего сервиса
    24 changes: 24 additions & 0 deletions 5_Usage.php
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,24 @@
    namespace SomeNamespace\SomeBundle\Controller;

    use SomeNamespace\SomeBundle\Annotations\Permissions;

    <?php

    /**
    * Dashboard controller.
    *
    * @Route("/dashboard")
    */
    class DefaultController extends Controller
    {

    /**
    * Dashboard page.
    * @Permissions(perm="dashboard_view")
    * @Route("/", name="ITEDashboardBundle_index")
    * @Template()
    * @return array
    */
    public function indexAction()
    {...}
    }
  3. cystbear revised this gist Nov 24, 2011. 3 changed files with 0 additions and 0 deletions.
    File renamed without changes.
    File renamed without changes.
    File renamed without changes.
  4. cystbear revised this gist Nov 24, 2011. 1 changed file with 47 additions and 0 deletions.
    47 changes: 47 additions & 0 deletions AnnotationDriver.php
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,47 @@
    <?php

    namespace SomeNamespace\SomeBundle\Annotations\Driver;

    use Doctrine\Common\Annotations\Reader;//Вот эта штука как раз и читает аннотации
    use Symfony\Component\HttpKernel\Event\FilterControllerEvent;//Подключаем нужный компонент ядра
    use SomeNamespace\SomeBundle\Annotations;//Юзаем свою аннотацию
    use SomeNamespace\SomeBundle\Security\Permission; //В этом классе я проверяю соответствие permission to user
    use Symfony\Component\HttpFoundation\Response; // В нашем примере я просто буду выводить 403, если нет доступа

    use Symfony\Component\HttpKernel\Exception\AccessDeniedHttpException;

    class AnnotationDriver{

    private $reader;

    public function __construct($reader)
    {
    $this->reader = $reader;//Получаем читалку аннотаций
    }
    /**
    * Это событие возникнет при вызове любого контроллера
    */
    public function onKernelController(FilterControllerEvent $event)
    {

    if (!is_array($controller = $event->getController())) { //Выходим, если нет контроллера
    return;
    }

    $object = new \ReflectionObject($controller[0]);// Получаем контроллер
    $method = $object->getMethod($controller[1]);// Получаем метод

    foreach ($this->reader->getMethodAnnotations($method) as $configuration) { //Начинаем читать аннотации
    if(isset($configuration->perm)){//Если прочитанная аннотация наша, то выполняем код ниже
    $perm = new Permission($controller[0]->get('doctrine.odm.mongodb.document_manager'));
    $userName = $controller[0]->get('security.context')->getToken()->getUser()->getUserName();
    if(!$perm->isAccess($userName,$configuration->perm)){
    //Если после проверки доступа нет, то выдаём 403
    throw new AccessDeniedHttpException();

    }

    }
    }
    }
    }
  5. cystbear revised this gist Nov 24, 2011. 1 changed file with 10 additions and 0 deletions.
    10 changes: 10 additions & 0 deletions newAnnotation.php
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,10 @@
    <?php

    namespace SomeNameSpace\SomeBundle\Annotations;
    /**
    * @Annotation
    */
    class Permissions
    {
    public $perm;
    }
  6. cystbear renamed this gist Nov 24, 2011. 1 changed file with 0 additions and 0 deletions.
    File renamed without changes.
  7. cystbear created this gist Nov 24, 2011.
    14 changes: 14 additions & 0 deletions gistfile1.aw
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,14 @@
    <?php

    class DefaultController extends Controller
    {

    /**
    * Dashboard page.
    * @Permissions(perm="dashboard_view")
    * @Route("/", name="ITEDashboardBundle_index")
    * @Template()
    * @return array
    */
    public function indexAction()
    {.......