src/Security/Voter/ApiLabelOwnerVoter.php line 13

  1. <?php
  2. declare(strict_types=1);
  3. namespace App\Security\Voter;
  4. use App\Entity\Label;
  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 App\Enum\RoleEnum;
  9. final class ApiLabelOwnerVoter extends Voter
  10. {
  11.     protected function supports(string $attribute$subject): bool
  12.     {
  13.         if ($attribute === 'api_label_owner_voter') {
  14.             return true;
  15.         }
  16.         return false;
  17.     }
  18.     protected function voteOnAttribute(string $attribute$subjectTokenInterface $token): bool
  19.     {
  20.         if (!$subject instanceof Label) {
  21.             return false;
  22.         }
  23.         /** @var ?User $loggedUser */
  24.         $loggedUser $token->getUser();
  25.         if (!$loggedUser) {
  26.             return false;
  27.         }
  28.         $labelInstitution $subject->getInstitution()->getId();
  29.         $userInstitution $loggedUser
  30.                         ->getCurrentRoleInstitution()
  31.                         ->getUserInstitution()
  32.                         ->getInstitution()
  33.                         ->getId();
  34.         $userRole $loggedUser->getCurrentRoleInstitution()->getRole()->getKeyName();
  35.         
  36.         return $userInstitution === $labelInstitution && $userRole === RoleEnum::GENERAL_MANAGER;
  37.     }
  38. }