src/Controller/Security/SecurityController.php line 20

  1. <?php
  2. namespace App\Controller\Security;
  3. use App\Entity\User;
  4. use App\Form\RegistrationFormType;
  5. use App\Repository\UserRepository;
  6. use Doctrine\ORM\EntityManagerInterface;
  7. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  8. use Symfony\Component\HttpFoundation\Request;
  9. use Symfony\Component\HttpFoundation\Response;
  10. use Symfony\Component\PasswordHasher\Hasher\UserPasswordHasherInterface;
  11. use Symfony\Component\Routing\Annotation\Route;
  12. use Symfony\Component\Security\Http\Authentication\AuthenticationUtils;
  13. class SecurityController extends AbstractController
  14. {
  15.     #[Route(path'/login'name'app_login')]
  16.     public function login(AuthenticationUtils $authenticationUtils,UserRepository $userRepository): Response
  17.     {
  18.         //test has user
  19.         if ($this->getUser()) {
  20.             $user $this->getUser();
  21.             if(in_array('ROLE_ADMIN',$user->getRoles())){
  22.                 return $this->redirectToRoute('app_admin_home');
  23.             }
  24.             else{
  25.                 return $this->redirectToRoute('app_pos_home');
  26.             }
  27.         }
  28.         // get the login error if there is one
  29.         $error $authenticationUtils->getLastAuthenticationError();
  30.         // last username entered by the user
  31.         $lastUsername $authenticationUtils->getLastUsername();
  32.         return $this->render('security/login.html.twig', [
  33.             'last_username' => $lastUsername,
  34.             'error' => $error,
  35.             'users' => $userRepository->findAll()
  36.         ]);
  37.     }
  38.     #[Route('/register'name'app_register')]
  39.     public function register(Request $request,
  40.                              UserPasswordHasherInterface $userPasswordHasher,
  41.                              EntityManagerInterface $entityManager): Response
  42.     {
  43.         //test has user
  44.         if ($this->getUser()) {
  45.             $user $this->getUser();
  46.             if(in_array('ROLE_ADMIN',$user->getRoles())){
  47.                 return $this->redirectToRoute('app_admin_home');
  48.             }
  49.             else{
  50.                 return $this->redirectToRoute('app_pos_home');
  51.             }
  52.         }
  53.         $user = new User();
  54.         $form $this->createForm(RegistrationFormType::class, $user);
  55.         $form->handleRequest($request);
  56.         if ($form->isSubmitted() && $form->isValid()) {
  57.             // encode the plain password
  58.             $user->setPassword(
  59.                 $userPasswordHasher->hashPassword(
  60.                     $user,
  61.                     $form->get('plainPassword')->getData()
  62.                 )
  63.             );
  64.             $entityManager->persist($user);
  65.             $entityManager->flush();
  66.             // do anything else you need here, like send an email
  67.             return $this->redirectToRoute('app_login');
  68.         }
  69.         return $this->render('security/register.html.twig', [
  70.             'registrationForm' => $form->createView(),
  71.         ]);
  72.     }
  73.     #[Route(path'/logout'name'app_logout')]
  74.     public function logout(): void
  75.     {
  76.         throw new \LogicException('This method can be blank - it will be intercepted by the logout key on your firewall.');
  77.     }
  78. }