src/Security/Voter/GroupVoter.php line 14

Open in your IDE?
  1. <?php
  2. namespace App\Security\Voter;
  3. use App\Entity\Authorization;
  4. use App\Entity\Group;
  5. use App\Entity\RealEstate;
  6. use App\Entity\User;
  7. use App\Manager\GroupManager;
  8. use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
  9. use Symfony\Component\Security\Core\Authorization\Voter\Voter;
  10. use Symfony\Component\Security\Core\Security;
  11. class GroupVoter extends Voter
  12. {
  13.     public const CREATE 'CAN_CREATE';
  14.     public const READ 'CAN_READ';
  15.     public const EDIT 'CAN_EDIT';
  16.     public const DELETE 'CAN_DELETE';
  17.     public function __construct(
  18.         private Security $security,
  19.         private GroupManager $groupRepository
  20.     ) {
  21.     }
  22.     protected function supports($attribute$subject): bool
  23.     {
  24.         $supportsAttribute in_array($attribute, [self::CREATEself::DELETEself::EDITself::READ]);
  25.         $supportsSubject $subject instanceof Group;
  26.         return $supportsAttribute && $supportsSubject;
  27.     }
  28.     /**
  29.      * @param mixed $subject
  30.      */
  31.     protected function voteOnAttribute(string $attribute$subjectTokenInterface $token): bool
  32.     {
  33.         $user $this->security->getUser();
  34.         if (!$user) {
  35.             return false;
  36.         }
  37.         switch ($attribute) {
  38.             case self::CREATE:
  39.                 return $this->canCreate($subject$user);
  40.             case self::READ:
  41.                 return $this->canRead($subject$user);
  42.             case self::EDIT:
  43.                 return $this->canEdit($subject$user);
  44.             case self::DELETE:
  45.                 return $this->canDelete($subject$user);
  46.         }
  47.         return false;
  48.     }
  49.     private function canCreate(Group $groupUser $user): bool
  50.     {
  51.         if ($this->security->isGranted(Authorization::ROLE_ADMIN)
  52.             || $this->security->isGranted(Authorization::ROLE_OWNER_ADMIN)
  53.         ) {
  54.             return true;
  55.         }
  56.         return false;
  57.     }
  58.     private function canRead(Group $groupUser $user): bool
  59.     {
  60.         if ($this->security->isGranted(Authorization::ROLE_ADMIN)) {
  61.             return true;
  62.         }
  63.         return $this->groupRepository->hasAccess($group$user);
  64.     }
  65.     /**
  66.      * Only owner can edit,
  67.      *  as owner admin, we need the same company
  68.      *  as simple owner, the real estate must be in the list of our realestate.
  69.      *
  70.      * @param RealEstate $realEstate
  71.      */
  72.     private function canEdit(Group $groupUser $user): bool
  73.     {
  74.         if ($this->security->isGranted(Authorization::ROLE_ADMIN)) {
  75.             return true;
  76.         }
  77.         // as a owner,we must at least have the same company of the realEstate
  78.         if ($user->getCompany()->getId() !== $group->getCompany()->getId()) {
  79.             return false;
  80.         }
  81.         if (
  82.             $this->security->isGranted(Authorization::ROLE_OWNER_ADMIN)
  83.             || $this->security->isGranted(Authorization::ROLE_SERVICE_PROVIDER_ADMIN)
  84.         ) {
  85.             return true;
  86.         }
  87.         return false;
  88.     }
  89.     private function canDelete(Group $groupUser $user): bool
  90.     {
  91.         return $this->canEdit($group$user);
  92.     }
  93. }