src/Infrastructure/Security/LocalisationVoter.php line 10

Open in your IDE?
  1. <?php
  2. namespace App\Infrastructure\Security;
  3. use App\Domain\Common\Entity\User;
  4. use App\Domain\Common\Enum\UserTypeEnum;
  5. use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
  6. use Symfony\Component\Security\Core\Authorization\Voter\Voter;
  7. class LocalisationVoter extends Voter
  8. {
  9. public const SHOW_LOCALISATION = 'SHOW_LOCALISATION';
  10. protected function supports(string $attribute, $subject): bool
  11. {
  12. return self::SHOW_LOCALISATION === $attribute;
  13. }
  14. protected function voteOnAttribute(string $attribute, $subject, TokenInterface $token): bool
  15. {
  16. $grantedUserTypes = [
  17. UserTypeEnum::TYPE_ADMIN,
  18. UserTypeEnum::TYPE_OPAC_VALIDATEUR_N1,
  19. UserTypeEnum::TYPE_OPAC_VALIDATEUR_N2,
  20. UserTypeEnum::TYPE_OPAC_READ_ONLY,
  21. ];
  22. $user = $token->getUser();
  23. if (!$user instanceof User) {
  24. return false;
  25. }
  26. return \in_array($user->getType(), $grantedUserTypes);
  27. }
  28. }