src/Controller/SecurityController.php line 97

Open in your IDE?
  1. <?php
  2. /*
  3.  * This file is part of the Symfony package.
  4.  *
  5.  * (c) Fabien Potencier <fabien@symfony.com>
  6.  *
  7.  * For the full copyright and license information, please view the LICENSE
  8.  * file that was distributed with this source code.
  9.  */
  10. namespace App\Controller;
  11. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  12. use Symfony\Component\HttpFoundation\Request;
  13. use Symfony\Component\HttpFoundation\Response;
  14. use Symfony\Component\Routing\Annotation\Route;
  15. use Symfony\Component\Security\Http\Authentication\AuthenticationUtils;
  16. use Symfony\Component\Security\Http\Util\TargetPathTrait;
  17. use Symfony\Component\PasswordHasher\Hasher\UserPasswordHasherInterface;
  18. use App\Entity\User;
  19. use App\Repository\UserRepository;
  20. use Doctrine\Persistence\ManagerRegistry;
  21. use Doctrine\ORM\EntityManagerInterface;
  22. use Symfony\Component\Security\Core\Security;
  23. use Symfony\Bridge\Twig\Mime\TemplatedEmail;
  24. use Symfony\Component\Mailer\MailerInterface;
  25. use Symfony\Component\Mime\Email;
  26. /**
  27.  * Controller used to manage the application security.
  28.  * See https://symfony.com/doc/current/security/form_login_setup.html.
  29.  *
  30.  * @author Ryan Weaver <weaverryan@gmail.com>
  31.  * @author Javier Eguiluz <javier.eguiluz@gmail.com>
  32.  */
  33. class SecurityController extends AbstractController {
  34.     use TargetPathTrait;
  35.     private $security;
  36.     public function __construct(Security $security) {
  37.         $this->security $security;
  38.     }
  39.     #[Route('/login'name'security_login')]
  40.     public function login(Request $requestAuthenticationUtils $helperUserPasswordHasherInterface $passwordHasherManagerRegistry $entityManager): Response {
  41.         // if user is already logged in, don't display the login page again
  42.         if ($this->getUser()) {
  43.             if ($this->security->isGranted('ROLE_ADMIN')) {
  44.                 $this->saveTargetPath($request->getSession(), 'main'$this->generateUrl('admin_booking_list'));
  45.             } else if ($this->security->isGranted('ROLE_COACH')) {
  46.                 $this->saveTargetPath($request->getSession(), 'main'$this->generateUrl('coach_account'));
  47.             } else if ($this->security->isGranted('ROLE_USER')) {
  48.                 $this->saveTargetPath($request->getSession(), 'main'$this->generateUrl('coach_account'));
  49.             }
  50.         }
  51.         // create the user and hash its password
  52. //        $user = new User();
  53. //        $user->setFullName("Visitor");
  54. //        $user->setUsername("user");
  55. //        $user->setEmail("visitor@evenews.com");
  56. //        $user->setRoles(['ROLE_USER']);
  57. //        $user->setEnabled('1');
  58. //
  59. //        $hashedPassword = $passwordHasher->hashPassword($user, '123456');
  60. //        $user->setPassword($hashedPassword);
  61. //
  62. //        $entityManager->persist($user);
  63. //        $entityManager->flush();
  64.         ///end create the user
  65.         // this statement solves an edge-case: if you change the locale in the login
  66.         // page, after a successful login you are redirected to a page in the previous
  67.         // locale. This code regenerates the referrer URL whenever the login page is
  68.         // browsed, to ensure that its locale is always the current one.
  69.         return $this->render('security/login.html.twig', [
  70.                     // last username entered by the user (if any)
  71.                     'last_username' => $helper->getLastUsername(),
  72.                     // last authentication error (if any)
  73.                     'error' => $helper->getLastAuthenticationError(),
  74.         ]);
  75.     }
  76.     /**
  77.      * This is the route the user can use to logout.
  78.      *
  79.      * But, this will never be executed. Symfony will intercept this first
  80.      * and handle the logout automatically. See logout in config/packages/security.yaml
  81.      */
  82.     #[Route('/logout'name'security_logout')]
  83.     public function logout(): void {
  84.         throw new \Exception('This should never be reached!');
  85.     }
  86. }