vendor/nellapp/sdk-bundle/src/Permission/Security/Voter/UsersResourceVoter.php line 10

Open in your IDE?
  1. <?php
  2. namespace Nellapp\Bundle\SDKBundle\Permission\Security\Voter;
  3. use Nellapp\Bundle\SDKBundle\Auth\Entity\UserInterface;
  4. use Nellapp\Bundle\SDKBundle\Permission\UsersResource\UsersResourceInterface;
  5. use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
  6. use Symfony\Component\Security\Core\Authorization\Voter\Voter;
  7. class UsersResourceVoter extends Voter
  8. {
  9. const USER_IN_RESOURCE = 'USER_IN_RESOURCE';
  10. public function supportsType(string $subjectType): bool
  11. {
  12. return is_subclass_of($subjectType, UsersResourceInterface::class);
  13. }
  14. public function supportsAttribute(string $attribute): bool
  15. {
  16. return $attribute === self::USER_IN_RESOURCE;
  17. }
  18. protected function supports(string $attribute, $subject): bool
  19. {
  20. return $this->supportsAttribute($attribute) && $this->supportsType($subject::class);
  21. }
  22. /**
  23. * @param string $attribute
  24. * @param UsersResourceInterface $subject
  25. * @param TokenInterface $token
  26. * @return bool
  27. */
  28. protected function voteOnAttribute(string $attribute, $subject, TokenInterface $token): bool
  29. {
  30. $user = $token->getUser();
  31. if (!$user instanceof UserInterface) {
  32. return false;
  33. }
  34. return $subject->hasUser($user);
  35. }
  36. }