src/Security/PaypalPayerDetailVoter.php line 12

Open in your IDE?
  1. <?php
  2. namespace App\Security;
  3. use App\Entity\Report\PaypalPayerDetail;
  4. use App\Entity\System\Employee;
  5. use App\Entity\System\Role;
  6. use App\Service\SecurityService;
  7. use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
  8. use Symfony\Component\Security\Core\Authorization\Voter\Voter;
  9. class PaypalPayerDetailVoter extends Voter
  10. {
  11. public const SHOW = 'show';
  12. private SecurityService $securityService;
  13. public function __construct(SecurityService $securityService)
  14. {
  15. $this->securityService = $securityService;
  16. }
  17. /**
  18. * @param PaypalPayerDetail $subject
  19. */
  20. protected function supports(string $attribute, $subject): bool
  21. {
  22. if (!(get_class($subject) === PaypalPayerDetail::class)) {
  23. return false;
  24. }
  25. return $this->supportsAttribute($attribute);
  26. }
  27. public function supportsAttribute(string $attribute): bool
  28. {
  29. $voter = new \ReflectionClass(__CLASS__);
  30. $allowedValues = $voter->getConstants();
  31. if (!\array_key_exists($attribute, \array_flip($allowedValues))) {
  32. return false;
  33. }
  34. return true;
  35. }
  36. /**
  37. * @param PaypalPayerDetail $paypalPayerDetail
  38. */
  39. protected function voteOnAttribute(string $attribute, $paypalPayerDetail, TokenInterface $token): bool
  40. {
  41. $user = $token->getUser();
  42. if (!$user instanceof Employee) {
  43. return false;
  44. }
  45. if ($attribute !== self::SHOW) {
  46. throw new \LogicException('This code should not be reached!');
  47. }
  48. return $this->showAllowed($paypalPayerDetail, $user);
  49. }
  50. private function showAllowed(PaypalPayerDetail $paypalPayerDetail, Employee $user): bool
  51. {
  52. if ($this->securityService->isGranted(Role::ROLE_EMPLOYEE_DEVELOPER)) {
  53. return true;
  54. }
  55. return false;
  56. }
  57. }