src/Security/Voter/ProcmailVoter.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 ProcmailVoter 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, ['PROCMAIL'])
  20.             && $subject instanceof \App\Model\ProcmailRule;
  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.         
  31.         switch ($attribute) {
  32.             case 'PROCMAIL':
  33.                 if($this->security->isGranted('ROLE_USER')) {
  34.                     // only possible to edit his own account
  35.                     if($subject->getOwner() == $user->getUsername()) {
  36.                         return true;
  37.                     }
  38.                 }
  39.         }
  40.         
  41.         return false;
  42.     }
  43. }