Skip to content

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

Merged
merged 3 commits into from
Jan 21, 2017
Merged

Add a security Post Voter #442

merged 3 commits into from
Jan 21, 2017

Conversation

yceruto
Copy link
Member

@yceruto yceruto commented Jan 17, 2017

Implement #440 feature.

  • Add more code comments and doc references

Following some references and recommendations in:

Any suggestion is welcome!

*/
class PostVoter extends Voter
{
const VIEW = 'show';
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have doubts about this permission, since in the real world any user should see any Post. This is restricted in admin only but is a bit confusing.

if (null === $this->getUser() || !$post->isAuthor($this->getUser())) {
throw $this->createAccessDeniedException('Posts can only be edited by their authors.');
}
$this->denyAccessUnlessGranted('edit', $post, 'Posts can only be edited by their authors.');
Copy link
Member Author

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?

/**
* Is the given User the author of this Post?
*/
public function isAuthor(User $user)
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should keep it to implement both ways?

@yceruto
Copy link
Member Author

yceruto commented Jan 19, 2017

Rebased with master!

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]);
Copy link
Member

Choose a reason for hiding this comment

The 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.

Copy link
Member

@javiereguiluz javiereguiluz left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

/**
* {@inheritdoc}
*/
protected function voteOnAttribute($attribute, $post, TokenInterface $token)
Copy link
Member Author

@yceruto yceruto Jan 20, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This implementation can be simplified too, just one line:

return $token->getUser() === $post->getAuthor();

? because $post->getAuthor() always has an author when $token->getUser() is null this return false.

Copy link
Member

Choose a reason for hiding this comment

The 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 if (!$user instanceof User) { return false; } thing in voters ... so we probably should keep it.

@javiereguiluz
Copy link
Member

@yceruto I wanted to add a security voter since day one. Thanks for making it happen!

@javiereguiluz javiereguiluz merged commit a592eea into symfony:master Jan 21, 2017
javiereguiluz added a commit that referenced this pull request Jan 21, 2017
This PR was merged into the master branch.

Discussion
----------

Add a security Post Voter

Implement #440 feature.

- [x] Add more code comments and doc references

Following some references and recommendations in:
- https://symfony.com/doc/current/components/security/authorization.html
- http://symfony.com/doc/current/security/voters.html
- https://stovepipe.systems/post/symfony-security-roles-vs-voters (@iltar blog)

Any suggestion is welcome!

Commits
-------

a592eea Minor reword in the help note
8468423 Simplified the voter code a bit
c14e9db Add security post voter
@yceruto yceruto deleted the voter branch January 22, 2017 03:15
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants