Skip to content

Commit 6ba75a4

Browse files
committed
security add expression voter
1 parent 34e2cd2 commit 6ba75a4

File tree

1 file changed

+34
-0
lines changed

1 file changed

+34
-0
lines changed

components/security/authorization.rst

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -159,6 +159,40 @@ role::
159159

160160
$roleHierarchyVoter = new RoleHierarchyVoter($roleHierarchy);
161161

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

164198
When you make your own voter, you may of course use its constructor

0 commit comments

Comments
 (0)