src/Security/Voter/AccessPointVoter.php line 12

Open in your IDE?
  1. <?php
  2. namespace App\Security\Voter;
  3. use App\Entity\AccessPoint;
  4. use App\Entity\Authorization;
  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 AccessPointVoter 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 AccessPoint;
  24.         return $supportsAttribute && $supportsSubject;
  25.     }
  26.     /**
  27.      * @param AccessPoint $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(AccessPoint $accessPointUser $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(AccessPoint $accessPointUser $user): bool
  57.     {
  58.         $realEstate $accessPoint->getRealEstate();
  59.         if ($realEstate) {
  60.             return $this->security->isGranted(RealEstateVoter::READ$realEstate);
  61.         }
  62.         return false;
  63.     }
  64.     private function canEdit(AccessPoint $accessPointUser $user): bool
  65.     {
  66.         $realEstate $accessPoint->getRealEstate();
  67.         if ($realEstate) {
  68.             return $this->security->isGranted(RealEstateVoter::EDIT$realEstate);
  69.         }
  70.         return true;
  71.     }
  72.     private function canDelete(AccessPoint $accessPointUser $user): bool
  73.     {
  74.         $realEstate $accessPoint->getRealEstate();
  75.         if ($realEstate) {
  76.             return $this->security->isGranted(RealEstateVoter::EDIT$realEstate);
  77.         }
  78.         return true;
  79.     }
  80. }