vendor/nellapp/sdk-bundle/src/Permission/Security/Voter/ChannelUserPermissionResourceVoter.php line 12

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