@@ -38,10 +38,11 @@ The Voter Interface
38
38
-------------------
39
39
40
40
A custom voter must implement
41
- :class: `Symfony\\ Component\\ Security\\ Core\\ Authorization\\ Voter\\ VoterInterface `,
42
- which has this structure:
41
+ :class: `Symfony\\ Component\\ Security\\ Core\\ Authorization\\ Voter\\ VoterInterface `
42
+ and an :class: `Symfony\\ Component\\ Security\\ Core\\ Authorization\\ Voter\\ AbstractVoter `
43
+ class is provided with following structure:
43
44
44
- .. include :: /cookbook/security/voter_interface .rst.inc
45
+ .. include :: /cookbook/security/abstract_voter .rst.inc
45
46
46
47
In this example, the voter will check if the user has access to a specific
47
48
object according to your custom conditions (e.g. they must be the owner of
@@ -61,84 +62,45 @@ edit a particular object. Here's an example implementation:
61
62
// src/Acme/DemoBundle/Security/Authorization/Voter/PostVoter.php
62
63
namespace Acme\DemoBundle\Security\Authorization\Voter;
63
64
64
- use Symfony\Component\Security\Core\Authorization\Voter\VoterInterface ;
65
+ use Symfony\Component\Security\Core\Authorization\Voter\AbstractVoter ;
65
66
use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
66
67
use Symfony\Component\Security\Core\User\UserInterface;
67
68
68
- class PostVoter implements VoterInterface
69
+ class PostVoter extends AbstractVoter
69
70
{
70
71
const VIEW = 'view';
71
72
const EDIT = 'edit';
72
73
73
- public function supportsAttribute($attribute )
74
+ protected function getSupportedAttributes( )
74
75
{
75
- return in_array($attribute, array(
76
- self::VIEW,
77
- self::EDIT,
78
- ));
76
+ return array(self::VIEW, self::EDIT);
79
77
}
80
78
81
- public function supportsClass($class )
79
+ protected function getSupportedClasses( )
82
80
{
83
- $supportedClass = 'Acme\DemoBundle\Entity\Post';
84
-
85
- return $supportedClass === $class || is_subclass_of($class, $supportedClass);
81
+ return array('Acme\DemoBundle\Entity\Post');
86
82
}
87
83
88
- /**
89
- * @var \Acme\DemoBundle\Entity\Post $post
90
- */
91
- public function vote(TokenInterface $token, $post, array $attributes)
84
+ protected function isGranted($attribute, $post, $user = null)
92
85
{
93
- // check if class of this object is supported by this voter
94
- if (!$this->supportsClass(get_class($post))) {
95
- return VoterInterface::ACCESS_ABSTAIN;
96
- }
97
-
98
- // check if the voter is used correct, only allow one attribute
99
- // this isn't a requirement, it's just one easy way for you to
100
- // design your voter
101
- if(1 !== count($attributes)) {
102
- throw new \InvalidArgumentException(
103
- 'Only one attribute is allowed for VIEW or EDIT'
104
- );
105
- }
106
-
107
- // set the attribute to check against
108
- $attribute = $attributes[0];
109
-
110
- // check if the given attribute is covered by this voter
111
- if (!$this->supportsAttribute($attribute)) {
112
- return VoterInterface::ACCESS_ABSTAIN;
113
- }
114
-
115
- // get current logged in user
116
- $user = $token->getUser();
117
-
118
86
// make sure there is a user object (i.e. that the user is logged in)
119
87
if (!$user instanceof UserInterface) {
120
- return VoterInterface::ACCESS_DENIED;
88
+ return false;
89
+ }
90
+
91
+ // the data object could have for example a method isPrivate()
92
+ // which checks the Boolean attribute $private
93
+ if ($attribute == self::VIEW && !$post->isPrivate()) {
94
+ return true;
121
95
}
122
96
123
- switch($attribute) {
124
- case self::VIEW:
125
- // the data object could have for example a method isPrivate()
126
- // which checks the Boolean attribute $private
127
- if (!$post->isPrivate()) {
128
- return VoterInterface::ACCESS_GRANTED;
129
- }
130
- break;
131
-
132
- case self::EDIT:
133
- // we assume that our data object has a method getOwner() to
134
- // get the current owner user entity for this data object
135
- if ($user->getId() === $post->getOwner()->getId()) {
136
- return VoterInterface::ACCESS_GRANTED;
137
- }
138
- break;
97
+ // we assume that our data object has a method getOwner() to
98
+ // get the current owner user entity for this data object
99
+ if ($attribute == self::EDIT && $user->getId() === $post->getOwner()->getId()) {
100
+ return true;
139
101
}
140
102
141
- return VoterInterface::ACCESS_DENIED ;
103
+ return false ;
142
104
}
143
105
}
144
106
0 commit comments