src/Lib/Payment/Voter/PaymentLimitsVoter.php line 16

Open in your IDE?
  1. <?php
  2. /*
  3.  * Author: Dominik Piekarski <code@dompie.de>
  4.  * Created at: 2022/07/05 06:50
  5.  */
  6. declare(strict_types=1);
  7. namespace App\Lib\Payment\Voter;
  8. use App\Dictionary\Permission;
  9. use App\Entity\Account;
  10. use App\Security\ApiUser;
  11. use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
  12. use Symfony\Component\Security\Core\Authorization\Voter\Voter;
  13. class PaymentLimitsVoter extends Voter
  14. {
  15.     protected function supports(string $attribute$subject): bool
  16.     {
  17.         return Permission::ALLOWED_TO_BOOK === $attribute && $subject instanceof Account;
  18.     }
  19.     /**
  20.      * @param Account $subject
  21.      *
  22.      * @return bool|int
  23.      */
  24.     protected function voteOnAttribute(string $attribute$subjectTokenInterface $token): bool
  25.     {
  26.         /** @var ApiUser $apiUser */
  27.         $apiUser $token->getUser();
  28.         if ($apiUser->getIsAdmin()) {
  29.             return true;
  30.         }
  31.         $currentAccount $apiUser->getAccount();
  32.         if ($currentAccount instanceof Account && $currentAccount->getId() !== $subject->getId()) {
  33.             throw PaymentVoterException::notYourAccount();
  34.         }
  35.         if (null === $member $subject->getMember()) {
  36.             throw PaymentVoterException::notAMember();
  37.         }
  38.         return true;
  39.     }
  40. }