src/Core/Security/Voter/PartnerableAccessVoter.php line 10

Open in your IDE?
  1. <?php
  2. namespace App\Core\Security\Voter;
  3. use App\Core\Entity\Interfaces\PartnerableInterface;
  4. use App\Core\Entity\User;
  5. use Symfony\Component\Security\Core\Authorization\Voter\Voter;
  6. use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
  7. final class PartnerableAccessVoter extends Voter
  8. {
  9.     const PARTNERABLE_ACCESS 'partnerable_access';
  10.     /**
  11.      * {@inheritdoc}
  12.      */
  13.     public function supports(string $attribute$subject): bool
  14.     {
  15.         return $subject instanceof PartnerableInterface && in_array($attribute, [
  16.             self::PARTNERABLE_ACCESS,
  17.         ]);
  18.     }
  19.     /**
  20.      * {@inheritdoc}
  21.      *
  22.      * @param PartnerableInterface $subject
  23.      */
  24.     protected function voteOnAttribute(string $attribute$subjectTokenInterface $token): bool
  25.     {
  26.         $user $token->getUser();
  27.         if (!$user instanceof User) {
  28.             return false;
  29.         }
  30.         if ($attribute === self::PARTNERABLE_ACCESS) {
  31.             if ($user->isSuperAdmin()) {
  32.                 return true;
  33.             }
  34.             $partnerId $user->getPartner() ? $user->getPartner()->getId() : null;
  35.             return $subject->getPartner() && $subject->getPartner()->getId() === $partnerId;
  36.         }
  37.         return false;
  38.     }
  39. }