src/Controller/PublicController.php line 40

Open in your IDE?
  1. <?php
  2. namespace App\Controller;
  3. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  4. use Symfony\Component\HttpFoundation\Response;
  5. use Symfony\Component\Routing\Annotation\Route;
  6. use Symfony\Component\Security\Http\Authentication\AuthenticationUtils;
  7. use Symfony\Contracts\Translation\TranslatorInterface;
  8. use Doctrine\ORM\EntityManagerInterface;
  9. use UltraMsg\WhatsAppApi;
  10. class PublicController extends AbstractController
  11. {
  12.     private $translator;
  13.     private $em;
  14.     public function __construct(TranslatorInterface $translatorEntityManagerInterface $em)
  15.     {
  16.         $this->translator $translator;
  17.         $this->em $em;
  18.     }
  19.     /**
  20.      * @Route("/", name="public_home")
  21.      */
  22.     public function public_home()
  23.     {
  24.         if ($this->getUser()) {
  25.             if ($this->getUser()->hasRole('ROLE_ADMIN')) {
  26.                 return $this->redirectToRoute('admin');
  27.             }
  28.         }
  29.         return $this->redirectToRoute('public_login');
  30.     }
  31.     /**
  32.      * @Route("/login", name="public_login")
  33.      */
  34.     public function public_login(AuthenticationUtils $authenticationUtils): Response
  35.     {
  36.         if ($this->getUser()) {
  37.             return $this->redirectToRoute('public_home');
  38.         }
  39.         $error $authenticationUtils->getLastAuthenticationError();
  40.         if ($error) {
  41.             $errorMsg null;
  42.             if ($error instanceof \Symfony\Component\Security\Core\Exception\BadCredentialsException) {
  43.                 $errorMsg $this->translator->trans('messages.errors.login.badCredentials');
  44.             } else {
  45.                 $errorMsg $error->getMessageKey();
  46.             }
  47.             $this->addFlash('danger'$errorMsg);
  48.         }
  49.         return $this->render('public/login.html.twig', [
  50.             'last_username' => $authenticationUtils->getLastUsername()
  51.         ]);
  52.     }
  53.     /**
  54.      * @Route("/logout", name="public_logout")
  55.      */
  56.     public function public_logout()
  57.     {
  58.     }
  59. }