📜 ⬆️ ⬇️

Custom annotations in symfony 2

Symfony2 is a web framework that has only recently appeared. Accordingly, the developers simply did not have time to write standing documentation for it. In one of the current projects, MongoDB is used, and you can screw the ACL, you just need to write your own ACL provider. But I decided to go my own way. So, that's about what we get:
class DefaultController extends Controller { /** * Dashboard page. * @Permissions(perm="dashboard_view") * @Route("/", name="ITEDashboardBundle_index") * @Template() * @return array */ public function indexAction() {....... 



As you can see, here the route and template annotations are standard and I will not talk about them. We are also interested in our own abstract Permissions.

Well, let's get started.
First you need to create an annotation class that will show the kernel that we now have a new annotation:
 namespace SomeNameSpace\SomeBundle\Annotations; /** * @Annotation */ class Permissions { public $perm; } 

Thus, the summary will be of type @ Permissions (perm = "some_value")
Our next step will be to create a service in which we will read annotations and perform some actions, depending on their values.
 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(); } } } } } 

Please note that we use the reader of annotations from the doctrine. But the doctrine today has become an integral part of symfony2.
Yes. And the last step, but no less important. Now we need to register our hook of the control, or, if correctly called it: EventListener
 # 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     

')
That's all! Now our annotations are ready to use.
PS To use your annotations in your bundles and controllers you need to connect only the class with annotations:
 namespace SomeNamespace\SomeBundle\Controller; use SomeNamespace\SomeBundle\Annotations\Permissions; /** * Dashboard controller. * * @Route("/dashboard") */ class DefaultController extends Controller { /** * Dashboard page. * @Permissions(perm="dashboard_view") * @Route("/", name="ITEDashboardBundle_index") * @Template() * @return array */ public function indexAction() {...} } 

UPD: Made a normal code highlighting, thanks sHinE

Source: https://habr.com/ru/post/133270/


All Articles