Skip to content

Commit 9b91501

Browse files
Michael Kleinweaverryan
authored andcommitted
updated the docs according to the last review
1 parent 872a05f commit 9b91501

File tree

4 files changed

+30
-21
lines changed

4 files changed

+30
-21
lines changed

cookbook/map.rst.inc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,7 @@
127127
* :doc:`/cookbook/security/remember_me`
128128
* :doc:`/cookbook/security/impersonating_user`
129129
* :doc:`/cookbook/security/voters`
130+
* :doc:`/cookbook/security/voters_data_permission`
130131
* :doc:`/cookbook/security/acl`
131132
* :doc:`/cookbook/security/acl_advanced`
132133
* :doc:`/cookbook/security/force_https`

cookbook/security/acl.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ the ACL system comes in.
1414
Using ACL's isn't trivial, and for simpler use cases, it may be overkill.
1515
If your permission logic could be described by just writing some code (e.g.
1616
to check if a Blog is owned by the current User), then consider using
17-
:doc:`voters </cookbook/security/dataPermissionVoters>`. A voter is passed the object
17+
:doc:`voters </cookbook/security/voters_data_permission>`. A voter is passed the object
1818
being voted on, which you can use to make complex decisions and effectively
1919
implement your own ACL. Enforcing authorization (e.g. the ``isGranted``
2020
part) will look similar to what you see in this entry, but your voter

cookbook/security/index.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ Security
88
remember_me
99
impersonating_user
1010
voters
11+
voters_data_permission
1112
acl
1213
acl_advanced
1314
force_https

cookbook/security/voters_data_permission.rst

Lines changed: 27 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -7,15 +7,15 @@ How to Use Voters to Check User Permissions
77
In Symfony2 you can check the permission to access data by using the
88
:doc:`ACL module </cookbook/security/acl>`, which is a bit overwhelming
99
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`".
1313

1414
.. tip::
1515

16-
Have a look at the chapter
16+
Have a look at the
1717
:doc:`authorization </components/security/authorization>`
18-
for a better understanding on voters.
18+
chapter for a better understanding on voters.
1919

2020
How Symfony Uses Voters
2121
-----------------------
@@ -25,7 +25,7 @@ In general, all registered custom voters will be called every time you ask
2525
Symfony about permissions (ACL). You can use one of three different
2626
approaches on how to handle the feedback from all voters: affirmative,
2727
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>`".
2929

3030
The Voter Interface
3131
-------------------
@@ -37,7 +37,7 @@ which has this structure:
3737
.. include:: /cookbook/security/voter_interface.rst.inc
3838

3939
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
4141
the object). If the condition fails, you'll return
4242
``VoterInterface::ACCESS_DENIED``, otherwise you'll return
4343
``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``
4646
Creating the Custom Voter
4747
-------------------------
4848

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::
5050

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;
5353

5454
use Symfony\Component\Security\Core\Exception\InvalidArgumentException;
5555
use Symfony\Component\DependencyInjection\ContainerInterface;
5656
use Symfony\Component\Security\Core\Authorization\Voter\VoterInterface;
5757
use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
5858
use Symfony\Component\Security\Core\User\UserInterface;
59-
use Doctrine\Common\Util\ClassUtils;
59+
use Acme\DemoBundle\Entity\Post;
6060

6161
class PostVoter implements VoterInterface
6262
{
@@ -73,7 +73,9 @@ You could store your Voter to check permission for the view and edit action like
7373

7474
public function supportsClass($obj)
7575
{
76-
if ($obj instanceof 'Acme\DemoBundle\Entity\Post') return true;
76+
if ($obj instanceof Post) {
77+
return true;
78+
}
7779

7880
return false;
7981
}
@@ -137,27 +139,29 @@ Declaring the Voter as a Service
137139
--------------------------------
138140

139141
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``:
141143

142144
.. configuration-block::
143145

144146
.. code-block:: yaml
145147
146-
# src/Acme/AcmeBundle/Resources/config/services.yml
148+
# src/Acme/DemoBundle/Resources/config/services.yml
147149
services:
148150
security.access.post_voter:
149-
class: Acme\DemoBundle\Security\Authorization\Entity\PostVoter
151+
class: Acme\DemoBundle\Security\Authorization\Voter\PostVoter
150152
public: false
151153
tags:
152154
- { name: security.voter }
153155
154156
.. code-block:: xml
155157
156158
<?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">
158162
<services>
159163
<service id="security.access.post_document_voter"
160-
class="Acme\DemoBundle\Security\Authorization\Document\PostVoter"
164+
class="Acme\DemoBundle\Security\Authorization\Voter\PostVoter"
161165
public="false">
162166
<tag name="security.voter" />
163167
</service>
@@ -166,29 +170,32 @@ and tag it as a 'security.voter':
166170
167171
.. code-block:: php
168172
173+
// src/Acme/DemoBundle/Resources/config/services.php
169174
$container
170175
->register(
171176
'security.access.post_document_voter',
172-
'Acme\DemoBundle\Security\Authorization\Document\PostVoter'
177+
'Acme\DemoBundle\Security\Authorization\Voter\PostVoter'
173178
)
174179
->addTag('security.voter')
175180
;
176181
177182
How to Use the Voter in a Controller
178183
------------------------------------
179184

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()``
181186
from the security context is called.
182187

183188
.. code-block:: php
184189
185190
// src/Acme/DemoBundle/Controller/PostController.php
186191
namespace Acme\DemoBundle\Controller;
187192
193+
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
188194
use Symfony\Component\HttpFoundation\Response;
189195
use Symfony\Component\Security\Core\Exception\AccessDeniedException;
196+
use Acme\DemoBundle\Entity\Post;
190197
191-
class PostController
198+
class PostController extends Controller
192199
{
193200
194201
/**

0 commit comments

Comments
 (0)