0

api-platform.com's event won't attach to my listener. I tried several combination from their event matrix but it still won't trigger.

# services.yml
user_access_listener:
    class:      AppBundle\Event\Listener\UserAccessListener
    arguments: [ "@security.authorization_checker" ]
    tags:
        - { name: kernel.event_listener, event: kernel.view, method: onKernelView }

Here is my listener class

namespace AppBundle\Event\Listener;

use UserBundle\Entity\User;
use Symfony\Component\Security\Core\Exception\AccessDeniedException;
use Symfony\Component\HttpKernel\Event\GetResponseForControllerResultEvent;
use Symfony\Component\Security\Core\Authorization\AuthorizationCheckerInterface;

class UserAccessListener
{
    /**
     * @var AuthorizationCheckerInterface
     */
    private $authorizationChecker;

    /**
     * @param AuthorizationCheckerInterface $authorizationChecker
     */
    public function __construct(AuthorizationCheckerInterface $authorizationChecker)
    {
        $this->authorizationChecker = $authorizationChecker;

    }

    /**
     * @param GetResponseForControllerResultEvent $event
     */
    public function onKernelView(GetResponseForControllerResultEvent $event)
    {

        echo "This should trigger";
        exit;

        $user = $event->getControllerResult();
        if (!$user instanceof User) {
            return;
        }

        if (!$this->authorizationChecker->isGranted(null, $user)) {
            throw new AccessDeniedException();
        }
    }
}

api-platform event reference

I was expecting "This should trigger" would appear when I hit GET /projects/1 and GET /projects, but it is not triggering. Thoughts?

Gottlieb Notschnabel
  • 9,408
  • 18
  • 74
  • 116
almar.io
  • 21
  • 3
  • what do you mean with `This should trigger`? Is this a part of unit test? – Confidence Apr 27 '17 at 09:04
  • The kernel view event only gets triggered when the controller does not return a response object. Is that the case in your app? And pulling user from the controller result? Typically your would check authorization in a controller event listener. Right before the controller itself is called. – Cerad Apr 27 '17 at 12:26
  • @Confidence I added an `echo` and `exit;` on that part to see if it is being triggered. – almar.io Apr 27 '17 at 15:22
  • @Cerad I don't have controller for `GET /projects/` I am using the CRUD operations from api-platform's bundle. I have a separate security voter for my controllers but that is no triggered by the api-platform CRUD operation that's why I need to listen to api-platform's event. – almar.io Apr 27 '17 at 15:24
  • So I just checked your api-platform link. I want some of whatever it is that they are smoking! Try bin/console debug:event-dispatcher kernel.view to see if your view listener is even being registered. – Cerad Apr 27 '17 at 16:19
  • @Cerad API Platform is built on top of Symfony's event listener. It doesn't use the default controller system. – Kévin Dunglas May 15 '17 at 11:52

1 Answers1

1

Your should use a higher priority (70 for instance), to be sure that your listener is executed before the builtin view listeners.

Example:

user_access_listener:
    class:      AppBundle\Event\Listener\UserAccessListener
    arguments: [ "@security.authorization_checker" ]
    tags:
        - { name: kernel.event_listener, event: kernel.view, method: onKernelView, priority: 70 }
Kévin Dunglas
  • 2,864
  • 2
  • 23
  • 39