Skip to content

Commit 1a7766f

Browse files
committed
Merge branch '3.4' into 4.2
* 3.4: [Security] Expression voter
2 parents cdbc26c + f6a5add commit 1a7766f

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+
grants access based on the evaluation of expressions created with the
165+
:doc:`ExpressionLanguage component </components/expression_language>`. These
166+
expressions have access to a number of
167+
:ref:`special security variables <security-expression-variables>`::
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, [$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)