src\Controller\LoginController.php line 99

Open in your IDE?
  1. <?php
  2. namespace App\Controller;
  3. use App\Entity\SystemUser;
  4. use App\Entity\User;
  5. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  6. use Symfony\Component\Form\Extension\Core\Type\PasswordType;
  7. use Symfony\Component\Form\Extension\Core\Type\RepeatedType;
  8. use Symfony\Component\Form\Extension\Core\Type\TextType;
  9. use Symfony\Component\Form\FormInterface;
  10. use Symfony\Component\HttpFoundation\Request;
  11. use Symfony\Component\HttpFoundation\Response;
  12. use Symfony\Component\PasswordHasher\Hasher\UserPasswordHasherInterface;
  13. use Symfony\Component\Routing\Annotation\Route;
  14. use Symfony\Component\Security\Http\Authentication\AuthenticationUtils;
  15. use Symfony\Component\Validator\Constraints\Length;
  16. use Symfony\Component\Validator\Constraints\NotBlank;
  17. class LoginController extends AbstractController
  18. {
  19.     private UserPasswordHasherInterface $passwordHasher;
  20.     public function __construct(UserPasswordHasherInterface $passwordHasher){
  21.         $this->passwordHasher $passwordHasher;
  22.     }
  23.     /**
  24.      * @Route("/web/login", name="app_login")
  25.      * @param AuthenticationUtils $authenticationUtils
  26.      * @return Response
  27.      */
  28.     public function login(AuthenticationUtils $authenticationUtils): Response
  29.     {
  30.         // if ($this->getUser()) {
  31.         //     return $this->redirectToRoute('target_path');
  32.         // }
  33.         // get the login error if there is one
  34.         $error $authenticationUtils->getLastAuthenticationError();
  35.         // last username entered by the user
  36.         $lastUsername $authenticationUtils->getLastUsername();
  37.         return $this->render('security/login.html.twig', ['last_username' => $lastUsername'error' => $error]);
  38.     }
  39.     /**
  40.      * @Route("/logout", name="app_logout")
  41.      */
  42.     public function logout()
  43.     {
  44.         throw new \LogicException('This method can be blank - it will be intercepted by the logout key on your firewall.');
  45.     }
  46.     /**
  47.      * @Route("/web/update", name="app_login_update")
  48.      * @return Response
  49.      */
  50.     public function updateUser(): Response
  51.     {
  52.         $em $this->getDoctrine()->getManager();
  53.         $user $em->getRepository(User::class)->findOneBy([
  54.             'phone' => '0716308459'
  55.         ]);
  56.         $user->setPassword($this->passwordHasher->hashPassword(
  57.             $user,
  58.             'mirage'
  59.         ));
  60.         /** @var array $array */
  61.         $array $user->getRoles();
  62. //        dump($array);
  63. //        dump(array_push($array,  'ROLE_WEB'));
  64. //        $user->setRoles(array_push($array, array('ROLE_WEB')));
  65.         $user->setIsActive(true);
  66.         $em->flush();
  67. //        return new Response('update complete', Response::HTTP_OK);
  68.         return $this->render('security/update.html.twig');
  69.     }
  70.     /**
  71.      * @Route("/web/sign_up", name="web_signup")
  72.      */
  73.     public function webSignUp(Request $request)
  74.     {
  75.         $em $this->getDoctrine()->getManager();
  76.         $signUpForm $this->signUpForm();
  77.         $signUpForm->handleRequest($request);
  78.         if($signUpForm->isSubmitted() && $signUpForm->isValid()){
  79.             $phone $signUpForm->get('phone')->getData();
  80.             $password $signUpForm->get('password')->getData();
  81.             $systemUser $em->getRepository(SystemUser::class)->findOneBy([
  82.                 'phone' => $phone
  83.             ]);
  84.             if(!$systemUser){
  85.                 $this->addFlash('error''Your account is not web ready - Contact ADMIN');
  86.                 return $this->redirectToRoute('web_signup');
  87.             }
  88.             $user $em->getRepository('App:User')->findOneBy([
  89.                 'systemUser' => $systemUser
  90.             ]);
  91.             if(!$user){
  92.                 $this->addFlash('error''Your account is not web ready - Contact ADMIN');
  93.                 return $this->redirectToRoute('web_signup');
  94.             }
  95.             if($user->getIsActive()){
  96.                 $this->addFlash('info''User already activated: please login!');
  97.                 return $this->redirectToRoute('app_login');
  98.             }
  99.             if(!in_array('ROLE_WEB'$user->getRoles())){
  100.                 $this->addFlash('warning''Your account is not web ready - Contact ADMIN');
  101.                 return $this->redirectToRoute('web_signup');
  102.             }
  103.             $pass $this->passwordHasher->hashPassword($user$password);
  104.             $user->setPassword($pass);
  105.             $user->setIsActive(true);
  106.             $em->flush();
  107.             $this->addFlash('info''Sign Up successful :-)');
  108.             return $this->redirectToRoute('app_login');
  109.         }
  110.         return $this->render('security/username.html.twig', [
  111.             'form' => $signUpForm->createView()
  112.         ]);
  113.     }
  114.     private function signUpForm(): FormInterface
  115.     {
  116.         $sF $this->get('form.factory')->createNamedBuilder('sign_up_form');
  117.         return $sF
  118.             ->add('phone'TextType::class,[
  119.                 'constraints' => [
  120.                     new NotBlank(),
  121.                     new Length(['min' => 10'minMessage'=> 'Enter a valid phone number'])
  122.                 ]
  123.             ])
  124.             ->add('password'RepeatedType::class,[
  125.                 'constraints' => [
  126.                     new NotBlank(['message' => 'Please enter a password']),
  127.                     new Length(['min' => 8,'minMessage' => 'Password should be at least 8 characters'])
  128.                 ],
  129.                 'type' => PasswordType::class,
  130.                 'invalid_message' => 'The Password fields must match',
  131.                 'required' => true,
  132.                 'first_options' => [
  133.                     'label' => 'Password'
  134.                 ],
  135.                 'second_options' => [
  136.                     'label' => 'Repeat Password'
  137.                 ]
  138.             ])
  139.             ->setAction($this->generateUrl('web_signup'))
  140.             ->setMethod('POST')
  141.             ->getForm();
  142.     }
  143. }