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)
  15. {
  16. $allowedAttributes = [
  17. self::EDIT,
  18. self::DELETE,
  19. ];
  20. if (!in_array(strtolower($attribute), $allowedAttributes)) {
  21. return false;
  22. }
  23. if (!$subject instanceof Ticket) {
  24. return false;
  25. }
  26. return true;
  27. }
  28. /**
  29. * {@inheritdoc}
  30. */
  31. protected function voteOnAttribute($attribute, $subject, TokenInterface $token)
  32. {
  33. $user = $token->getUser();
  34. if (!$user instanceof User) {
  35. return false;
  36. }
  37. $allowedRoles = [
  38. User::ROLE_ADMIN,
  39. User::ROLE_OPERATOR,
  40. ];
  41. return !empty(array_intersect($allowedRoles, $user->getRoles()));
  42. }
  43. }