src/Controller/UserController.php line 111

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\HttpFoundation\Request;
  6. use Symfony\Component\Routing\Annotation\Route;
  7. use App\Repository\UserRepository;
  8. use Symfony\Component\Mailer\MailerInterface;
  9. use Symfony\Component\Mime\Email;
  10. use Symfony\Component\Mailer\Transport;
  11. use Symfony\Component\Mailer\Mailer;
  12. use Symfony\Component\Form\FormBuilderInterface;
  13. use Symfony\Component\Form\Extension\Core\Type\PasswordType;
  14. use Symfony\Component\Form\Extension\Core\Type\RepeatedType;
  15. use App\Form\ChangePasswordType;
  16. use Symfony\Component\Security\Core\User\User;
  17. class UserController extends AbstractController
  18. {
  19.     private $userRepository;
  20.     public function __construct(UserRepository $userRepository)
  21.     {
  22.         $this->userRepository $userRepository;
  23.     }
  24.     /**
  25.      * @Route("/login", name="app_login", methods={"POST"})
  26.      */
  27.     public function login(Request $request): Response
  28.     {
  29.         $request_data json_decode($request->getContent(), true);
  30.         $email $request_data["email"];
  31.         $password $request_data["password"];
  32.         $isAuthenticated $this->userRepository->authenticate(strval($email), strval($password));
  33.         if (empty($isAuthenticated)) {
  34.             $check $this->userRepository->checkEmail(strval($email));
  35.             if (empty($check)) {
  36.                 return $this->json(['Failure' => 'Wrong email or password.']);
  37.             } else {
  38.                 if ($check[0]['password'] === null) {
  39.                     //dd($check[0]['password']);
  40.                     //Update password for existing account
  41.                     $this->userRepository->updatePassword(strval($email), strval($password));
  42.                     return $this->json(['Success' => 'Password Updated.']);
  43.                 } else {
  44.                     return $this->json(['Failure' => 'Wrong email or password.']);
  45.                 }
  46.             }
  47.         } else {
  48.             return $this->json(['Success' => 'Successfuly LoggedIn.']);
  49.         }
  50.     }
  51.     /**
  52.      * @Route("/signup", name="sign_up", methods={"POST"})
  53.      * 
  54.      * To Do: Needs to send an email 
  55.      */
  56.     public function signup(Request $request): Response
  57.     {
  58.         $request_data json_decode($request->getContent(), true);
  59.         $user_data $this->userRepository->checkEmail($request_data["email"]);
  60.         if (empty($user_data)) {
  61.             $r $this->userRepository->register($request_data["firstName"], $request_data["lastName"], $request_data["phoneNo"], $request_data["email"], $request_data["password"]);
  62.             $result $this->userRepository->fetchId($request_data["email"]);
  63.             $transport Transport::fromDsn('smtp://localhost');
  64.             $mailer = new Mailer($transport);
  65.             $email $request_data["email"];
  66.             $email = (new Email())
  67.             ->from('info@geneva-barbers.ch')
  68.             ->to($email)
  69.             //->cc('cc@example.com')
  70.             //->bcc('bcc@example.com')
  71.             //->replyTo('fabien@example.com')
  72.             //->priority(Email::PRIORITY_HIGH)
  73.             ->subject('Geneva Barbers - Bienvenue!')
  74.             ->text('Geneva Barbers - Bienvenue!')
  75.             ->html("<h1>Geneva Barbers - Bienvenue!</h1></br><p>Merci d'être famille de Geneva Barbers. Vous pouvez désormais réserver l'heure pour couper votre cheveaux très facilement</p>");
  76.             $mailer->send($email);
  77.             return $this->json(['Success' => 'Successfuly Registered.''user_id' => $result[0]["id"]]);
  78.         } else {
  79.             return $this->json(['Failure' => 'Email already exists!']);
  80.         }
  81.     }
  82.     /**
  83.      * @Route("/updateUser", name="update", methods={"POST"})
  84.      */
  85.     public function updateUser(Request $request): Response
  86.     {
  87.         $request_data json_decode($request->getContent(), true);
  88.         $user_data $this->userRepository->checkEmail($request_data["email"]);
  89.         if (!empty($user_data)) {
  90.             $r $this->userRepository->updateUser($request_data["firstName"], $request_data["lastName"], $request_data["phoneNo"], $request_data["email"], $request_data["password"]);
  91.             $result $this->userRepository->fetchId($request_data["email"]);
  92.             return $this->json(['Success' => 'Successfuly Updated.''user_id' => $result[0]["id"]]);
  93.         } else {
  94.             return $this->json(['Failure' => 'User Does not exist!']);
  95.         }
  96.     }
  97.     /**
  98.      * @Route("/barbers", name="fetch_barbers", methods={"GET"})
  99.      * lists the barbers
  100.      */
  101.     public function fetchBarbers(): Response
  102.     {
  103.         $result $this->userRepository->fetchBarbers();
  104.         return $this->json($result);
  105.     }
  106.     /**
  107.      * @Route("/delete", name="delete_user", methods={"POST"})
  108.      */
  109.     public function deleteUser(Request $request): Response
  110.     {
  111.         $request_data json_decode($request->getContent(), true);
  112.         $email $request_data["email"];
  113.         $result $this->userRepository->deleteUser($email);
  114.         return $this->json($result);
  115.     }
  116.     /**
  117.      * @Route("/user", name="fetch_user", methods={"POST"})
  118.      */
  119.     public function fetchUserInfo(Request $request): Response
  120.     {
  121.         $request_data json_decode($request->getContent(), true);
  122.         $email $request_data["email"];
  123.         $result $this->userRepository->fetchUser($email);
  124.         return $this->json($result);
  125.     }
  126.     /**
  127.      * @Route("/cancel", name="cancel_appointment", methods={"POST"})
  128.      */
  129.     public function cancelAppointment(Request $request): Response
  130.     {
  131.         $request_data json_decode($request->getContent(), true);
  132.         $id $request_data["id"];
  133.         $result $this->userRepository->cancelAppointment($id);
  134.         return $this->json($result);
  135.     }
  136.     /**
  137.      * @Route("/play", name="play", methods={"POST"})
  138.      */
  139.     public function play(Request $request): Response
  140.     {
  141.         $request_data json_decode($request->getContent(), true);
  142.         $email $request_data["email"];
  143.         $check $this->userRepository->fetchUser($email);
  144.         return $this->json($check);
  145.     }
  146.     /**
  147.      * @Route("/forget", name="forget_password", methods={"POST"})
  148.      */
  149.     public function forgetPassword(Request $request): Response
  150.     {
  151.         $request_data json_decode($request->getContent(), true);
  152.         $email $request_data["email"];
  153.         $check $this->userRepository->fetchUser($email);
  154.         
  155.         //return $this->json($firstName);
  156.         $transport Transport::fromDsn('smtp://localhost');
  157.         $mailer = new Mailer($transport);
  158.         
  159.         if ($check){
  160.             $id $check[0]['id'];
  161.             $firstName $check[0]['firstName'];
  162.             $email = (new Email())
  163.             ->from('info@geneva-barbers.ch')
  164.             ->to($email)
  165.             //->cc('cc@example.com')
  166.             //->bcc('bcc@example.com')
  167.             //->replyTo('fabien@example.com')
  168.             //->priority(Email::PRIORITY_HIGH)
  169.             ->subject('Geneva Barbers - Réinitialiserr mot de passe')
  170.             ->text('Geneva Barbers - Réinitialiser mot de passe')
  171.             ->html("<h1>Réinitialiser ton mot de passe</h1></br><p>Clique sur le lien pour réinitialiser ton mot de passe: https://api.abc-barber.ch/forgot/".$id."/".$firstName);
  172.             $mailer->send($email);
  173.         } else {
  174.             $email = (new Email())
  175.             ->from('info@geneva-barbers.ch')
  176.             ->to($email)
  177.             //->cc('cc@example.com')
  178.             //->bcc('bcc@example.com')
  179.             //->replyTo('fabien@example.com')
  180.             //->priority(Email::PRIORITY_HIGH)
  181.             ->subject("Couldn't find your email")
  182.             ->text("Couldn't find your email in our Database. Please sign up if you have an account yet!")
  183.             ->html("<p>Couldn't find your email in our Database. Please sign up if you have an account yet!</p>");
  184.             $mailer->send($email);
  185.         }
  186.         
  187.         return $this->json($email);
  188.     }
  189.     /**
  190.      * @Route("/forgot/{id}/{firstName}", name="insitialize")
  191.      * TO DO
  192.      * This link is generated in the previous function and sent to the user by email. 
  193.      * When the user clicks on it, it first checks the id with the firstName and if it matches, then it lets the users modify the password
  194.      * The submitted form should take the new password and update it on the database 
  195.      */
  196.     public function initialize($id$firstNameRequest $request): Response
  197.     {
  198.         $checkid  $this->userRepository->checkid($id$firstName);
  199.         $form $this->createForm(ChangePasswordType::class);
  200.         $form->handleRequest($request);
  201.         if ($checkid){
  202.             if ($form->isSubmitted() && $form->isValid()){
  203.                 $password $form->get('plainPassword')->getData();
  204.                 $this->userRepository->passwordUpdate($id$password);
  205.                 return $this->render('successful.html.twig');
  206.             }
  207.             return $this->render('initialize.html.twig', [
  208.                 'firstName' => $firstName,
  209.                 'form' => $form->createView()
  210.             ]);
  211.         } else {
  212.             return $this->render('makesure.html.twig');
  213.         }  
  214.     }
  215.     
  216. }