I think I was wrong. I wanted to know if the URL was behind the firewall, but I think I should have known if the current user was allowed for the request. In fact, knowing that the user is denied access to the URL means that the URL must be behind the firewall, otherwise access could not be denied.
With this in mind, I was able to get the final result that I wanted. It's pretty simple once you understand how the security mechanism works ...
Symfony\Component\Security\Http\Firewall
listens for the kernel.request
event- The firewall then calls several event listeners registered in
security.yml
- If a security violation is detected (i.e. a user trying to access something without logging in), an
AccessDeniedException
and a kernel.exception
event is kernel.exception
. Symfony/Component/Security/Http/Firewall/ExceptionListener
listens for the event and fires its onKernelException
method, which determines what the next step is. In my case, it will start the authentication process
Since starting the authentication process is something I wanted to avoid, I wrote my own event listener that catches kernel.exception
before the Symfony ExceptionListener
does. I gave my event listener priority 1.
This is the method I wrote:
public function handleException(GetResponseForExceptionEvent $event) { $exception = $event->getException(); $request = $event->getRequest(); if ($request->getMethod() == 'POST') { if ($exception instanceof AccessDeniedException) { $response = new Response({err: 'not logged in'}); $event->setResponse($response); } } }
Until the user is logged in and the request method is POST, a JSON object is returned (which also stops the event being positioned) instead of HTML for the login page. Otherwise, other kernel.exception
listeners will respond, and Symfony can go about its business.
So, the original question remains unanswered, but I think this can be achieved by checking if the user has access to the action. Symfony\Component\Security\Core\Authorization\AccessDecisionManager
looks like this would be useful for this.
Edit
I do not know if this method only processes users who are not logged in. I have not tested it yet, but I think that it also works if the user (login) tries to access an action that requires a role that they provide was not. If this causes a problem, I will try to change it to use the Symfony\Component\Security\Core\Authentication\AuthenticationTrustResolver
isFullFledged($token)
method to take care only of users who are not logged in.
source share