@@ -7,15 +7,15 @@ How to Use Voters to Check User Permissions
7
7
In Symfony2 you can check the permission to access data by using the
8
8
:doc: `ACL module </cookbook/security/acl >`, which is a bit overwhelming
9
9
for many applications. A much easier solution is to work with custom voters,
10
- which are like simple conditional statements. Voters can be
11
- also be used to check for permission as a part or even the whole
12
- application: ":doc: `/cookbook/security/voters `".
10
+ which are like simple conditional statements. Voters can also be used to
11
+ check for permission to a part or even of the whole application:
12
+ ":doc: `/cookbook/security/voters `".
13
13
14
14
.. tip ::
15
15
16
- Have a look at the chapter
16
+ Have a look at the
17
17
:doc: `authorization </components/security/authorization >`
18
- for a better understanding on voters.
18
+ chapter for a better understanding on voters.
19
19
20
20
How Symfony Uses Voters
21
21
-----------------------
@@ -25,7 +25,7 @@ In general, all registered custom voters will be called every time you ask
25
25
Symfony about permissions (ACL). You can use one of three different
26
26
approaches on how to handle the feedback from all voters: affirmative,
27
27
consensus and unanimous. For more information have a look at
28
- ":ref: `components-security-access-decision-manager `".
28
+ ":ref: `the section about access decision managers < components-security-access-decision-manager > `".
29
29
30
30
The Voter Interface
31
31
-------------------
@@ -37,7 +37,7 @@ which has this structure:
37
37
.. include :: /cookbook/security/voter_interface.rst.inc
38
38
39
39
In this example, it'll check if the user will have access to a specific
40
- object according to your custom conditions (e.g. he must be the owner of
40
+ object according to your custom conditions (e.g. they must be the owner of
41
41
the object). If the condition fails, you'll return
42
42
``VoterInterface::ACCESS_DENIED ``, otherwise you'll return
43
43
``VoterInterface::ACCESS_GRANTED ``. In case the responsibility for this decision
@@ -46,17 +46,17 @@ does not belong to this voter, it will return ``VoterInterface::ACCESS_ABSTAIN``
46
46
Creating the Custom Voter
47
47
-------------------------
48
48
49
- You could store your Voter to check permission for the view and edit action like the following::
49
+ You could implement your Voter to check permission for the view and edit action like the following::
50
50
51
- // src/Acme/DemoBundle/Security/Authorization/Entity /PostVoter.php
52
- namespace Acme\DemoBundle\Security\Authorization\Entity ;
51
+ // src/Acme/DemoBundle/Security/Authorization/Voter /PostVoter.php
52
+ namespace Acme\DemoBundle\Security\Authorization\Voter ;
53
53
54
54
use Symfony\Component\Security\Core\Exception\InvalidArgumentException;
55
55
use Symfony\Component\DependencyInjection\ContainerInterface;
56
56
use Symfony\Component\Security\Core\Authorization\Voter\VoterInterface;
57
57
use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
58
58
use Symfony\Component\Security\Core\User\UserInterface;
59
- use Doctrine\Common\Util\ClassUtils ;
59
+ use Acme\DemoBundle\Entity\Post ;
60
60
61
61
class PostVoter implements VoterInterface
62
62
{
@@ -73,7 +73,9 @@ You could store your Voter to check permission for the view and edit action like
73
73
74
74
public function supportsClass($obj)
75
75
{
76
- if ($obj instanceof 'Acme\DemoBundle\Entity\Post') return true;
76
+ if ($obj instanceof Post) {
77
+ return true;
78
+ }
77
79
78
80
return false;
79
81
}
@@ -137,27 +139,29 @@ Declaring the Voter as a Service
137
139
--------------------------------
138
140
139
141
To inject the voter into the security layer, you must declare it as a service
140
- and tag it as a ' security.voter' :
142
+ and tag it as a `` security.voter `` :
141
143
142
144
.. configuration-block ::
143
145
144
146
.. code-block :: yaml
145
147
146
- # src/Acme/AcmeBundle /Resources/config/services.yml
148
+ # src/Acme/DemoBundle /Resources/config/services.yml
147
149
services :
148
150
security.access.post_voter :
149
- class : Acme\DemoBundle\Security\Authorization\Entity \PostVoter
151
+ class : Acme\DemoBundle\Security\Authorization\Voter \PostVoter
150
152
public : false
151
153
tags :
152
154
- { name: security.voter }
153
155
154
156
.. code-block :: xml
155
157
156
158
<?xml version =" 1.0" encoding =" UTF-8" ?>
157
- <container xmlns =" http://symfony.com/schema/dic/services" >
159
+ <container xmlns =" http://symfony.com/schema/dic/services"
160
+ xsi : schemaLocation =" http://symfony.com/schema/dic/services
161
+ http://symfony.com/schema/dic/services/services-1.0.xsd" >
158
162
<services >
159
163
<service id =" security.access.post_document_voter"
160
- class =" Acme\DemoBundle\Security\Authorization\Document \PostVoter"
164
+ class =" Acme\DemoBundle\Security\Authorization\Voter \PostVoter"
161
165
public =" false" >
162
166
<tag name =" security.voter" />
163
167
</service >
@@ -166,29 +170,32 @@ and tag it as a 'security.voter':
166
170
167
171
.. code-block :: php
168
172
173
+ // src/Acme/DemoBundle/Resources/config/services.php
169
174
$container
170
175
->register(
171
176
'security.access.post_document_voter',
172
- 'Acme\DemoBundle\Security\Authorization\Document \PostVoter'
177
+ 'Acme\DemoBundle\Security\Authorization\Voter \PostVoter'
173
178
)
174
179
->addTag('security.voter')
175
180
;
176
181
177
182
How to Use the Voter in a Controller
178
183
------------------------------------
179
184
180
- The registered voter will then always be asked as soon as the method ' isGranted'
185
+ The registered voter will then always be asked as soon as the method `` isGranted() ``
181
186
from the security context is called.
182
187
183
188
.. code-block :: php
184
189
185
190
// src/Acme/DemoBundle/Controller/PostController.php
186
191
namespace Acme\DemoBundle\Controller;
187
192
193
+ use Symfony\Bundle\FrameworkBundle\Controller\Controller;
188
194
use Symfony\Component\HttpFoundation\Response;
189
195
use Symfony\Component\Security\Core\Exception\AccessDeniedException;
196
+ use Acme\DemoBundle\Entity\Post;
190
197
191
- class PostController
198
+ class PostController extends Controller
192
199
{
193
200
194
201
/**
0 commit comments