src/AdminBundle/Security/TicketVoter.php line 10

Open in your IDE?
  1. <?php
  2. namespace AdminBundle\Security;
  3. use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
  4. use Symfony\Component\Security\Core\Authorization\Voter\Voter;
  5. use AdminBundle\Entity\Ticket;
  6. use AdminBundle\Entity\User;
  7. class TicketVoter extends Voter
  8. {
  9. private const EDIT = 'edit';
  10. private const DELETE = 'delete';
  11. /**
  12. * {@inheritdoc}
  13. */
  14. protected function supports($attribute, $subject): bool {
  15. $allowedAttributes = [
  16. self::EDIT,
  17. self::DELETE,
  18. ];
  19. if (!in_array(strtolower($attribute), $allowedAttributes)) {
  20. return false;
  21. }
  22. if (!$subject instanceof Ticket) {
  23. return false;
  24. }
  25. return true;
  26. }
  27. /**
  28. * {@inheritdoc}
  29. */
  30. protected function voteOnAttribute($attribute, $subject, TokenInterface $token): bool {
  31. $user = $token->getUser();
  32. if (!$user instanceof User) {
  33. return false;
  34. }
  35. $allowedRoles = [
  36. User::ROLE_ADMIN,
  37. User::ROLE_OPERATOR,
  38. ];
  39. return !empty(array_intersect($allowedRoles, $user->getRoles()));
  40. }
  41. }