8000 Add documentation about access denied handler by nykopol · Pull Request #7105 · symfony/symfony-docs · GitHub
[go: up one dir, main page]

Skip to content

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

Closed
wants to merge 6 commits into from
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
add documentation about access denied handler
  • Loading branch information
nykopol committed Oct 31, 2016
8000
commit 10eff8514491c7e98a401d161caa801b6e73921a
1 change: 1 addition & 0 deletions security.rst
Original file line number Diff line number Diff line change
Expand Up @@ -1295,6 +1295,7 @@ Authorization (Denying Access)
security/force_https
security/securing_services
security/access_control
security/access_denied_handler

Other Security Related Topics
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Expand Down
68 changes: 68 additions & 0 deletions security/access_denied_handler.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
.. index::
single: Security; Creating a Custom Access Denied Handler

How to Create a Custom Access Denied Handler
============================================

When your application throw an ``AccessDeniedException`` you can catch this exception
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

throws

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

missing comma before "you"

with a service to return a custom Response.

On each firewall context you can define a custom access denied handler.

.. configuration-block::

.. code-block:: yaml

# app/config/security.yml
firewalls:
foo:
# ...
access_denied_handler: custom_handler.service.id

.. code-block:: php

// app/config/security.php
$container->loadFromExtension('security', array(
'firewalls' => array(
'foo' => array(
// ...
'access_denied_handler' => 'custom_handler.service.id',
),
),
));
Copy link
Member

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?



Your handler must implement the interface
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we can omit "interface" here

:class:`Symfony\\Component\\Security\\Http\\Authorization\\AccessDeniedHandlerInterface`.
This interface define one method called ``handle()`` that can do whatever you want.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

defines

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would reword this a bit:

[...] that implements the logic you want to execute when access is denied to the current user.

You can use it to send a mail, log a message, or generally return a custom Response.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This could then be enclosed with parentheses after the previous sentence.



Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please remove one blank line.

.. code-block:: php

namespace AppBundle\Security;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should better not use AppBundle in this context.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What would be a good place for this class ? For me AppBundle\Security look like a perfect default place for a Foo project.

Copy link
Member

Choose a reason for hiding this comment

The 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\Security\Http\Authorization\AccessDeniedHandlerInterface;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Security\Core\Exception\AccessDeniedException;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you please sort the use statements alphabetically?


class AccessDeniedHandler implements AccessDeniedHandlerInterface
{
public function handle(Request $request, AccessDeniedException $accessDeniedException)
{
// to some stuff...
return new Response($content, 403);
}
}

Then you must register your service :

.. code-block:: yml

# app/config/services.yml
services:
custom_handler.service.id:
class: AppBundle\Security\AccessDeniedHandler

That's it, now on the ``foo`` firewall, all ``AccessDeniedException`` will be notified to you service.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

- to you service.
+ to your service.

0