vendor/nellapp/sdk-bundle/src/Permission/Security/Voter/SharableChannelPermissionVoter.php line 11

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\Channel\Entity\ChannelInterface;
  5. use Nellapp\Bundle\SDKBundle\Permission\SharablePermission\SharableChannelPermissionService;
  6. use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
  7. use Symfony\Component\Security\Core\Authorization\Voter\Voter;
  8. class SharableChannelPermissionVoter extends Voter
  9. {
  10. public function __construct(
  11. private SharableChannelPermissionService $sharablePermissionService,
  12. )
  13. {
  14. }
  15. public function supportsAttribute(string $attribute): bool
  16. {
  17. return str_starts_with($attribute, 'CHANNEL_USER_PERM');
  18. }
  19. public function supportsType(string $subjectType): bool
  20. {
  21. return is_subclass_of($subjectType, ChannelInterface::class);
  22. }
  23. protected function supports(string $attribute, $subject): bool
  24. {
  25. return $this->supportsAttribute($attribute) && $this->supportsType($subject::class);
  26. }
  27. /**
  28. * @param string $attribute
  29. * @param ChannelInterface $subject
  30. * @param TokenInterface $token
  31. * @return bool
  32. */
  33. protected function voteOnAttribute(string $attribute, $subject, TokenInterface $token): bool
  34. {
  35. $user = $token->getUser();
  36. if (!$user instanceof UserInterface) {
  37. return false;
  38. }
  39. $channelUserData = $user->getChannelUserDataByChannel($subject);
  40. if ($channelUserData === null) {
  41. return false;
  42. }
  43. return $this->sharablePermissionService->hasAccess($channelUserData, $subject, $attribute);
  44. }
  45. }