src/Security/Voter/PaymentVoter.php line 15

  1. <?php
  2. namespace App\Security\Voter;
  3. use App\Entity\Authorization;
  4. use App\Entity\Document;
  5. use App\Entity\PaymentRecord;
  6. use App\Enum\RoleType;
  7. use Faker\Provider\Payment;
  8. use Symfony\Bundle\SecurityBundle\Security;
  9. use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
  10. use Symfony\Component\Security\Core\Authorization\Voter\Voter;
  11. use Symfony\Component\Security\Core\User\UserInterface;
  12. use function PHPUnit\Framework\isInstanceOf;
  13. /**
  14. * @extends Voter<string, Payment>
  15. */
  16. class PaymentVoter extends Voter
  17. {
  18. public const SHOW = 'PAYMENT_SHOW';
  19. public const EDIT = 'PAYMENT_EDIT';
  20. public const CHECKOUT = 'PAYMENT_CHECKOUT';
  21. public function __construct(private readonly Security $security)
  22. {
  23. }
  24. protected function supports(string $attribute, mixed $subject): bool
  25. {
  26. return in_array($attribute, [self::SHOW, self::EDIT, self::CHECKOUT]);
  27. }
  28. protected function voteOnAttribute(string $attribute, mixed $subject, TokenInterface $token): bool
  29. {
  30. $user = $token->getUser();
  31. // if the user is anonymous, do not grant access
  32. if (!$user instanceof UserInterface) {
  33. return false;
  34. }
  35. return match ($attribute) {
  36. 'PAYMENT_SHOW' =>
  37. // @phpstan-ignore-next-line
  38. $subject->getEnrollment()->getGuardian()->getUser() === $user
  39. ||
  40. $this->security->isGranted(RoleType::ROLE_EMPLOYEE) ||
  41. $this->security->isGranted(RoleType::ROLE_FINANCIAL_DIRECTOR),
  42. 'PAYMENT_EDIT' =>
  43. $this->security->isGranted(RoleType::ROLE_ADMIN) ||
  44. $this->security->isGranted(RoleType::ROLE_FINANCIAL_DIRECTOR),
  45. 'PAYMENT_CHECKOUT' =>
  46. // @phpstan-ignore-next-line
  47. $subject->getEnrollment()->getGuardian()->getUser() === $user
  48. ||
  49. $this->security->isGranted(RoleType::ROLE_ADMIN),
  50. default => false
  51. };
  52. }
  53. }