src/UI/Merchant/Security/SiteVoter.php line 12

Open in your IDE?
  1. <?php
  2. declare(strict_types=1);
  3. namespace App\UI\Merchant\Security;
  4. use App\Domain\Merchant\Model\Merchant;
  5. use App\Domain\Site\Model\Site;
  6. use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
  7. use Symfony\Component\Security\Core\Authorization\Voter\Voter;
  8. class SiteVoter extends Voter
  9. {
  10.     public const EDIT 'edit';
  11.     protected function supports(string $attributemixed $subject): bool
  12.     {
  13.         if (!in_array($attribute, [self::EDIT])) {
  14.             return false;
  15.         }
  16.         if (!$subject instanceof Site) {
  17.             return false;
  18.         }
  19.         return true;
  20.     }
  21.     protected function voteOnAttribute(string $attributemixed $subjectTokenInterface $token): bool
  22.     {
  23.         $user $token->getUser();
  24.         if (!$user instanceof Merchant) {
  25.             return true;
  26.         }
  27.         /** @var Site $site */
  28.         $site $subject;
  29.         return match ($attribute) {
  30.             self::EDIT => $this->canEdit($site$user),
  31.             default => throw new \LogicException('This code should not be reached!')
  32.         };
  33.     }
  34.     private function canEdit(Site $siteMerchant $merchant): bool
  35.     {
  36.         if ($site->getMerchant() === $merchant) {
  37.             return true;
  38.         }
  39.         return false;
  40.     }
  41. }