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