src/Security/Voter/EquipmentVoter.php line 12

Open in your IDE?
  1. <?php
  2. namespace App\Security\Voter;
  3. use App\Entity\Authorization;
  4. use App\Entity\Equipment;
  5. use App\Entity\User;
  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 EquipmentVoter extends Voter
  10. {
  11.     public const CREATE 'CAN_CREATE';
  12.     public const READ 'CAN_READ';
  13.     public const EDIT 'CAN_EDIT';
  14.     public const DELETE 'CAN_DELETE';
  15.     private Security $security;
  16.     public function __construct(Security $security)
  17.     {
  18.         $this->security $security;
  19.     }
  20.     protected function supports($attribute$subject): bool
  21.     {
  22.         $supportsAttribute in_array($attribute, [self::CREATEself::DELETEself::EDITself::READ]);
  23.         $supportsSubject $subject instanceof Equipment;
  24.         return $supportsAttribute && $supportsSubject;
  25.     }
  26.     /**
  27.      * @param Equipment $subject
  28.      */
  29.     protected function voteOnAttribute(string $attribute$subjectTokenInterface $token): bool
  30.     {
  31.         $user $this->security->getUser();
  32.         if (!$user) {
  33.             return false;
  34.         }
  35.         switch ($attribute) {
  36.             case self::CREATE:
  37.                 return $this->canCreate($subject$user);
  38.             case self::READ:
  39.                 return $this->canRead($subject$user);
  40.             case self::EDIT:
  41.                 return $this->canEdit($subject$user);
  42.             case self::DELETE:
  43.                 return $this->canDelete($subject$user);
  44.         }
  45.         return false;
  46.     }
  47.     private function canCreate(Equipment $equipmentUser $user): bool
  48.     {
  49.         if ($this->security->isGranted(Authorization::ROLE_ADMIN)
  50.             || $this->security->isGranted(Authorization::ROLE_OWNER_ADMIN)
  51.         ) {
  52.             return true;
  53.         }
  54.         return false;
  55.     }
  56.     private function canRead(Equipment $equipmentUser $user): bool
  57.     {
  58.         if ($this->security->isGranted(Authorization::ROLE_ADMIN)) {
  59.             return true;
  60.         }
  61.         $realEstate $equipment->getRealEstate();
  62.         if ($realEstate) {
  63.             return $this->security->isGranted(RealEstateVoter::READ$realEstate);
  64.         }
  65.         return false;
  66.     }
  67.     private function canEdit(Equipment $equipmentUser $user): bool
  68.     {
  69.         if ($this->security->isGranted(Authorization::ROLE_ADMIN)) {
  70.             return true;
  71.         }
  72.         $realEstate $equipment->getRealEstate();
  73.         if ($realEstate) {
  74.             return $this->security->isGranted(RealEstateVoter::EDIT$realEstate);
  75.         }
  76.         return false;
  77.     }
  78.     private function canDelete(Equipment $equipmentUser $user): bool
  79.     {
  80.         if ($this->security->isGranted(Authorization::ROLE_ADMIN)) {
  81.             return true;
  82.         }
  83.         $realEstate $equipment->getRealEstate();
  84.         if ($realEstate) {
  85.             return $this->security->isGranted(RealEstateVoter::EDIT$realEstate);
  86.         }
  87.         return false;
  88.     }
  89. }