CotisationVoter.php 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. <?php
  2. declare(strict_types=1);
  3. namespace App\Security\Voter;
  4. use App\ApiResources\Cotisation\Cotisation;
  5. use App\Entity\Access\Access;
  6. use App\Service\Network\Utils as NetworkUtils;
  7. use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
  8. use Symfony\Component\Security\Core\Authorization\Voter\Voter;
  9. use Symfony\Bundle\SecurityBundle\Security;
  10. use Symfony\Component\Security\Core\User\UserInterface;
  11. class CotisationVoter extends Voter
  12. {
  13. public function __construct(
  14. private Security $security,
  15. private NetworkUtils $networkUtils)
  16. { }
  17. protected function supports($attribute, $subject): bool
  18. {
  19. return in_array($attribute, ['COTISATION_CALL']) && $subject instanceof Cotisation;
  20. }
  21. /**
  22. * @param string $attribute
  23. * @param mixed $subject
  24. * @param TokenInterface $token
  25. * @return bool
  26. */
  27. protected function voteOnAttribute(string $attribute, $subject, TokenInterface $token): bool
  28. {
  29. /** @var Access $user */
  30. $user = $token->getUser();
  31. // if the user is anonymous, do not grant access
  32. if (!$user instanceof UserInterface) {
  33. return false;
  34. }
  35. if($subject->getOrganizationId() !== $user->getOrganization()->getId()){
  36. return false;
  37. }
  38. return $this->security->isGranted('ROLE_COTISATION') &&
  39. $this->networkUtils->isCMFAndActiveNow($user->getOrganization());
  40. }
  41. }