src/Security/Voter/RealEstateVoter.php line 13

Open in your IDE?
  1. <?php
  2. namespace App\Security\Voter;
  3. use App\Entity\Authorization;
  4. use App\Entity\RealEstate;
  5. use App\Entity\User;
  6. use App\Repository\ContractRepository;
  7. use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
  8. use Symfony\Component\Security\Core\Authorization\Voter\Voter;
  9. use Symfony\Component\Security\Core\Security;
  10. class RealEstateVoter extends Voter
  11. {
  12.     public const CREATE 'CAN_CREATE';
  13.     public const READ 'CAN_READ';
  14.     public const EDIT 'CAN_EDIT';
  15.     public const DELETE 'CAN_DELETE';
  16.     private Security $security;
  17.     private ContractRepository $contractRepository;
  18.     public function __construct(Security $securityContractRepository $contractRepository)
  19.     {
  20.         $this->security $security;
  21.         $this->contractRepository $contractRepository;
  22.     }
  23.     protected function supports($attribute$subject): bool
  24.     {
  25.         $supportsAttribute in_array($attribute, [self::CREATEself::DELETEself::EDITself::READ]);
  26.         $supportsSubject $subject instanceof RealEstate;
  27.         return $supportsAttribute && $supportsSubject;
  28.     }
  29.     /**
  30.      * @param mixed $subject
  31.      */
  32.     protected function voteOnAttribute(string $attribute$subjectTokenInterface $token): bool
  33.     {
  34.         $user $this->security->getUser();
  35.         if (!$user) {
  36.             return false;
  37.         }
  38.         switch ($attribute) {
  39.             case self::CREATE:
  40.                 return $this->canCreate($subject$user);
  41.             case self::READ:
  42.                 return $this->canRead($subject$user);
  43.             case self::EDIT:
  44.                 return $this->canEdit($subject$user);
  45.             case self::DELETE:
  46.                 return $this->canDelete($subject$user);
  47.         }
  48.         return false;
  49.     }
  50.     private function canCreate(RealEstate $realEstateUser $user): bool
  51.     {
  52.         if ($this->security->isGranted(Authorization::ROLE_ADMIN)
  53.             || $this->security->isGranted(Authorization::ROLE_OWNER_ADMIN)
  54.         ) {
  55.             return true;
  56.         }
  57.         return false;
  58.     }
  59.     private function canRead(RealEstate $realEstateUser $user): bool
  60.     {
  61.         if ($this->security->isGranted(Authorization::ROLE_ADMIN)) {
  62.             return true;
  63.         }
  64.         // shortcut for all owner
  65.         if ($this->security->isGranted(Authorization::ROLE_OWNER_REQUESTER)
  66.             && $user->getCompany()->getId() === $realEstate->getCompany()->getId()) {
  67.             // as admin of the owner with same company... all is good :-)
  68.             if ($this->security->isGranted(Authorization::ROLE_OWNER_ADMIN)) {
  69.                 return true;
  70.             }
  71.             // others owners role
  72.             foreach ($user->getGroups() as $userGroup) {
  73.                 foreach ($realEstate->getGroups() as $realEstateGroup) {
  74.                     if ($userGroup->getGroup()->getId() === $realEstateGroup->getGroup()->getId()) {
  75.                         return true;
  76.                     }
  77.                 }
  78.             }
  79.             return false;
  80.         }
  81.         // in all other case, we must check if the realEstate is in the list of InterventionRequest
  82.         // TODO: query for checking if realestate is granted
  83.         if ($this->security->isGranted(Authorization::ROLE_SERVICE_PROVIDER)) {
  84.             return $this->contractRepository->isValidForCompanyAndRealEstate($user->getCompany()->getId(), $realEstate->getId());
  85.         }
  86.         return false;
  87.     }
  88.     /**
  89.      * Only owner can edit,
  90.      *  as owner admin, we need the same company
  91.      *  as simple owner, the real estate must be in the list of our realestate.
  92.      */
  93.     private function canEdit(RealEstate $realEstateUser $user): bool
  94.     {
  95.         if ($this->security->isGranted(Authorization::ROLE_ADMIN)) {
  96.             return true;
  97.         }
  98.         // as a owner,we must at least have the same company of the realEstate
  99.         if ($user->getCompany()->getId() !== $realEstate->getCompany()->getId()) {
  100.             return false;
  101.         }
  102.         // as admin of the owner with same company... all is good :-)
  103.         if ($this->security->isGranted(Authorization::ROLE_OWNER_ADMIN)) {
  104.             return true;
  105.         }
  106.         // allow only owners
  107.         if (!$this->security->isGranted(Authorization::ROLE_OWNER)) {
  108.             return false;
  109.         }
  110.         // others owners role
  111.         foreach ($user->getGroups() as $userGroup) {
  112.             foreach ($realEstate->getGroups() as $realEstateGroup) {
  113.                 if ($userGroup->getGroup()->getId() === $realEstateGroup->getGroup()->getId()) {
  114.                     return true;
  115.                 }
  116.             }
  117.         }
  118.         return false;
  119.     }
  120.     private function canDelete(RealEstate $realEstateUser $user): bool
  121.     {
  122.         if ($this->security->isGranted(Authorization::ROLE_ADMIN)) {
  123.             return true;
  124.         }
  125.         return false;
  126.     }
  127. }