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

Appearance settings

New Data Voter Article (continuation) #3594

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 15 commits into from
Mar 4, 2014
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
updated page with suggestion from the review
  • Loading branch information
Michael Klein authored and weaverryan committed Feb 20, 2014
commit 731dcad72ea7e0e2b0f27cc693d01e47b6e805f3
29 changes: 17 additions & 12 deletions 29 cookbook/security/voters_data_permission.rst
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ application: :doc:`"/cookbook/security/voters"`.

.. tip::

It is good to understand the basics about what and how
:doc:`authorization </components/security/authorization>` works. // correct link in book?
Have a look at the referenced page if you are not familiar with
:doc:`authorization </components/security/authorization>`.

How Symfony uses Voters
-----------------------
Expand All @@ -33,7 +33,9 @@ A custom voter must implement
:class:`Symfony\\Component\\Security\\Core\\Authorization\\Voter\\VoterInterface`,
which has this structure:

.. code-block:: php // :: shortcut? and put the snippet (to line 56) in a single file an reference ?
// 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.

.. code-block:: php

interface VoterInterface
{
Expand Down Expand Up @@ -95,10 +97,8 @@ You could store your Voter to check permission for the view and edit action like
$array = array('Acme\DemoBundle\Entity\Post');

foreach ($array as $item) {
// check with stripos in case doctrine is using a proxy class for this object
// if (stripos($s, $item) !== false) {
if ($obj instanceof $item)) // check if this will also check for interfaces etc. like it should be in oop (inheritace)
// or return $targetClass === $class || is_subclass_of($class, $targetClass);
if ($obj instanceof $item))

return true;
}
}
Expand All @@ -107,16 +107,21 @@ You could store your Voter to check permission for the view and edit action like
}

/** @var \Acme\DemoBundle\Entity\Post $post */
public function vote(TokenInterface $token, $post, array $attributes) // remove array
public function vote(TokenInterface $token, $post, array $attributes)
{
// always get the first attribute
// check if voter is used correct, only allow one attribute for a check
if(count($attributes) !== 1 || !is_string($attributes[0])) {
throw new PreconditionFailedHttpException('The Attribute was not set correct. Maximum 1 attribute.');
}

// set the attribute to check against
$attribute = $attributes[0];

// get current logged in user
$user = $token->getUser();

// check if class of this object is supported by this voter
if (!($this->supportsClass($post))) { // maybe without ClassUtils::getRealClass(
if (!($this->supportsClass($post))) {

return VoterInterface::ACCESS_ABSTAIN;
}
Expand Down Expand Up @@ -151,8 +156,8 @@ You could store your Voter to check permission for the view and edit action like
break;

default:
// otherwise throw an exception
throw new PreconditionFailedHttpException('The Attribute "'.$attribute.'"" was not found.')
// otherwise throw an exception, which will break the request
throw new PreconditionFailedHttpException('The Attribute "'.$attribute.'" was not found.')
}

}
Expand Down
Morty Proxy This is a proxified and sanitized view of the page, visit original site.