-
-
Save cystbear/1391850 to your computer and use it in GitHub Desktop.
| <?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(); | |
| } | |
| } | |
| } | |
| } | |
| } |
| <?php | |
| namespace SomeNameSpace\SomeBundle\Annotations; | |
| /** | |
| * @Annotation | |
| */ | |
| class Permissions | |
| { | |
| public $perm; | |
| } |
| <?php | |
| class DefaultController extends Controller | |
| { | |
| /** | |
| * Dashboard page. | |
| * @Permissions(perm="dashboard_view") | |
| * @Route("/", name="ITEDashboardBundle_index") | |
| * @Template() | |
| * @return array | |
| */ | |
| public function indexAction() | |
| {....... |
Try:
$event->setResponse($response);Nice tips, have you an idea to list all IDs ?
@tgallice all event ids ?
I am followed all steps but I put the annotation on method. I get this error:
FileLoaderImportCircularReferenceException in FileLoader.php line 97:
Circular reference detected in "C:\htdocs\pronaturalis\app/config/routing_dev.yml" ("C:\htdocs\pronaturalis\app/config/routing_dev.yml" > "C:\htdocs\pronaturalis\app/config\routing.yml" > "C:\htdocs\pronaturalis\src\MLM\Bundle\MLMBundle/Controller/" > "C:\htdocs\pronaturalis\app/config/routing_dev.yml").
Any idea what could be wrong? I am using Symfony 2.6
@dextervip I had the same error when I tried to create a service that provide me the annotation reader object to pass as an argument in the annotation driver service (all this because the service; @annotation_reader its not reconized) I solve this creating a class (AnnotationReaderSon) that extends Annotation Reader (from Doctrine Common), and then creating a service from the new class 'AnnotationReaderSon'.
Is there a way to make a redirection inside onKernelController ?