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

Open in your IDE?
  1. <?php
  2. namespace Nellapp\Bundle\SDKBundle\Permission\Security\Voter;
  3. use Nellapp\Bundle\SDKBundle\Permission\UserOwner\UserOwnerGetter;
  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 UserOwnerVoter extends Voter
  8. {
  9. const USER_OWNER = 'USER_OWNER';
  10. public function __construct(
  11. private UserOwnerGetter $userOwnerGetter,
  12. ) {}
  13. public function supportsType(string $subjectType): bool
  14. {
  15. return $this->userOwnerGetter->supportsType($subjectType);
  16. }
  17. public function supportsAttribute(string $attribute): bool
  18. {
  19. return $attribute === self::USER_OWNER;
  20. }
  21. protected function supports(string $attribute, $subject): bool
  22. {
  23. return $this->supportsAttribute($attribute) && $this->supportsType($subject::class);
  24. }
  25. protected function voteOnAttribute(string $attribute, $subject, TokenInterface $token): bool
  26. {
  27. $user = $token->getUser();
  28. if (!$user instanceof UserInterface) {
  29. return false;
  30. }
  31. return $this->userOwnerGetter->isOwnerOf($user, $subject);
  32. }
  33. }