@@ -46,7 +46,7 @@ which makes creating a voter even easier.
46
46
abstract protected function voteOnAttribute($attribute, $subject, TokenInterface $token);
47
47
}
48
48
49
- .. versionadded ::
49
+ .. versionadded :: 2.8
50
50
The ``Voter `` helper class was added in Symfony 2.8. In earlier versions, an
51
51
``AbstractVoter `` class with similar behavior was available.
52
52
@@ -150,7 +150,7 @@ would look like this::
150
150
return false;
151
151
}
152
152
153
- // we know $subject is a Post object, thanks to supports
153
+ // you know $subject is a Post object, thanks to supports
154
154
/** @var Post $post */
155
155
$post = $subject;
156
156
@@ -172,7 +172,7 @@ would look like this::
172
172
}
173
173
174
174
// the Post object could have, for example, a method isPrivate()
175
- // that checks a Boolean $private property
175
+ // that checks a boolean $private property
176
176
return !$post->isPrivate();
177
177
}
178
178
@@ -191,7 +191,7 @@ To recap, here's what's expected from the two abstract methods:
191
191
``Voter::supports($attribute, $subject) ``
192
192
When ``isGranted() `` (or ``denyAccessUnlessGranted() ``) is called, the first
193
193
argument is passed here as ``$attribute `` (e.g. ``ROLE_USER ``, ``edit ``) and
194
- the second argument (if any) is passed as ``` $subject `` (e.g. ``null ``, a ``Post ``
194
+ the second argument (if any) is passed as ``$subject `` (e.g. ``null ``, a ``Post ``
195
195
object). Your job is to determine if your voter should vote on the attribute/subject
196
196
combination. If you return true, ``voteOnAttribute() `` will be called. Otherwise,
197
197
your voter is done: some other voter should process this. In this example, you
@@ -222,6 +222,8 @@ and tag it with ``security.voter``:
222
222
class : AppBundle\Security\PostVoter
223
223
tags :
224
224
- { name: security.voter }
225
+ # small performance boost
226
+ public : false
225
227
226
228
.. code-block :: xml
227
229
@@ -234,7 +236,7 @@ and tag it with ``security.voter``:
234
236
235
237
<services >
236
238
<service id =" app.post_voter"
237
- class =" AppBundle\Security\Authorization\Voter\ PostVoter"
239
+ class =" AppBundle\Security\PostVoter"
238
240
public =" false"
239
241
>
240
242
@@ -248,7 +250,7 @@ and tag it with ``security.voter``:
248
250
// app/config/services.php
249
251
use Symfony\Component\DependencyInjection\Definition;
250
252
251
- $container->register('app.post_voter', 'AppBundle\Security\Authorization\Voter\ PostVoter')
253
+ $container->register('app.post_voter', 'AppBundle\Security\PostVoter')
252
254
->setPublic(false)
253
255
->addTag('security.voter')
254
256
;
@@ -265,14 +267,15 @@ Checking for Roles inside a Voter
265
267
``service_container `` itself and fetch out the ``security.authorization_checker ``
266
268
to use ``isGranted() ``.
267
269
268
- What if you want to call ``isGranted() `` fomr *inside * your voter - e.g. you want
270
+ What if you want to call ``isGranted() `` from *inside * your voter - e.g. you want
269
271
to see if the current user has ``ROLE_SUPER_ADMIN ``. That's possible by injecting
270
- the ``AccessDecisionManager `` into your voter. You can use this to, for example,
271
- *always * allow access to a user with ``ROLE_SUPER_ADMIN ``::
272
+ the :class: `Symfony\\ Component\\ Security\\ Core\\ Authorization\\ AccessDecisionManager `
273
+ into your voter. You can use this to, for example, *always * allow access to a user
274
+ with ``ROLE_SUPER_ADMIN ``::
272
275
273
276
// src/AppBundle/Security/PostVoter.php
274
- // ...
275
277
278
+ // ...
276
279
use Symfony\Component\Security\Core\Authorization\AccessDecisionManagerInterface;
277
280
278
281
class PostVoter extends Voter
@@ -311,6 +314,7 @@ service:
311
314
app.post_voter :
312
315
class : AppBundle\Security\PostVoter
313
316
arguments : ['@security.access.decision_manager']
317
+ public : false
314
318
tags :
315
319
- { name: security.voter }
316
320
@@ -325,7 +329,7 @@ service:
325
329
326
330
<services >
327
331
<service id =" app.post_voter"
328
- class =" AppBundle\Security\Authorization\Voter\ PostVoter"
332
+ class =" AppBundle\Security\PostVoter"
329
333
public =" false"
330
334
>
331
335
<argument type =" service" id =" security.access.decision_manager" />
@@ -341,15 +345,15 @@ service:
341
345
use Symfony\Component\DependencyInjection\Definition;
342
346
use Symfony\Component\DependencyInjection\Reference;
343
347
344
- $container->register('app.post_voter', 'AppBundle\Security\Authorization\Voter\ PostVoter')
348
+ $container->register('app.post_voter', 'AppBundle\Security\PostVoter')
345
349
->addArgument(new Reference('security.access.decision_manager'))
346
350
->setPublic(false)
347
351
->addTag('security.voter')
348
352
;
349
353
350
354
That's it! Calling ``decide() `` on the ``AccessDecisionManager `` is essentially
351
- the same as calling ``isGranted() `` on the normal `` security.authorization_checker ``
352
- service (it's just a little lower-level, which is necessary for a voter).
355
+ the same as calling ``isGranted() `` from a controller or other places
356
+ (it's just a little lower-level, which is necessary for a voter).
353
357
354
358
.. note ::
355
359
0 commit comments