src/LiteDesk/CoreBundle/Security/Authorization/GlobalSystemAdminVoter.php line 16

Open in your IDE?
  1. <?php
  2. namespace LiteDesk\CoreBundle\Security\Authorization;
  3. use LiteDesk\OfficeBundle\Entity\Team;
  4. use LiteDesk\UserBundle\Entity\User;
  5. use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
  6. use Symfony\Component\Security\Core\Authorization\Voter\AbstractVoter;
  7. use JMS\DiExtraBundle\Annotation\Service;
  8. use JMS\DiExtraBundle\Annotation\Tag;
  9. use Symfony\Component\Security\Core\Authorization\Voter\Voter;
  10. /**
  11.  * @Service
  12.  * @Tag("security.voter")
  13.  */
  14. class GlobalSystemAdminVoter extends Voter
  15. {
  16.     protected function supports($attribute$subject)
  17.     {
  18.         return true;
  19.     }
  20.     protected function voteOnAttribute($attribute$subjectTokenInterface $token)
  21.     {
  22.         foreach($token->getRoles() as $role)
  23.         {
  24.             if($role->getRole() == $attribute || ($role->getRole() == 'GLOBAL_SYSTEM_ADMINISTRATION' && $attribute != 'ROLE_PREVIOUS_ADMIN'))
  25.             {
  26.                 return true;
  27.             }
  28.         }
  29.         return false;
  30.     }
  31.     public function vote(TokenInterface $token$subject, array $attributes)
  32.     {
  33.         // abstain vote by default in case none of the attributes are supported
  34.         $vote self::ACCESS_ABSTAIN;
  35.         if(!$attributes)
  36.         {
  37.             return $this->voteOnAttribute(array(), $subject$token);
  38.         }
  39.         foreach ($attributes as $attribute) {
  40.             if (!$this->supports($attribute$subject)) {
  41.                 continue;
  42.             }
  43.             // as soon as at least one attribute is supported, default is to deny access
  44.             $vote self::ACCESS_DENIED;
  45.             if ($this->voteOnAttribute($attribute$subject$token)) {
  46.                 // grant access as soon as at least one attribute returns a positive response
  47.                 return self::ACCESS_GRANTED;
  48.             }
  49.         }
  50.         return $vote;
  51.     }
  52. }