src/Security/ScheduleAuthenticator.php line 64

Open in your IDE?
  1. <?php
  2. namespace App\Security;
  3. use App\Entity\User;
  4. use App\Exception\SiteClosedException;
  5. use App\Services\ScheduleService;
  6. use Symfony\Component\HttpFoundation\RedirectResponse;
  7. use Symfony\Component\HttpFoundation\Request;
  8. use Symfony\Component\HttpFoundation\Response;
  9. use Symfony\Component\Routing\RouterInterface;
  10. use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
  11. use Symfony\Component\Security\Core\Exception\AuthenticationException;
  12. use Symfony\Component\Security\Core\Security;
  13. use Symfony\Component\Security\Http\Authenticator\AbstractAuthenticator;
  14. use Symfony\Component\Security\Http\Authenticator\Passport\Badge\UserBadge;
  15. use Symfony\Component\Security\Http\Authenticator\Passport\Credentials\CustomCredentials;
  16. use Symfony\Component\Security\Http\Authenticator\Passport\Passport;
  17. use Symfony\Component\Security\Http\Authenticator\Passport\PassportInterface;
  18. use Symfony\Component\Security\Http\Util\TargetPathTrait;
  19. use Symfony\Contracts\Translation\TranslatorInterface;
  20. class ScheduleAuthenticator extends AbstractAuthenticator
  21. {
  22.     use TargetPathTrait;
  23.     public const CLOSED_ROUTE 'site_closed';
  24.     private TranslatorInterface $translator;
  25.     private Security $security;
  26.     private ScheduleService $scheduleService;
  27.     private RouterInterface $router;
  28.     public function __construct(
  29.         TranslatorInterface $translator,
  30.         Security $security,
  31.         ScheduleService $scheduleService,
  32.         RouterInterface $router
  33.     ) {
  34.         $this->security $security;
  35.         $this->translator $translator;
  36.         $this->scheduleService $scheduleService;
  37.         $this->router $router;
  38.     }
  39.     public function supports(Request $request): ?bool
  40.     {
  41.         //dd('supports');
  42.         $authorizedRoutes = ['app_login''app_logout''site_closed'];
  43.         if (in_array($request->attributes->get('_route'), $authorizedRoutes)) {
  44.             return false;
  45.         }
  46.         return !$this->security->isGranted('ROLE_SAAS') && !$this->security->isGranted('ROLE_OWNER');
  47.     }
  48.     public function authenticate(Request $request): PassportInterface
  49.     {
  50.         return new Passport(
  51.             new UserBadge('', function () {
  52.                 return $this->security->getUser();
  53.             }),
  54.             new CustomCredentials(function ($credentialsUser $user) {
  55.                 if ($this->scheduleService->isSiteClosed($user)) {
  56.                     throw new SiteClosedException($this->translator->trans('site_closed'));
  57.                 }
  58.                 return true;
  59.             }, null)
  60.         );
  61.     }
  62.     public function onAuthenticationSuccess(Request $requestTokenInterface $tokenstring $firewallName): ?Response
  63.     {
  64.         return null;
  65.     }
  66.     public function onAuthenticationFailure(Request $requestAuthenticationException $exception): ?Response
  67.     {
  68.         if ($exception instanceof SiteClosedException) {
  69.             return new RedirectResponse($this->router->generate(self::CLOSED_ROUTE));
  70.         }
  71.         return null;
  72.     }
  73. }