src/Aqarmap/Bundle/ListingBundle/Security/Authorization/Voter/OwnerVoter.php line 10

  1. <?php
  2. namespace Aqarmap\Bundle\ListingBundle\Security\Authorization\Voter;
  3. use Aqarmap\Bundle\ListingBundle\Security\Authorization\OwnerInterface;
  4. use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
  5. use Symfony\Component\Security\Core\Authorization\Voter\Voter;
  6. use Symfony\Component\Security\Core\User\UserInterface;
  7. class OwnerVoter extends Voter
  8. {
  9. protected function voteOnAttribute($attribute, $subject, TokenInterface $token): bool
  10. {
  11. $user = $token->getUser();
  12. if (!$user instanceof UserInterface) {
  13. return false;
  14. }
  15. // Verifies that the user is the owner
  16. $listingOwner = $subject->getUser();
  17. if ($listingOwner->getId() === $user->getId() || ($user->getChildren() && $user->getChildren()->contains($listingOwner))) {
  18. return true;
  19. }
  20. return false;
  21. }
  22. public function supports($attribute, $subject): bool
  23. {
  24. return 'ROLE_OWNER' === $attribute && $subject instanceof OwnerInterface;
  25. }
  26. }