src/V4/Voters/MailTypeVoter.php line 11

Open in your IDE?
  1. <?php
  2. namespace App\V4\Voters;
  3. use App\Security\User;
  4. use LogicException;
  5. use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
  6. use Symfony\Component\Security\Core\Authorization\Voter\Voter;
  7. use Symfony\Component\Security\Core\Security;
  8. class MailTypeVoter extends Voter
  9. {
  10.     const MAIL_TYPE_SHOW_LIST 'mail_type_show_list';
  11.     const MAIL_TYPE_ADD_EDIT 'mail_type_add_edit';
  12.     /**
  13.      * @var Security
  14.      */
  15.     private $security;
  16.     public function __construct(Security $security)
  17.     {
  18.         $this->security $security;
  19.     }
  20.     /**
  21.      * @param $attribute
  22.      * @param $subject
  23.      *
  24.      * @return bool
  25.      */
  26.     protected function supports($attribute$subject): bool
  27.     {
  28.         return in_array($attribute, [
  29.             self::MAIL_TYPE_SHOW_LIST,
  30.             self::MAIL_TYPE_ADD_EDIT,
  31.         ], true);
  32.     }
  33.     /**
  34.      * @param $attribute
  35.      * @param $subject
  36.      * @param TokenInterface $token
  37.      *
  38.      * @return bool
  39.      *
  40.      * @throws LogicException
  41.      */
  42.     protected function voteOnAttribute($attribute$subjectTokenInterface $token): bool
  43.     {
  44.         $user $token->getUser();
  45.         if (!$user instanceof User) {
  46.             return false;
  47.         }
  48.         switch ($attribute) {
  49.             case self::MAIL_TYPE_SHOW_LIST:
  50.                 return $this->canMailTypeShowList();
  51.             case self::MAIL_TYPE_ADD_EDIT:
  52.                 return $this->canMailTypeAddEdit();
  53.         }
  54.         throw new LogicException('This should never happen');
  55.     }
  56.     private function canMailTypeShowList(): bool
  57.     {
  58.         return true;
  59.     }
  60.     private function canMailTypeAddEdit(): bool
  61.     {
  62.         return true;
  63.     }
  64. }