Skip to content

Commit 2f0330c

Browse files
committed
minor #10236 [Security] Expression voter (alavieille)
This PR was submitted for the master branch but it was squashed and merged into the 3.4 branch instead (closes #10236). Discussion ---------- [Security] Expression voter Adding missing documentation of expression voter Commits ------- 3f458e7 [Security] Expression voter
2 parents b327acb + 3f458e7 commit 2f0330c

File tree

1 file changed

+35
-0
lines changed

1 file changed

+35
-0
lines changed

components/security/authorization.rst

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -157,6 +157,41 @@ role::
157157

158158
$roleHierarchyVoter = new RoleHierarchyVoter($roleHierarchy);
159159

160+
ExpressionVoter
161+
~~~~~~~~~~~~~~~
162+
163+
The :class:`Symfony\\Component\\Security\\Core\\Authorization\\Voter\\ExpressionVoter`
164+
supports :class:`Symfony\\Component\\ExpressionLanguage\\Expression` attributes
165+
and grants access based on the evaluation of expression (see :doc:`/security/expressions` )
166+
167+
.. code-block:: php
168+
169+
use Symfony\Component\ExpressionLanguage\Expression;
170+
use Symfony\Component\Security\Core\Authorization\Voter\ExpressionVoter;
171+
172+
// Symfony\Component\Security\Core\Authorization\ExpressionLanguage;
173+
$expressionLanguage = ...;
174+
175+
// instance of Symfony\Component\Security\Core\Authentication\AuthenticationTrustResolverInterface
176+
$trustResolver = ...;
177+
178+
// Symfony\Component\Security\Core\Authorization\AuthorizationCheckerInterface
179+
$authorizationChecker = ...;
180+
181+
$expressionVoter = new ExpressionVoter($expressionLanguage, $trustResolver, $authorizationChecker);
182+
183+
// instance of Symfony\Component\Security\Core\Authentication\Token\TokenInterface
184+
$token = ...;
185+
186+
// any object
187+
$object = ...;
188+
189+
$expression = new Expression(
190+
'"ROLE_ADMIN" in roles or (not is_anonymous() and user.isSuperAdmin())'
191+
)
192+
193+
$vote = $expressionVoter->vote($token, $object, array($expression));
194+
160195
.. note::
161196

162197
When you make your own voter, you can use its constructor to inject any

0 commit comments

Comments
 (0)