-
-
Notifications
You must be signed in to change notification settings - Fork 5.2k
Update voter section of best practices #5926
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -264,37 +264,66 @@ the same ``getAuthorEmail`` logic you used above: | |
|
||
namespace AppBundle\Security; | ||
|
||
use Symfony\Component\Security\Core\Authorization\Voter\AbstractVoter; | ||
use Symfony\Component\Security\Core\Authentication\Token\TokenInterface; | ||
use Symfony\Component\Security\Core\Authorization\AccessDecisionManagerInterface; | ||
use Symfony\Component\Security\Core\Authorization\Voter\Voter; | ||
use Symfony\Component\Security\Core\User\UserInterface; | ||
use AppBundle\Entity\Post; | ||
|
||
// AbstractVoter class requires Symfony 2.6 or higher version | ||
class PostVoter extends AbstractVoter | ||
// Voter class requires Symfony 2.8 or higher version | ||
class PostVoter extends Voter | ||
{ | ||
const CREATE = 'create'; | ||
const EDIT = 'edit'; | ||
|
||
protected function getSupportedAttributes() | ||
/** | ||
* @var AccessDecisionManagerInterface | ||
*/ | ||
private $decisionManager; | ||
|
||
public function __construct(AccessDecisionManagerInterface $decisionManager) | ||
{ | ||
return array(self::CREATE, self::EDIT); | ||
$this->decisionManager = $decisionManager; | ||
} | ||
|
||
protected function getSupportedClasses() | ||
protected function supports($attribute, $subject) | ||
{ | ||
return array('AppBundle\Entity\Post'); | ||
if (!in_array($attribute, array(self::CREATE, self::EDIT))) { | ||
return false; | ||
} | ||
|
||
if (!$subject instanceof Post) { | ||
return false; | ||
} | ||
|
||
return true; | ||
} | ||
|
||
protected function isGranted($attribute, $post, $user = null) | ||
protected function voteOnAttribute($attribute, $subject, TokenInterface $token) | ||
{ | ||
$user = $token->getUser(); | ||
/** @var Post */ | ||
$post = $subject; // $subject must be a Post instance, thanks to the supports method | ||
|
||
if (!$user instanceof UserInterface) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. same here (for |
||
return false; | ||
} | ||
|
||
if ($attribute === self::CREATE && in_array('ROLE_ADMIN', $user->getRoles(), true)) { | ||
return true; | ||
} | ||
|
||
if ($attribute === self::EDIT && $user->getEmail() === $post->getAuthorEmail()) { | ||
return true; | ||
switch ($attribute) { | ||
case self::CREATE: | ||
// if the user is an admin, allow them to create new posts | ||
if ($this->decisionManager->decide($token, array('ROLE_ADMIN'))) { | ||
return true; | ||
} | ||
|
||
break; | ||
case self::EDIT: | ||
// if the user is the author of the post, allow them to edit the posts | ||
if ($user->getEmail() === $post->getAuthorEmail()) { | ||
return true; | ||
} | ||
|
||
break; | ||
} | ||
|
||
return false; | ||
|
@@ -310,6 +339,7 @@ To enable the security voter in the application, define a new service: | |
# ... | ||
post_voter: | ||
class: AppBundle\Security\PostVoter | ||
arguments: ['@security.access.decision_manager'] | ||
public: false | ||
tags: | ||
- { name: security.voter } | ||
|
@@ -337,7 +367,7 @@ via the even easier shortcut in a controller: | |
*/ | ||
public function editAction($id) | ||
{ | ||
$post = // query for the post ... | ||
$post = ...; // query for the post | ||
|
||
$this->denyAccessUnlessGranted('edit', $post); | ||
|
||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We need to test first if
$subject
really is an object.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That's not needed, instanceof with a non-object simply evaluates to
false
: https://3v4l.org/FJqoI