src/Security/Blo/PayoutVoter.php line 12

Open in your IDE?
  1. <?php
  2. declare(strict_types=1);
  3. namespace App\Security\Blo;
  4. use App\Entity\Blo\Payout;
  5. use App\Entity\User;
  6. use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
  7. use Symfony\Component\Security\Core\Authorization\Voter\Voter;
  8. class PayoutVoter extends Voter
  9. {
  10.     public const VIEW 'blo_payout_view';
  11.     public const APPROVE 'blo_payout_approve';
  12.     protected function supports(string $attributemixed $subject): bool
  13.     {
  14.         return $subject instanceof Payout
  15.             && \in_array($attribute, [self::VIEWself::APPROVE], true);
  16.     }
  17.     protected function voteOnAttribute(string $attributemixed $subjectTokenInterface $token): bool
  18.     {
  19.         $user $token->getUser();
  20.         if (!$user instanceof User) {
  21.             return false;
  22.         }
  23.         /** @var Payout $payout */
  24.         $payout $subject;
  25.         return match ($attribute) {
  26.             self::VIEW => $this->canView($payout$user),
  27.             self::APPROVE => $this->canApprove($payout$user),
  28.             default => false,
  29.         };
  30.     }
  31.     private function canView(Payout $payoutUser $user): bool
  32.     {
  33.         if (\in_array('ROLE_ADMIN'$user->getRoles(), true)) {
  34.             return true;
  35.         }
  36.         return $payout->getShop()->getOwner() === $user;
  37.     }
  38.     private function canApprove(Payout $payoutUser $user): bool
  39.     {
  40.         return \in_array('ROLE_ADMIN'$user->getRoles(), true);
  41.     }
  42. }