-
-
Notifications
You must be signed in to change notification settings - Fork 1.7k
Add a security Post Voter #442
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Symfony package. | ||
* | ||
* (c) Fabien Potencier <[email protected]> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace AppBundle\Security; | ||
|
||
use AppBundle\Entity\Post; | ||
use AppBundle\Entity\User; | ||
use Symfony\Component\Security\Core\Authentication\Token\TokenInterface; | ||
use Symfony\Component\Security\Core\Authorization\Voter\Voter; | ||
|
||
/** | ||
* It grants or denies permissions for actions related to blog posts (such as | ||
* showing, editing and deleting posts). | ||
* | ||
* See http://symfony.com/doc/current/security/voters.html | ||
* | ||
* @author Yonel Ceruto <[email protected]> | ||
*/ | ||
class PostVoter extends Voter | ||
{ | ||
// Defining these constants is overkill for this simple application, but for real | ||
// applications, it's a recommended practice to avoid relying on "magic strings" | ||
const SHOW = 'show'; | ||
const EDIT = 'edit'; | ||
const DELETE = 'delete'; | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
protected function supports($attribute, $subject) | ||
{ | ||
// this voter is only executed for three specific permissions on Post objects | ||
return $subject instanceof Post && in_array($attribute, [self::SHOW, self::EDIT, self::DELETE]); | ||
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. I've simplified this method to a 1-liner to reduce the perceived complexity of voters. They have been traditionally criticized for the required boilerplate ... and thanks to the new abstract voter, it's code can be super simple. |
||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
protected function voteOnAttribute($attribute, $post, TokenInterface $token) | ||
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. This implementation can be simplified too, just one line:
? because 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. What you say is correct ... but at the same time is very common to do the |
||
{ | ||
$user = $token->getUser(); | ||
|
||
// the user must be logged in; if not, deny permission | ||
if (!$user instanceof User) { | ||
return false; | ||
} | ||
|
||
// the logic of this voter is pretty simple: if the logged user is the | ||
// author of the given blog post, grant permission; otherwise, deny it. | ||
// (the supports() method guarantees that $post is a Post object) | ||
return $user === $post->getAuthor(); | ||
} | ||
} |
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 should use
PostVoter::EDIT
instead?