.. index:: single: Security; Data Permission Voters
In Symfony, you can check the permission to access data by using the :doc:`ACL module </security/acl>`, which is a bit overwhelming for many applications. A much easier solution is to work with custom voters, which are like simple conditional statements.
Tip
Take a look at the :doc:`authorization </components/security/authorization>` article for an even deeper understanding on voters.
In order to use voters, you have to understand how Symfony works with them.
All voters are called each time you use the isGranted()
method on Symfony's
authorization checker or call denyAccessUnlessGranted
in a controller (which
uses the authorization checker).
Ultimately, Symfony takes the responses from all voters and makes the final decision (to allow or deny access to the resource) according to the strategy defined in the application, which can be: affirmative, consensus or unanimous.
For more information take a look at :ref:`the section about access decision managers <components-security-access-decision-manager>`.
A custom voter needs to implement :class:`Symfony\\Component\\Security\\Core\\Authorization\\Voter\\VoterInterface` or extend :class:`Symfony\\Component\\Security\\Core\\Authorization\\Voter\\Voter`, which makes creating a voter even easier.
abstract class Voter implements VoterInterface
{
abstract protected function supports($attribute, $subject);
abstract protected function voteOnAttribute($attribute, $subject, TokenInterface $token);
}
Suppose you have a Post
object and you need to decide whether or not the current
user can edit or view the object. In your controller, you'll check access with
code like this:
// src/AppBundle/Controller/PostController.php // ... class PostController extends Controller { /** * @Route("/posts/{id}", name="post_show") */ public function showAction($id) { // get a Post object - e.g. query for it $post = ...; // check for "view" access: calls all voters $this->denyAccessUnlessGranted('view', $post); // ... } /** * @Route("/posts/{id}/edit", name="post_edit") */ public function editAction($id) { // get a Post object - e.g. query for it $post = ...; // check for "edit" access: calls all voters $this->denyAccessUnlessGranted('edit', $post); // ... } }
The denyAccessUnlessGranted()
method (and also the isGranted()
method)
calls out to the "voter" system. Right now, no voters will vote on whether or not
the user can "view" or "edit" a Post
. But you can create your own voter that
decides this using whatever logic you want.
Suppose the logic to decide if a user can "view" or "edit" a Post
object is
pretty complex. For example, a User
can always edit or view a Post
they created.
And if a Post
is marked as "public", anyone can view it. A voter for this situation
would look like this:
// src/AppBundle/Security/PostVoter.php namespace AppBundle\Security; use AppBundle\Entity\Post; use AppBundle\Entity\User; use Symfony\Component\Security\Core\Authentication\Token\TokenInterface; use Symfony\Component\Security\Core\Authorization\Voter\Voter; class PostVoter extends Voter { // these strings are just invented: you can use anything const VIEW = 'view'; const EDIT = 'edit'; protected function supports($attribute, $subject) { // if the attribute isn't one we support, return false if (!in_array($attribute, array(self::VIEW, self::EDIT))) { return false; } // only vote on Post objects inside this voter if (!$subject instanceof Post) { return false; } return true; } protected function voteOnAttribute($attribute, $subject, TokenInterface $token) { $user = $token->getUser(); if (!$user instanceof User) { // the user must be logged in; if not, deny access return false; } // you know $subject is a Post object, thanks to supports /** @var Post $post */ $post = $subject; switch ($attribute) { case self::VIEW: return $this->canView($post, $user); case self::EDIT: return $this->canEdit($post, $user); } throw new \LogicException('This code should not be reached!'); } private function canView(Post $post, User $user) { // if they can edit, they can view if ($this->canEdit($post, $user)) { return true; } // the Post object could have, for example, a method isPrivate() // that checks a boolean $private property return !$post->isPrivate(); } private function canEdit(Post $post, User $user) { // this assumes that the data object has a getOwner() method // to get the entity of the user who owns this data object return $user === $post->getOwner(); } }
That's it! The voter is done! Next, :ref:`configure it <declaring-the-voter-as-a-service>`.
To recap, here's what's expected from the two abstract methods:
Voter::supports($attribute, $subject)
- When
isGranted()
(ordenyAccessUnlessGranted()
) is called, the first argument is passed here as$attribute
(e.g.ROLE_USER
,edit
) and the second argument (if any) is passed as$subject
(e.g.null
, aPost
object). Your job is to determine if your voter should vote on the attribute/subject combination. If you return true,voteOnAttribute()
will be called. Otherwise, your voter is done: some other voter should process this. In this example, you returntrue
if the attribue isview
oredit
and if the object is aPost
instance. voteOnAttribute($attribute, $subject, TokenInterface $token)
- If you return
true
fromsupports()
, then this method is called. Your job is simple: returntrue
to allow access andfalse
to deny access. The$token
can be used to find the current user object (if any). In this example, all of the complex business logic is included to determine access.
To inject the voter into the security layer, you must declare it as a service
and tag it with security.voter
. But if you're using the
:ref:`default services.yml configuration <service-container-services-load-example>`,
that's done automatically for you! When you
:ref:`call isGranted() with view/edit and pass a Post object <how-to-use-the-voter-in-a-controller>`,
your voter will be executed and you can control access.
What if you want to call isGranted()
from inside your voter - e.g. you want
to see if the current user has ROLE_SUPER_ADMIN
. That's possible by injecting
the :class:`Symfony\\Component\\Security\\Core\\Authorization\\AccessDecisionManager`
into your voter. You can use this to, for example, always allow access to a user
with ROLE_SUPER_ADMIN
:
// src/AppBundle/Security/PostVoter.php // ... use Symfony\Component\Security\Core\Authorization\AccessDecisionManagerInterface; class PostVoter extends Voter { // ... private $decisionManager; public function __construct(AccessDecisionManagerInterface $decisionManager) { $this->decisionManager = $decisionManager; } protected function voteOnAttribute($attribute, $subject, TokenInterface $token) { // ... // ROLE_SUPER_ADMIN can do anything! The power! if ($this->decisionManager->decide($token, array('ROLE_SUPER_ADMIN'))) { return true; } // ... all the normal voter logic } }
If you're using the :ref:`default services.yml configuration <service-container-services-load-example>`,
you're done! Symfony will automatically pass the security.access.decision_manager
service when instantiating your voter (thanks to autowiring).
Calling decide()
on the AccessDecisionManager
is essentially the same as
calling isGranted()
from a controller or other places
(it's just a little lower-level, which is necessary for a voter).
Note
If you need to check access in any non-voter service, use the security.authorization_checker
service (i.e. type-hint Symfony\Component\Security\Core\Authorization\AuthorizationCheckerInterface
)
instead of the security.access.decision_manager
service shown here.
Normally, only one voter will vote at any given time (the rest will "abstain", which
means they return false
from supports()
). But in theory, you could make multiple
voters vote for one action and object. For instance, suppose you have one voter that
checks if the user is a member of the site and a second one that checks if the user
is older than 18.
To handle these cases, the access decision manager uses an access decision strategy. You can configure this to suit your needs. There are three strategies available:
affirmative
(default)- This grants access as soon as there is one voter granting access;
consensus
- This grants access if there are more voters granting access than denying;
unanimous
- This only grants access once all voters grant access.
In the above scenario, both voters should grant access in order to grant access
to the user to read the post. In this case, the default strategy is no longer
valid and unanimous
should be used instead. You can set this in the
security configuration:
.. configuration-block:: .. code-block:: yaml # app/config/security.yml security: access_decision_manager: strategy: unanimous .. code-block:: xml <!-- app/config/security.xml --> <?xml version="1.0" encoding="UTF-8" ?> <srv:container xmlns="http://symfony.com/schema/dic/security" xmlns:srv="http://symfony.com/schema/dic/services" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://symfony.com/schema/dic/services http://symfony.com/schema/dic/services/services-1.0.xsd" > <config> <access-decision-manager strategy="unanimous" /> </config> </srv:container> .. code-block:: php // app/config/security.php $container->loadFromExtension('security', array( 'access_decision_manager' => array( 'strategy' => 'unanimous', ), ));