Skip to content

[Security] Expression voter #10236

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 35 additions & 0 deletions components/security/authorization.rst
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,41 @@ role::

$roleHierarchyVoter = new RoleHierarchyVoter($roleHierarchy);

ExpressionVoter
~~~~~~~~~~~~~~~

The :class:`Symfony\\Component\\Security\\Core\\Authorization\\Voter\\ExpressionVoter`
supports :class:`Symfony\\Component\\ExpressionLanguage\\Expression` attributes
and grants access based on the evaluation of expression (see :doc:`/security/expressions` )

.. code-block:: php

use Symfony\Component\ExpressionLanguage\Expression;
use Symfony\Component\Security\Core\Authorization\Voter\ExpressionVoter;

// Symfony\Component\Security\Core\Authorization\ExpressionLanguage;
$expressionLanguage = ...;

// instance of Symfony\Component\Security\Core\Authentication\AuthenticationTrustResolverInterface
$trustResolver = ...;

// Symfony\Component\Security\Core\Authorization\AuthorizationCheckerInterface
$authorizationChecker = ...;

$expressionVoter = new ExpressionVoter($expressionLanguage, $trustResolver, $authorizationChecker);

// instance of Symfony\Component\Security\Core\Authentication\Token\TokenInterface
$token = ...;

// any object
$object = ...;

$expression = new Expression(
'"ROLE_ADMIN" in roles or (not is_anonymous() and user.isSuperAdmin())'
)

$vote = $expressionVoter->vote($token, $object, array($expression));
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Replace array by []

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should be merged
It is really a component worth knowing :)


.. note::

When you make your own voter, you can use its constructor
Expand Down