@@ -13,8 +13,8 @@ application: :doc:`"/cookbook/security/voters"`.
13
13
14
14
.. tip ::
15
15
16
- It is good to understand the basics about what and how
17
- :doc: `authorization </components/security/authorization >` works. // correct link in book?
16
+ Have a look at the referenced page if you are not familiar with
17
+ :doc: `authorization </components/security/authorization >`.
18
18
19
19
How Symfony uses Voters
20
20
-----------------------
@@ -33,7 +33,9 @@ A custom voter must implement
33
33
:class: `Symfony\\ Component\\ Security\\ Core\\ Authorization\\ Voter\\ VoterInterface `,
34
34
which has this structure:
35
35
36
- .. code-block :: php // :: shortcut? and put the snippet (to line 56) in a single file an reference ?
36
+ // how to put this following snippet (to line 56) in a single file an embed it? as it is used in voters.rst as well.
37
+
38
+ .. code-block :: php
37
39
38
40
interface VoterInterface
39
41
{
@@ -95,10 +97,8 @@ You could store your Voter to check permission for the view and edit action like
95
97
$array = array('Acme\DemoBundle\Entity\Post');
96
98
97
99
foreach ($array as $item) {
98
- // check with stripos in case doctrine is using a proxy class for this object
99
- // if (stripos($s, $item) !== false) {
100
- if ($obj instanceof $item)) // check if this will also check for interfaces etc. like it should be in oop (inheritace)
101
- // or return $targetClass === $class || is_subclass_of($class, $targetClass);
100
+ if ($obj instanceof $item))
101
+
102
102
return true;
103
103
}
104
104
}
@@ -107,16 +107,21 @@ You could store your Voter to check permission for the view and edit action like
107
107
}
108
108
109
109
/** @var \Acme\DemoBundle\Entity\Post $post */
110
- public function vote(TokenInterface $token, $post, array $attributes) // remove array
110
+ public function vote(TokenInterface $token, $post, array $attributes)
111
111
{
112
- // always get the first attribute
112
+ // check if voter is used correct, only allow one attribute for a check
113
+ if(count($attributes) !== 1 || !is_string($attributes[0])) {
114
+ throw new PreconditionFailedHttpException('The Attribute was not set correct. Maximum 1 attribute.');
115
+ }
116
+
117
+ // set the attribute to check against
113
118
$attribute = $attributes[0];
114
119
115
120
// get current logged in user
116
121
$user = $token->getUser();
117
122
118
123
// check if class of this object is supported by this voter
119
- if (!($this->supportsClass($post))) { // maybe without ClassUtils::getRealClass(
124
+ if (!($this->supportsClass($post))) {
120
125
121
126
return VoterInterface::ACCESS_ABSTAIN;
122
127
}
@@ -151,8 +156,8 @@ You could store your Voter to check permission for the view and edit action like
151
156
break;
152
157
153
158
default:
154
- // otherwise throw an exception
155
- throw new PreconditionFailedHttpException('The Attribute "'.$attribute.'"" was not found.')
159
+ // otherwise throw an exception, which will break the request
160
+ throw new PreconditionFailedHttpException('The Attribute "'.$attribute.'" was not found.')
156
161
}
157
162
158
163
}
0 commit comments