Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit1fd3b0e

Browse files
Michael Kleinweaverryan
Michael Klein
authored andcommitted
updated docs according to the review
1 parent5275230 commit1fd3b0e

File tree

2 files changed

+31
-33
lines changed

2 files changed

+31
-33
lines changed

‎cookbook/security/voter_interface.rst.inc

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,15 +7,16 @@
77
public function vote(TokenInterface $token, $post, array $attributes);
88
}
99

10-
The :method:`Symfony\\Component\\Security\\Core\\Authorization\\Voter\\VoterInterface::supportsAttribute` method is used to check if the voter supports
11-
the given user attribute (i.e: a role, an ACL, etc.).
10+
The :method:`Symfony\\Component\\Security\\Core\\Authorization\\Voter\\VoterInterface::supportsAttribute`
11+
method is used to check if the voter supportsthe given user attribute (i.e: a role, an ACL, etc.).
1212

13-
The :method:`Symfony\\Component\\Security\\Core\\Authorization\\Voter\\VoterInterface::supportsClass` method is used to check if the voter supports the
14-
class of the object whose access is being checked (doesn't apply to this entry).
13+
The :method:`Symfony\\Component\\Security\\Core\\Authorization\\Voter\\VoterInterface::supportsClass`
14+
method is used to check if the voter supports the class of the object whose
15+
access is being checked (doesn't apply to this entry).
1516

16-
The :method:`Symfony\\Component\\Security\\Core\\Authorization\\Voter\\VoterInterface::vote` method must implement the business logic that verifies whether
17-
or nottheuser is granted access. This method must return one ofthe following
18-
values:
17+
The :method:`Symfony\\Component\\Security\\Core\\Authorization\\Voter\\VoterInterface::vote`
18+
method must implementthebusiness logic that verifies whether or notthe
19+
user is granted access. This method must return one of the followingvalues:
1920

2021
* ``VoterInterface::ACCESS_GRANTED``: The authorization will be granted by this voter;
2122
* ``VoterInterface::ACCESS_ABSTAIN``: The voter cannot decide if authorization should be granted;

‎cookbook/security/voters_data_permission.rst

Lines changed: 23 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ You could store your Voter to check permission for the view and edit action like
5151
// src/Acme/DemoBundle/Security/Authorization/Entity/PostVoter.php
5252
namespace Acme\DemoBundle\Security\Authorization\Entity;
5353

54-
use Symfony\Component\HttpKernel\Exception\PreconditionFailedHttpException;
54+
use Symfony\Component\Security\Core\Exception\InvalidArgumentException;
5555
use Symfony\Component\DependencyInjection\ContainerInterface;
5656
use Symfony\Component\Security\Core\Authorization\Voter\VoterInterface;
5757
use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
@@ -60,33 +60,35 @@ You could store your Voter to check permission for the view and edit action like
6060

6161
class PostVoter implements VoterInterface
6262
{
63+
const VIEW = 'view';
64+
const EDIT = 'edit';
65+
6366
public function supportsAttribute($attribute)
6467
{
6568
return in_array($attribute, array(
66-
'view',
67-
'edit',
69+
self::VIEW,
70+
self::EDIT,
6871
));
6972
}
7073

7174
public function supportsClass($obj)
7275
{
73-
$array = array('Acme\DemoBundle\Entity\Post');
74-
75-
foreach ($array as $item) {
76-
if ($obj instanceof $item))
77-
return true;
78-
}
79-
}
76+
if ($obj instanceof 'Acme\DemoBundle\Entity\Post') return true;
8077

8178
return false;
8279
}
8380

8481
/** @var \Acme\DemoBundle\Entity\Post $post */
8582
public function vote(TokenInterface $token, $post, array $attributes)
8683
{
84+
// check if class of this object is supported by this voter
85+
if (!$this->supportsClass($post)) {
86+
return VoterInterface::ACCESS_ABSTAIN;
87+
}
88+
8789
// check if voter is used correct, only allow one attribute for a check
8890
if(count($attributes) !== 1 || !is_string($attributes[0])) {
89-
throw newPreconditionFailedHttpException(
91+
throw newInvalidArgumentException(
9092
'Only one attribute is allowed for VIEW or EDIT'
9193
);
9294
}
@@ -97,11 +99,6 @@ You could store your Voter to check permission for the view and edit action like
9799
// get current logged in user
98100
$user = $token->getUser();
99101

100-
// check if class of this object is supported by this voter
101-
if (!$this->supportsClass($post)) {
102-
return VoterInterface::ACCESS_ABSTAIN;
103-
}
104-
105102
// check if the given attribute is covered by this voter
106103
if (!$this->supportsAttribute($attribute)) {
107104
return VoterInterface::ACCESS_ABSTAIN;
@@ -128,12 +125,6 @@ You could store your Voter to check permission for the view and edit action like
128125
return VoterInterface::ACCESS_GRANTED;
129126
}
130127
break;
131-
132-
default:
133-
// otherwise throw an exception, which will break the request
134-
throw new PreconditionFailedHttpException(
135-
'The Attribute "'.$attribute.'" was not found.'
136-
);
137128
}
138129

139130
}
@@ -146,7 +137,7 @@ Declaring the Voter as a Service
146137
--------------------------------
147138

148139
To inject the voter into the security layer, you must declare it as a service
149-
and tag it as a´security.voter´:
140+
and tag it as a'security.voter':
150141

151142
..configuration-block::
152143

@@ -185,8 +176,9 @@ and tag it as a ´security.voter´:
185176
186177
How to Use the Voter in a Controller
187178
------------------------------------
188-
The registered voter will then always be asked as soon the method isGranted from
189-
the security context is called.
179+
180+
The registered voter will then always be asked as soon as the method 'isGranted'
181+
from the security context is called.
190182

191183
..code-block::php
192184
@@ -198,7 +190,12 @@ the security context is called.
198190
199191
class PostController
200192
{
201-
public function showAction($id)
193+
194+
/**
195+
* @Route("/blog/{id}")
196+
* @ParamConverter("post", class="SensioBlogBundle:Post")
197+
*/
198+
public function showAction(Post $post)
202199
{
203200
// keep in mind, this will call all registered security voters
204201
if (false === $this->get('security.context')->isGranted('view', $post)) {

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp