src/Security/Voter/CompanyVoter.php line 15

Open in your IDE?
  1. <?php
  2. namespace App\Security\Voter;
  3. use App\Entity\Authorization;
  4. use App\Entity\Company;
  5. use App\Entity\Group;
  6. use App\Entity\RealEstate;
  7. use App\Entity\User;
  8. use App\Manager\CompanyManager;
  9. use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
  10. use Symfony\Component\Security\Core\Authorization\Voter\Voter;
  11. use Symfony\Component\Security\Core\Security;
  12. class CompanyVoter extends Voter
  13. {
  14.     public const CREATE 'CAN_CREATE';
  15.     public const READ 'CAN_READ';
  16.     public const EDIT 'CAN_EDIT';
  17.     public const DELETE 'CAN_DELETE';
  18.     public function __construct(
  19.         private Security $security,
  20.         private CompanyManager $companyManager
  21.     ) {
  22.     }
  23.     protected function supports($attribute$subject): bool
  24.     {
  25.         $supportsAttribute in_array($attribute, [self::CREATEself::DELETEself::EDITself::READ]);
  26.         $supportsSubject $subject instanceof Company;
  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(Company $companyUser $user): bool
  51.     {
  52.         if ($this->security->isGranted(Authorization::ROLE_ADMIN)) {
  53.             return true;
  54.         }
  55.         return false;
  56.     }
  57.     private function canRead(Company $companyUser $user): bool
  58.     {
  59.         if ($this->security->isGranted(Authorization::ROLE_ADMIN)) {
  60.             return true;
  61.         }
  62.         if ($this->security->isGranted(Authorization::ROLE_OWNER_ADMIN)) {
  63.             if (Company::CUSTOMER_TYPE_SERVICE_PROVIDER == $company->getCustomerType()) {
  64.                 return true;
  65.             }
  66.         }
  67.         if ($this->security->isGranted(Authorization::ROLE_SERVICE_PROVIDER_ADMIN)) {
  68.             if (Company::CUSTOMER_TYPE_OWNER == $company->getCustomerType()) {
  69.                 return true;
  70.             }
  71.         }
  72.         return $this->companyManager->hasAccess($company$user);
  73.     }
  74.     /**
  75.      * Only owner can edit,
  76.      *  as owner admin, we need the same company
  77.      *  as simple owner, the real estate must be in the list of our realestate.
  78.      *
  79.      * @param RealEstate $realEstate
  80.      */
  81.     private function canEdit(Company $companyUser $user): bool
  82.     {
  83.         if ($this->security->isGranted(Authorization::ROLE_ADMIN)) {
  84.             return true;
  85.         }
  86.         // as a owner,we must at least have the same company of the realEstate
  87.         if ($user->getCompany()->getId() !== $company->getId()) {
  88.             return false;
  89.         }
  90.         if (
  91.             $this->security->isGranted(Authorization::ROLE_OWNER_ADMIN)
  92.             || $this->security->isGranted(Authorization::ROLE_SERVICE_PROVIDER_ADMIN)
  93.         ) {
  94.             return true;
  95.         }
  96.         return false;
  97.     }
  98.     private function canDelete(Group $groupUser $user): bool
  99.     {
  100.         return $this->canEdit($group$user);
  101.     }
  102. }