Skip to content

Commit 816e562

Browse files
[Security] Document #[AsTaggedItem] support for voter priority
1 parent 6e36ec0 commit 816e562

File tree

1 file changed

+33
-2
lines changed

1 file changed

+33
-2
lines changed

security/voters.rst

Lines changed: 33 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -247,6 +247,37 @@ that's done automatically for you! When you
247247
:ref:`call isGranted() with view/edit and pass a Post object <how-to-use-the-voter-in-a-controller>`,
248248
your voter will be called and you can control access.
249249

250+
.. _security-voter-priority:
251+
252+
Setting the Voter Priority
253+
~~~~~~~~~~~~~~~~~~~~~~~~~~
254+
255+
.. versionadded:: 8.1
256+
257+
The ``#[AsTaggedItem]`` attribute can be used to define voter priority
258+
starting from Symfony 8.1.
259+
260+
Voters are called according to their service priority, with higher priority
261+
voters being called first. By default, this priority is ``0``. When using the
262+
:ref:`priority strategy <security-voters-change-strategy>`, use the
263+
``#[AsTaggedItem]`` attribute to define the execution order::
264+
265+
// src/Security/PostVoter.php
266+
namespace App\Security;
267+
268+
use Symfony\Component\DependencyInjection\Attribute\AsTaggedItem;
269+
use Symfony\Component\Security\Core\Authorization\Voter\Voter;
270+
271+
#[AsTaggedItem(priority: 10)]
272+
class PostVoter extends Voter
273+
{
274+
// ...
275+
}
276+
277+
This is the recommended way to handle voter priority because it integrates
278+
with autoconfiguration to set the priority without re-declaring the
279+
``security.voter`` tag.
280+
250281
Checking for Roles inside a Voter
251282
---------------------------------
252283

@@ -416,7 +447,7 @@ There are four strategies available:
416447

417448
``priority``
418449
This grants or denies access by the first voter that does not abstain,
419-
based on their service priority;
450+
based on their service priority (see :ref:`security-voter-priority`);
420451

421452
Regardless the chosen strategy, if all voters abstained from voting, the
422453
decision is based on the ``allow_if_all_abstain`` config option (which
@@ -557,4 +588,4 @@ must implement the :class:`Symfony\\Component\\Security\\Core\\Authorization\\Ac
557588
->service(MyCustomAccessDecisionManager::class)
558589
// ...
559590
;
560-
};
591+
};

0 commit comments

Comments
 (0)