src/Action/PressSite/Account/SignInAction.php line 19

Open in your IDE?
  1. <?php
  2. namespace App\Action\PressSite\Account;
  3. use App\Action\PressSite\DomainAwareAction;
  4. use App\Contract\PressSite\Language\Switcher\GenericActionInterface;
  5. use App\Entity\PressSite\AccountManager;
  6. use App\Service\PressSite\DomainAwareManager;
  7. use Symfony\Component\HttpFoundation\RedirectResponse;
  8. use Symfony\Component\HttpFoundation\Response;
  9. use Symfony\Component\Routing\RouterInterface;
  10. use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface;
  11. use Symfony\Component\Security\Http\Authentication\AuthenticationUtils;
  12. use Twig\Environment;
  13. /**
  14.  * Class SignInAction.
  15.  */
  16. class SignInAction extends DomainAwareAction implements GenericActionInterface
  17. {
  18.     /**
  19.      * The token manager.
  20.      *
  21.      * @var \Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface
  22.      */
  23.     private $tokenStorage;
  24.     /**
  25.      * The routing manager.
  26.      *
  27.      * @var \Symfony\Component\Routing\RouterInterface
  28.      */
  29.     private $router;
  30.     public function __construct(
  31.         Environment $twig,
  32.         DomainAwareManager $domainAwareManager,
  33.         TokenStorageInterface $tokenStorage,
  34.         RouterInterface $router
  35.     ) {
  36.         parent::__construct($twig$domainAwareManager);
  37.         $this->tokenStorage $tokenStorage;
  38.         $this->router $router;
  39.     }
  40.     /**
  41.      * Responsible for rendering the authentication page.
  42.      *
  43.      * The homepage will actually serve as a login page as well,
  44.      * because in order for a member to access the rest of the pages,
  45.      * they have to login first.
  46.      *
  47.      * @param \Symfony\Component\Security\Http\Authentication\AuthenticationUtils $authenticationUtils
  48.      *   The authentication helper
  49.      *
  50.      * @return \Symfony\Component\HttpFoundation\Response
  51.      *   The response object to return
  52.      */
  53.     public function __invoke(AuthenticationUtils $authenticationUtils): Response
  54.     {
  55.         $token $this->tokenStorage->getToken();
  56.         if ($token && ($member $token->getUser()) && $member instanceof AccountManager) {
  57.             return new RedirectResponse(
  58.                 $this->router->generate($this->getDefaultRedirectRoute(), [
  59.                     '_locale' => $this->getDomainManager()->getCurrentByHostnameAndLocale()->getLanguageCode(),
  60.                 ])
  61.             );
  62.         }
  63.         return $this->render('press_site/actions/account/sign_in.html.twig', [
  64.             'last_user' => $authenticationUtils->getLastUsername(),
  65.             'error' => $authenticationUtils->getLastAuthenticationError(),
  66.         ]);
  67.     }
  68. }