-
-
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
Closed
Closed
Changes from 1 commit
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
10eff85
add documentation about access denied handler
nykopol 3d28e5b
fix yml marker to yaml
nykopol 4731340
fix typo
nykopol e8b708b
xabbuh review
nykopol 7922dc9
When talking about generic responses, don't spell it as Response (whi…
javiereguiluz b30ad2a
Minor rewordings
javiereguiluz File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev
Previous commit
Minor rewordings
- Loading branch information
commit b30ad2a4e5c60611c7b442135562d83cf23a328b
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,10 +4,10 @@ | |
How to Create a Custom Access Denied Handler | ||
============================================ | ||
|
||
When your application throws an ``AccessDeniedException``, you can catch this exception | ||
When your application throws an ``AccessDeniedException``, you can handle this exception | ||
with a service to return a custom response. | ||
|
||
On each firewall context you can define a custom access denied handler. | ||
Each firewall context can define its own custom access denied handler: | ||
|
||
.. configuration-block:: | ||
|
||
|
@@ -17,13 +17,13 @@ On each firewall context you can define a custom access denied handler. | |
firewalls: | ||
foo: | ||
# ... | ||
access_denied_handler: custom_handler.service.id | ||
access_denied_handler: app.security.access_denied_handler | ||
|
||
.. code-block:: xml | ||
|
||
<config> | ||
<firewall name="foo"> | ||
<access_denied_handler>custom_handler.service.id</access_denied_handler> | ||
<access_denied_handler>app.security.access_denied_handler</access_denied_handler> | ||
</firewall> | ||
</config> | ||
|
||
|
@@ -34,16 +34,16 @@ On each firewall context you can define a custom access denied handler. | |
'firewalls' => array( | ||
'foo' => array( | ||
// ... | ||
'access_denied_handler' => 'custom_handler.service.id', | ||
'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 you want | ||
to execute when access is denied to the current user (send a mail, log a message, or | ||
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 | ||
|
@@ -59,18 +59,19 @@ generally return a custom response). | |
{ | ||
public function handle(Request $request, AccessDeniedException $accessDeniedException) | ||
{ | ||
// to some stuff... | ||
// ... | ||
|
||
return new Response($content, 403); | ||
} | ||
} | ||
|
||
Then you must register your service : | ||
Then, register the service for the access denied handler: | ||
|
||
.. code-block:: yaml | ||
|
||
# app/config/services.yml | ||
services: | ||
custom_handler.service.id: | ||
app.security.access_denied_handler: | ||
class: AppBundle\Security\AccessDeniedHandler | ||
|
||
That's it, now on the ``foo`` firewall, all ``AccessDeniedException`` will be notified to your service. | ||
That's it! Any ``AccessDeniedException`` thrown by the ``foo`` firewall will now be handled by your service. |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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?