src/Security/Voter/SpamAssassinVoter.php line 11

Open in your IDE?
  1. <?php
  2. namespace App\Security\Voter;
  3. use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
  4. use Symfony\Component\Security\Core\Authorization\Voter\Voter;
  5. use Symfony\Component\Security\Core\User\UserInterface;
  6. use Sensio\Bundle\FrameworkExtraBundle\Configuration\IsGranted;
  7. use Symfony\Component\Security\Core\Security;
  8. class SpamAssassinVoter extends Voter
  9. {
  10.     private $security;
  11.     
  12.     public function __construct(Security $security)
  13.     {
  14.         $this->security $security;
  15.     }
  16.     
  17.     protected function supports($attribute$subject) : bool
  18.     {
  19.         return in_array($attribute, ['SPAMASSASSIN'])
  20.         && $subject instanceof \App\Model\UserPref;
  21.     }
  22.     
  23.     protected function voteOnAttribute($attribute$subjectTokenInterface $token) : bool
  24.     {
  25.         $user $token->getUser();
  26.         // if the user is anonymous, do not grant access
  27.         if (!$user instanceof UserInterface) {
  28.             return false;
  29.         }
  30.         switch ($attribute) {   
  31.             case 'SPAMASSASSIN':
  32.                 if($this->security->isGranted('ROLE_POSTMASTER_MANY')) {
  33.                     if(preg_match('/^:/'$subject->getUsername())) {
  34.                         $subjUserDomainArr explode('@'$subject->getUsername());
  35.                         
  36.                         if(sizeof($subjUserDomainArr) != 2) {
  37.                             return false;
  38.                         }
  39.                         
  40.                         foreach($user->getDomains() as $d) {
  41.                             if($subjUserDomainArr[1] == $d) {
  42.                                 return true;
  43.                             }
  44.                         }
  45.                         
  46.                     }
  47.                     elseif($subject->getUsername() == $user->getUsername()) {
  48.                         return true;
  49.                     }
  50.                 }
  51.                 elseif($this->security->isGranted('ROLE_POSTMASTER')) {
  52.                     if(preg_match('/^:/'$subject->getUsername())) {
  53.                         $subjUserDomainArr explode('@'$subject->getUsername());
  54.                         $userDomainArr explode('@'$user->getUsername());                        
  55.                         
  56.                         if(sizeof($subjUserDomainArr) != || sizeof($userDomainArr) != 2) {
  57.                             return false;
  58.                         }
  59.                         
  60.                         if($userDomainArr[1] == $subjUserDomainArr[1]) {
  61.                             return true;
  62.                         }
  63.                     }
  64.                     elseif($subject->getUsername() == $user->getUsername()) {
  65.                         return true;
  66.                     }
  67.                 }
  68.                 elseif($this->security->isGranted('ROLE_USER')) {
  69.                     // only possible to edit his own account
  70.                     if($subject->getUsername() == $user->getUsername()) {
  71.                         return true;
  72.                     }
  73.                 }
  74.                 break;                
  75.         }
  76.         
  77.         return false;
  78.     }
  79. }