symfony - Symfony2 Custom Voter Role Hierarchy -


i'm trying create custom voter check access on entities specific actions. logic working fine. have actions allowed if user either "owner" of entity, or admin.

however, can't check role of user because i'm looking @ role hierarchy. example in docs uses in_array, won't work (http://symfony.com/doc/current/best_practices/security.html)

my voter (shortened clarity). i've tried injecting security context (or authorizationcheckerinterface in 2.6), has circular dependency since voter.

<?php // ... class applicationvoter extends abstractvoter {     const view = 'view';      /**      * @var authorizationcheckerinterface       */     private $security;      /*public function __construct(authorizationcheckerinterface $security)     {         $this->security = $security;     }*/      /**      * {@inheritdoc}      */     protected function getsupportedattributes()     {         return array(             self::view         );     }      /**      * {@inheritdoc}      */     protected function getsupportedclasses()     {         return array('study\mainbundle\entity\application');     }      /**      * {@inheritdoc}      */     protected function isgranted($attribute, $application, $user = null)     {         if (!$user instanceof userinterface) {             return false;         }          if ($attribute === self::view) {             return $this->canview($application, $user);         }          return false;     }      /**      * can view own application if not deleted      * admin can view if submitted      *       * @param \study\mainbundle\entity\application $application      * @param \study\mainbundle\entity\user $user      *       * @return boolean      */     protected function canview(application $application, user $user)     {         return ($application->isowner($user) && !$application->isdeleted())             || (!$application->ishiddentoadmin() && $this->security->isgranted('role_admin_ro'));     } } 

i'd use built-in rolehiearchyvoter here, it's non-public service. there solution this? i'd avoid duplicating framework code or making roles more complicated strings if possible.

edit: injecting whole container works, isn't ideal solution. way can access built-in hierarchy voter?

there service called security.role_hierarchy has info need. it's how security context checks roles. need few lines of wrapper code it's not bad.

# need because service not public # http://symfony.com/doc/current/components/dependency_injection/advanced.html cerad_core__role_hierarchy:     alias: security.role_hierarchy  cerad_game__game_official__voter:     class:  cerad\bundle\gamebundle\action\gameofficial\gameofficialvoter     public: false     arguments:       - '@cerad_core__role_hierarchy'     tags:        - { name: security.voter }  

the voter class:

class gameofficialvoter implements voterinterface {         public function __construct($rolehierarchy)     {           $this->rolehierarchy = $rolehierarchy;     }      protected function hasrole($token,$targetrole)     {         $reachableroles = $this->rolehierarchy->getreachableroles($token->getroles());         foreach($reachableroles $role)         {             if ($role->getrole() == $targetrole) return true;         }         return false;     }      protected function canviewofficialname($official,$token)     {              // pending 1 protected against          if ($official->getassignstate() != 'pending') return $this->accessgranted;           // assignors can see          if ($this->hasrole($token,'role_assignor')) return $this->accessgranted;           return $this->accessdenied;     } } 

Comments

Popular posts from this blog

python - mat is not a numerical tuple : openCV error -

c# - MSAA finds controls UI Automation doesn't -

wordpress - .htaccess: RewriteRule: bad flag delimiters -