src/Security/Voter/AbsenceVoter.php line 18

Open in your IDE?
  1. <?php
  2. namespace App\Security\Voter;
  3. use App\Entity\Absence;
  4. use App\Entity\User;
  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. use Symfony\Component\Security\Core\User\UserInterface;
  9. /**
  10.  * Class AbsenceVoter
  11.  *
  12.  * @package App\Security\Voter
  13.  * @author Lukasz Krakowiak <lukasz94krakowiak@gmail.com>
  14.  */
  15. class AbsenceVoter extends Voter
  16. {
  17.     const CAN_EDIT_ABSENCE "CAN_EDIT_ABSENCE";
  18.     const CAN_REMOVE_ABSENCE "CAN_REMOVE_ABSENCE";
  19.     /**
  20.      * @var Security
  21.      */
  22.     private Security $security;
  23.     /**
  24.      * @param Security $security
  25.      */
  26.     public function __construct(Security $security)
  27.     {
  28.         $this->security $security;
  29.     }
  30.     /**
  31.      * @param string $attribute
  32.      * @param $subject
  33.      * @return bool
  34.      */
  35.     protected function supports(string $attribute$subject): bool
  36.     {
  37.         if(!$subject instanceof Absence) {
  38.             return false;
  39.         }
  40.         if (!in_array($attribute, [self::CAN_EDIT_ABSENCEself::CAN_REMOVE_ABSENCE])) {
  41.             return false;
  42.         }
  43.         return true;
  44.     }
  45.     /**
  46.      * @param string $attribute
  47.      * @param $subject
  48.      * @param TokenInterface $token
  49.      * @return bool
  50.      */
  51.     protected function voteOnAttribute(string $attribute$subjectTokenInterface $token): bool
  52.     {
  53.         $user $token->getUser();
  54.         if (!$user instanceof UserInterface) {
  55.             return false;
  56.         }
  57.         switch ($attribute) {
  58.             case self::CAN_EDIT_ABSENCE:
  59.                 return $this->canEditAbsence($user$subject);
  60.             case self::CAN_REMOVE_ABSENCE:
  61.                 return $this->canRemoveAbsence($user$subject);
  62.         }
  63.         return false;
  64.     }
  65.     /**
  66.      * @param User|UserInterface $user
  67.      * @param Absence $absence
  68.      * @return bool
  69.      */
  70.     private function canEditAbsence(User $userAbsence $absence): bool
  71.     {
  72.         return $this->isAdminOrOwner($user$absence);
  73.     }
  74.     /**
  75.      * @param User|UserInterface $user
  76.      * @param Absence $absence
  77.      * @return bool
  78.      */
  79.     private function canRemoveAbsence(User $userAbsence $absence): bool
  80.     {
  81.         return $this->isAdminOrOwner($user$absence);
  82.     }
  83.     /**
  84.      * @param User $user
  85.      * @param Absence $absence
  86.      * @return bool
  87.      */
  88.     private function isAdminOrOwner(User $userAbsence $absence): bool
  89.     {
  90.         if ($this->security->isGranted("ROLE_ADMIN")) {
  91.             return true;
  92.         }
  93.         if($absence->getUser() === $user) {
  94.             return true;
  95.         }
  96.         return false;
  97.     }
  98. }