-
-
Notifications
You must be signed in to change notification settings - Fork 5.2k
Add documentation about access denied handler #7105
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
10eff85
3d28e5b
4731340
e8b708b
7922dc9
b30ad2a
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
.. index:: | ||
single: Security; Creating a Custom Access Denied Handler | ||
|
||
How to Create a Custom Access Denied Handler | ||
============================================ | ||
|
||
When your application throws an ``AccessDeniedException``, you can handle this exception | ||
with a service to return a custom response. | ||
|
||
Each firewall context can define its own custom access denied handler: | ||
|
||
.. configuration-block:: | ||
|
||
.. code-block:: yaml | ||
|
||
# app/config/security.yml | ||
firewalls: | ||
foo: | ||
# ... | ||
access_denied_handler: app.security.access_denied_handler | ||
|
||
.. code-block:: xml | ||
|
||
<config> | ||
<firewall name="foo"> | ||
<access_denied_handler>app.security.access_denied_handler</access_denied_handler> | ||
</firewall> | ||
</config> | ||
|
||
.. code-block:: php | ||
|
||
// app/config/security.php | ||
$container->loadFromExtension('security', array( | ||
'firewalls' => array( | ||
'foo' => array( | ||
// ... | ||
'access_denied_handler' => 'app.security.access_denied_handler', | ||
), | ||
), | ||
)); | ||
|
||
|
||
Your handler must implement the | ||
:class:`Symfony\\Component\\Security\\Http\\Authorization\\AccessDeniedHandlerInterface`. | ||
This interface defines one method called ``handle()`` that implements the logic to | ||
execute when access is denied to the current user (send a mail, log a message, or | ||
generally return a custom response). | ||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please remove one blank line. |
||
.. code-block:: php | ||
|
||
namespace AppBundle\Security; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We should better not use AppBundle in this context. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. What would be a good place for this class ? For me There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ah sorry, I was confused. Of course let's keep it as is. |
||
|
||
use Symfony\Component\HttpFoundation\Request; | ||
use Symfony\Component\HttpFoundation\Response; | ||
use Symfony\Component\Security\Core\Exception\AccessDeniedException; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can you please sort the |
||
use Symfony\Component\Security\Http\Authorization\AccessDeniedHandlerInterface; | ||
|
||
class AccessDeniedHandler implements AccessDeniedHandlerInterface | ||
{ | ||
public function handle(Request $request, AccessDeniedException $accessDeniedException) | ||
{ | ||
// ... | ||
|
||
return new Response($content, 403); | ||
} | ||
} | ||
|
||
Then, register the service for the access denied handler: | ||
|
||
.. code-block:: yaml | ||
|
||
# app/config/services.yml | ||
services: | ||
app.security.access_denied_handler: | ||
class: AppBundle\Security\AccessDeniedHandler | ||
|
||
That's it! Any ``AccessDeniedException`` thrown by the ``foo`` firewall will now be handled by your service. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can you please also add an XML config example?