Created
November 24, 2011 17:24
-
-
Save cystbear/1391850 to your computer and use it in GitHub Desktop.
Revisions
-
cystbear revised this gist
Nov 24, 2011 . 3 changed files with 16 additions and 17 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -2,7 +2,6 @@ class DefaultController extends Controller { /** * Dashboard page. * @Permissions(perm="dashboard_view") This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -2,11 +2,11 @@ namespace SomeNamespace\SomeBundle\Annotations\Driver; 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;//get annotations reader } /** * This event will fire during any controller call */ public function onKernelController(FilterControllerEvent $event) { if (!is_array($controller = $event->getController())) { //return if no controller return; } $object = new \ReflectionObject($controller[0]);// get controller $method = $object->getMethod($controller[1]);// get method 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)){ //if any throw 403 throw new AccessDeniedHttpException(); } This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal 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 #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 -
cystbear revised this gist
Nov 24, 2011 . 2 changed files with 30 additions and 0 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal 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 в конструктор нашего сервиса This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal 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() {...} } -
cystbear revised this gist
Nov 24, 2011 . 3 changed files with 0 additions and 0 deletions.There are no files selected for viewing
File renamed without changes.File renamed without changes.File renamed without changes. -
cystbear revised this gist
Nov 24, 2011 . 1 changed file with 47 additions and 0 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal 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(); } } } } } -
cystbear revised this gist
Nov 24, 2011 . 1 changed file with 10 additions and 0 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,10 @@ <?php namespace SomeNameSpace\SomeBundle\Annotations; /** * @Annotation */ class Permissions { public $perm; } -
cystbear renamed this gist
Nov 24, 2011 . 1 changed file with 0 additions and 0 deletions.There are no files selected for viewing
File renamed without changes. -
cystbear created this gist
Nov 24, 2011 .There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal 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() {.......