@@ -37,12 +37,19 @@ For more information take a look at
37
37
The Voter Interface
38
38
-------------------
39
39
40
- A custom voter must implement
40
+ A custom voter needs to implement
41
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:
42
+ or extend :class: `Symfony\\ Component\\ Security\\ Core\\ Authorization\\ Voter\\ AbstractVoter `,
43
+ which makes creating a voter even easier.
44
44
45
- .. include :: /cookbook/security/abstract_voter.rst.inc
45
+ .. code-block :: php
46
+
47
+ abstract class AbstractVoter implements VoterInterface
48
+ {
49
+ abstract protected function getSupportedClasses();
50
+ abstract protected function getSupportedAttributes();
51
+ abstract protected function isGranted($attribute, $object, $user = null);
52
+ }
46
53
47
54
In this example, the voter will check if the user has access to a specific
48
55
object according to your custom conditions (e.g. they must be the owner of
@@ -106,6 +113,22 @@ edit a particular object. Here's an example implementation:
106
113
That's it! The voter is done. The next step is to inject the voter into
107
114
the security layer.
108
115
116
+ To recap, here's what's expected from the three abstract methods:
117
+
118
+ The :method: `Symfony\\ Component\\ Security\\ Core\\ Authorization\\ Voter\\ AbstractVoter::getSupportedClasses `
119
+ method tells Symfony that your voter should be called whenever an object of one of the given classes
120
+ is passed to `isGranted ` For example, if you return ['\A cme\D emoBundle\M odel\P roduct'],
121
+ Symfony will call your voter when a `Product ` object is passed to `isGranted `.
122
+
123
+ The :method: `Symfony\\ Component\\ Security\\ Core\\ Authorization\\ Voter\\ AbstractVoter::getSupportedAttributes `
124
+ method tells Symfony that your voter should be called whenever one of these strings is passes as the
125
+ first argument to `isGranted `. For example, if you return `array('CREATE', 'READ') `, then
126
+ Symfony will call your voter when one of these is passed to `isGranted `.
127
+
128
+ The :method: `Symfony\\ Component\\ Security\\ Core\\ Authorization\\ Voter\\ AbstractVoter::isGranted `
129
+ method must implement the business logic that verifies whether or not a given
130
+ user is allowed access to a given attribute on a given object. This method must return a boolean.
131
+
109
132
Declaring the Voter as a Service
110
133
--------------------------------
111
134
0 commit comments