src/Security/Voter/LoginMessageVoter.php line 12

Open in your IDE?
  1. <?php
  2. namespace App\Security\Voter;
  3. use App\Util\AccountTools;
  4. use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
  5. use Symfony\Component\Security\Core\Authorization\Voter\Voter;
  6. use Symfony\Component\Security\Core\User\UserInterface;
  7. use Sensio\Bundle\FrameworkExtraBundle\Configuration\IsGranted;
  8. use Symfony\Component\Security\Core\Security;
  9. class LoginMessageVoter extends Voter
  10. {
  11.     private $security;
  12.     
  13.     public function __construct(Security $security)
  14.     {
  15.         $this->security $security;
  16.     }
  17.     
  18.     protected function supports($attribute$subject) : bool
  19.     {
  20.         // NEW actions don't yet have objects
  21.         if(in_array($attribute, ['LOGIN_MESSAGES'])) {
  22.             return true;
  23.         }
  24.         
  25.         if(in_array($attribute, ['NEW_LOGIN_MESSAGE'])) {
  26.             return true;
  27.         }
  28.             
  29.         return in_array($attribute, ['EDIT''DELETE'])
  30.             && $subject instanceof \App\Model\LoginMessage;
  31.     }
  32.     
  33.     protected function voteOnAttribute($attribute$subjectTokenInterface $token) : bool
  34.     {
  35.         $user $token->getUser();
  36.         // if the user is anonymous, do not grant access
  37.         if (!$user instanceof UserInterface) {
  38.             return false;
  39.         }
  40.         
  41.         if(!isset($_SERVER['APP_QP_LOGIN_MSG_ALLOWED'])) {
  42.             return false;
  43.         }
  44.         
  45.         if(isset($_SERVER['APP_QP_LOGIN_MSG_ALLOWED'])) {
  46.             if($_SERVER['APP_QP_LOGIN_MSG_ALLOWED'] == 'false') {
  47.                 return false;
  48.             }
  49.         }
  50.         
  51.         switch ($attribute) {
  52.             case 'EDIT':
  53.                 if($this->security->isGranted('ROLE_POSTMASTER')) {
  54.                     return true;
  55.                 }
  56.                 break;
  57.                 
  58.             case 'DELETE':
  59.                 if($this->security->isGranted('ROLE_POSTMASTER')) {
  60.                     return true;
  61.                 }
  62.                 break;
  63.                 
  64.             case 'NEW_LOGIN_MESSAGE':
  65.                 if($this->security->isGranted('ROLE_POSTMASTER')) {
  66.                     return true;
  67.                 }
  68.                 break;
  69.                     
  70.             case 'LOGIN_MESSAGES':
  71.                 if($this->security->isGranted('ROLE_POSTMASTER')) {
  72.                     return true;
  73.                 }
  74.                 break;                
  75.         }
  76.         
  77.         return false;
  78.     }
  79. }