8000 [Security] add & update doc entries on AbstractVoter implementation by inoryy · Pull Request #4257 · symfony/symfony-docs · GitHub
[go: up one dir, main page]

Skip to content

[Security] add & update doc entries on AbstractVoter implementation #4257

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

Closed
wants to merge 5 commits into from
Closed
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
add fixes to data_permission cookbook
  • Loading branch information
inoryy committed Jan 31, 2015
commit b1a90ba63409f113f5fbfe75cb5c7e7185264025
31 changes: 27 additions & 4 deletions cookbook/security/voters_data_permission.rst
Original file line number Diff line number Diff line change
Expand Up @@ -37,12 +37,19 @@ For more information take a look at
The Voter Interface
-------------------

A custom voter must implement
A custom voter needs to implement
:class:`Symfony\\Component\\Security\\Core\\Authorization\\Voter\\VoterInterface`
and an :class:`Symfony\\Component\\Security\\Core\\Authorization\\Voter\\AbstractVoter`
class is provided with following structure:
or extend :class:`Symfony\\Component\\Security\\Core\\Authorization\\Voter\\AbstractVoter`,
which makes creating a voter even easier.

.. include:: /cookbook/security/abstract_voter.rst.inc
.. code-block:: php

abstract class AbstractVoter implements VoterInterface
{
abstract protected function getSupportedClasses();
abstract protected function getSupportedAttributes();
abstract protected function isGranted($attribute, $object, $user = null);
}

In this example, the voter will check if the user has access to a specific
object according to your custom conditions (e.g. they must be the owner of
Expand Down Expand Up @@ -106,6 +113,22 @@ edit a particular object. Here's an example implementation:
That's it! The voter is done. The next step is to inject the voter into
the security layer.

To recap, here's what's expected from the three abstract methods:

The :method:`Symfony\\Component\\Security\\Core\\Authorization\\Voter\\AbstractVoter::getSupportedClasses`
method tells Symfony that your voter should be called whenever an object of one of the given classes
is passed to `isGranted` For example, if you return ['\Acme\DemoBundle\Model\Product'],
Copy link
Member

Choose a reason for hiding this comment

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

double-ticks around isGranted

Copy link
Member

Choose a reason for hiding this comment

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

Change the ['Acme\DemoBundle... to be AppBundle\Model\Product and use a full array( there instead of [.

Symfony will call your voter when a `Product` object is passed to `isGranted`.
Copy link
Member

Choose a reason for hiding this comment

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

double-ticks!: ``

Copy link
Member

Choose a reason for hiding this comment

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

We should add one more note:

Currently, to use the AbstractVoter base class, you must be creating a voter where an object is always passed to isGranted().


The :method:`Symfony\\Component\\Security\\Core\\Authorization\\Voter\\AbstractVoter::getSupportedAttributes`
method tells Symfony that your voter should be called whenever one of these strings is passes as the
Copy link
Member

Choose a reason for hiding this comment

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

strings is passed

first argument to `isGranted`. For example, if you return `array('CREATE', 'READ')`, then
Symfony will call your voter when one of these is passed to `isGranted`.
Copy link
Member

Choose a reason for hiding this comment

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

a few more double-ticks in this paragraph are needed


The :method:`Symfony\\Component\\Security\\Core\\Authorization\\Voter\\AbstractVoter::isGranted`
method must implement the business logic that verifies whether or not a given
user is allowed access to a given attribute on a given object. This method must return a boolean.
Copy link
Member

Choose a reason for hiding this comment

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

... access to a given attribute (e.g. `CREATE` or `READ`) on a give object.


Declaring the Voter as a Service
--------------------------------

Expand Down
0