New Voter implements VoterInterface

Publié le par Jordan Samouh

When we call the method isGranted in Symfony2, it checks if the current user can access to a page or an object with a mask defined. (ACL/ROLE)
These checking are made by Voter class implemented by the VoterInterface of Symfony2.
All we need to do is to define a new voter class to do some other checkings like some treatments when the user is granted or not, blocks.authorizes a user according his IP….
At first we need to declare a new Voter class:

&lt;/p&gt;<br />
&lt;p&gt;class MVMVoter implements VoterInterface&lt;/p&gt;<br />
&lt;p&gt;{&lt;br /&gt;<br />
/**&lt;/p&gt;<br />
&lt;p&gt;* Checks if the voter supports the given attribute.&lt;/p&gt;<br />
&lt;p&gt;* @param string $attribute An attribute&lt;/p&gt;<br />
&lt;p&gt;* @return Boolean true if this Voter supports the attribute, false otherwise&lt;/p&gt;<br />
&lt;p&gt;*/&lt;/p&gt;<br />
&lt;p&gt;function supportsAttribute($attribute)&lt;/p&gt;<br />
&lt;p&gt;{&lt;/p&gt;<br />
&lt;p&gt;return true;&lt;/p&gt;<br />
&lt;p&gt;}&lt;/p&gt;<br />
&lt;p&gt;&amp;nbsp;&lt;/p&gt;<br />
&lt;p&gt;/**&lt;/p&gt;<br />
&lt;p&gt;* Checks if the voter supports the given class.&lt;/p&gt;<br />
&lt;p&gt;* @param string $class A class name&lt;/p&gt;<br />
&lt;p&gt;* @return true if this Voter can process the class&lt;/p&gt;<br />
&lt;p&gt;*/&lt;/p&gt;<br />
&lt;p&gt;function supportsClass($class)&lt;/p&gt;<br />
&lt;p&gt;{&lt;/p&gt;<br />
&lt;p&gt;return true;&lt;/p&gt;<br />
&lt;p&gt;}&lt;/p&gt;<br />
&lt;p&gt;&amp;nbsp;&lt;/p&gt;<br />
&lt;p&gt;/**&lt;/p&gt;<br />
&lt;p&gt;* Returns the vote for the given parameters.&lt;/p&gt;<br />
&lt;p&gt;*&lt;/p&gt;<br />
&lt;p&gt;* This method must return one of the following constants:&lt;/p&gt;<br />
&lt;p&gt;* ACCESS_GRANTED, ACCESS_DENIED, or ACCESS_ABSTAIN.&lt;/p&gt;<br />
&lt;p&gt;*&lt;/p&gt;<br />
&lt;p&gt;* @param TokenInterface $tokenAi??Ai??Ai??Ai??Ai?? A TokenInterface instance&lt;/p&gt;<br />
&lt;p&gt;* @param objectAi??Ai??Ai??Ai??Ai??Ai??Ai??Ai?? $objectAi??Ai??Ai??Ai?? The object to secure&lt;/p&gt;<br />
&lt;p&gt;* @param arrayAi??Ai??Ai??Ai??Ai??Ai??Ai??Ai??Ai?? $attributes An array of attributes associated with the method being invoked&lt;/p&gt;<br />
&lt;p&gt;* @return integer either ACCESS_GRANTED, ACCESS_ABSTAIN, or ACCESS_DENIED&lt;/p&gt;<br />
&lt;p&gt;*/&lt;/p&gt;<br />
&lt;p&gt;function vote(TokenInterface $token, $object, array $attributes)&lt;/p&gt;<br />
&lt;p&gt;{&lt;/p&gt;<br />
&lt;p&gt;return self::ACCESS_ABSTAIN;&lt;/p&gt;<br />
&lt;p&gt;}&lt;/p&gt;<br />
&lt;p&gt;

Then, we need to http://imoas.kbsu.ru/index.php/2018/02/13/skype-spy-localizador-gps-movil/ create a new service to tag this class as a voter security class

&lt;br /&gt;<br />
&amp;lt;parameter   key=&amp;quot;mvm.voter.class&amp;quot;&amp;gt;MVM\MainBundle\Acl\MVMVoter&amp;lt;/parameter&amp;gt;&lt;/p&gt;<br />
&lt;p&gt;&amp;lt;service id=&amp;quot;mvm.voter&amp;quot;  &amp;gt;&lt;/p&gt;<br />
&lt;p&gt;&amp;lt;tag name=&amp;quot;security.voter&amp;quot;&amp;gt;&amp;lt;/tag&amp;gt;&lt;/p&gt;<br />
&lt;p&gt;&amp;lt;/service&amp;gt;&lt;br /&gt;<br />

As you can see http://laiz.lv/?p=6057 the VoterClass has a method named vote

This method will be called each time there is a check http://anithamgroup.com/?p=3567 acl/permission.

Cette entrée a été publiée dans Symfony 2 Utils. Vous pouvez la mettre en favoris avec ce permalien.



2 réponses à New Voter implements VoterInterface

  1. cordoval dit :

    does not make sense why your class just denies access, class MVMVoter implements VoterInterface

    you could have point to the cookbook entry on the documentation for more reference.

    • Jordan Samouh dit :

      Yes of course, it is for example :-)
      In the vote method you do your own logic code.
      For example, if you want to access denied for a list of IPS, do the code here

      Jordan

Répondre à Jordan Samouh Annuler la réponse.

Votre adresse de messagerie ne sera pas publiée. Les champs obligatoires sont indiqués avec *

*


3 * = nine

Vous pouvez utiliser ces balises et attributs HTML : <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>