Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit bb0872a

Browse files
Update voters.rst
Trying to merge the suggestions of 4 people ;-) What's really odd is that on this dedicated voters page, the reader is referred to someplace else for more info on voters ;-) > Take a look at the authorization article for an even deeper understanding on voters. So in the long run, these two should be merged - or at least the voters part integrated into this page.
1 parent 577dfa2 commit bb0872a

File tree

1 file changed

+17
-21
lines changed

1 file changed

+17
-21
lines changed

security/voters.rst

Lines changed: 17 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -6,19 +6,30 @@
66
How to Use Voters to Check User Permissions
77
===========================================
88

9-
Security voters are the most granular way of checking permissions (e.g. "can this
10-
specific user edit the given item?"). This article explains voters in detail.
9+
Voters are Symfony's most powerful way of managing permissions. They allow you
10+
to centralize all permission logic, then reuse them in many places.
11+
12+
However, if you don't reuse permissions or your rules are basic, you can always
13+
put that logic directly into your controller instead. Here's an example how
14+
this could look like, if you want to make a route accessible to the "owner" only::
15+
16+
// src/AppBundle/Controller/PostController.php
17+
// ...
18+
19+
if ($post->getOwner() !== $this->getUser()) {
20+
throw $this->createAccessDeniedException();
21+
}
22+
23+
In that sense, the following example used throughout this page is more like a
24+
minimal example for voters, rather than a real-world use case.
1125

1226
.. tip::
1327

1428
Take a look at the
1529
:doc:`authorization </components/security/authorization>`
1630
article for an even deeper understanding on voters.
1731

18-
How Symfony Uses Voters
19-
-----------------------
20-
21-
In order to use voters, you have to understand how Symfony works with them.
32+
Here's how Symfony works with voters:
2233
All voters are called each time you use the ``isGranted()`` method on Symfony's
2334
authorization checker or call ``denyAccessUnlessGranted`` in a controller (which
2435
uses the authorization checker), or by
@@ -31,21 +42,6 @@ in the application, which can be: affirmative, consensus or unanimous.
3142
For more information take a look at
3243
:ref:`the section about access decision managers <components-security-access-decision-manager>`.
3344

34-
.. tip::
35-
36-
The example used throughout this page features just two routes (``post_show`` and ``post_edit``).
37-
However, the advantage of voters is that you can reuse them in *many* places and centralize
38-
all permission logic. If you don't reuse permissions or the rules are basic, you instead
39-
might want to do the check in the controller directly and throw an ``AccessDeniedException``
40-
to create the correct response::
41-
42-
// src/AppBundle/Controller/PostController.php
43-
// ...
44-
45-
if ($post->getOwner() !== $this->getUser()) {
46-
throw $this-> createAccessDeniedException();
47-
}
48-
4945
The Voter Interface
5046
-------------------
5147

0 commit comments

Comments
 (0)