src/V4/Voters/UnitMailVoter.php line 12

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