src/Security/Voter/UserVoter.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\Manager\UserManager;
  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 UserVoter 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.     public function __construct(
  17.         private Security $security,
  18.         private UserManager $userManager
  19.     ) {
  20.     }
  21.     protected function supports($attribute$subject): bool
  22.     {
  23.         $supportsAttribute in_array($attribute, [self::CREATEself::DELETEself::EDITself::READ]);
  24.         $supportsSubject $subject instanceof User;
  25.         return $supportsAttribute && $supportsSubject;
  26.     }
  27.     /**
  28.      * @param mixed $subject
  29.      */
  30.     protected function voteOnAttribute(string $attribute$subjectTokenInterface $token): bool
  31.     {
  32.         $user $this->security->getUser();
  33.         if (!$user) {
  34.             return false;
  35.         }
  36.         switch ($attribute) {
  37.             case self::CREATE:
  38.                 return $this->canCreate($subject$user);
  39.             case self::READ:
  40.                 return $this->canRead($subject$user);
  41.             case self::EDIT:
  42.                 return $this->canEdit($subject$user);
  43.             case self::DELETE:
  44.                 return $this->canDelete($subject$user);
  45.         }
  46.         return false;
  47.     }
  48.     private function canCreate(User $subjectUser $user): bool
  49.     {
  50.         if ($this->security->isGranted(Authorization::ROLE_ADMIN)
  51.             || $this->security->isGranted(Authorization::ROLE_OWNER_ADMIN)
  52.         ) {
  53.             return true;
  54.         }
  55.         return false;
  56.     }
  57.     private function canRead(User $subjectUser $user): bool
  58.     {
  59.         if ($this->security->isGranted(Authorization::ROLE_ADMIN)) {
  60.             return true;
  61.         }
  62.         return $this->userManager->hasAccess($subject$user);
  63.     }
  64.     /**
  65.      * Only owner can edit,
  66.      *  as owner admin, we need the same company
  67.      *  as simple owner, the real estate must be in the list of our realestate.
  68.      *
  69.      * @param RealEstate $realEstate
  70.      */
  71.     private function canEdit(User $subjectUser $user): bool
  72.     {
  73.         if ($this->security->isGranted(Authorization::ROLE_ADMIN)) {
  74.             return true;
  75.         }
  76.         // as a owner,we must at least have the same company of the realEstate
  77.         if ($user->getCompany()->getId() !== $subject->getCompany()->getId()) {
  78.             return false;
  79.         }
  80.         if (
  81.             $this->security->isGranted(Authorization::ROLE_OWNER_ADMIN)
  82.             || $this->security->isGranted(Authorization::ROLE_SERVICE_PROVIDER_ADMIN)
  83.         ) {
  84.             return true;
  85.         }
  86.         return false;
  87.     }
  88.     private function canDelete(User $subjectUser $user): bool
  89.     {
  90.         return $this->canEdit($subject$user);
  91.     }
  92. }