src/Security/Voter/PurchaseItemEtsyVoter.php line 11

Open in your IDE?
  1. <?php
  2. namespace App\Security\Voter;
  3. use App\Entity\PurchaseItemEtsy;
  4. use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
  5. use Symfony\Component\Security\Core\Authorization\Voter\Voter;
  6. use Symfony\Component\Security\Core\Security;
  7. use Symfony\Component\Security\Core\User\UserInterface;
  8. class PurchaseItemEtsyVoter extends Voter
  9. {
  10. public const EDIT = 'purchase_item_etsy_edit';
  11. public function __construct(
  12. private Security $security,
  13. ) {
  14. }
  15. protected function supports(string $attribute, mixed $subject): bool
  16. {
  17. return $attribute == self::EDIT && $subject instanceof PurchaseItemEtsy;
  18. }
  19. protected function voteOnAttribute(string $attribute, mixed $subject, TokenInterface $token): bool
  20. {
  21. $user = $token->getUser();
  22. // if the user is anonymous, do not grant access
  23. if (!$user instanceof UserInterface) {
  24. return false;
  25. }
  26. /** @var PurchaseItemEtsy $purchase */
  27. $purchase = $subject;
  28. if ($purchase->getPurchase()->getUserStore()->getUser() === $user
  29. || $this->security->isGranted('ROLE_ADMIN')
  30. ) {
  31. return true;
  32. }
  33. return false;
  34. }
  35. }