Advertisement
Guest User

Untitled

a guest
Sep 18th, 2018
143
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.96 KB | None | 0 0
  1. /**
  2. * Function to register new user
  3. * @Route("/register", name="register")
  4. * @param Request $request
  5. * @param UserPasswordEncoderInterface $passwordEncoder
  6. * @param \Swift_Mailer $mailer
  7. * @return \Symfony\Component\HttpFoundation\Response
  8. * @throws \Exception
  9. */
  10. public function registerAction(Request $request, UserPasswordEncoderInterface $passwordEncoder, \Swift_Mailer $mailer)
  11. {
  12. $user = new User();
  13.  
  14. $form = $this->createForm(UserType::class, $user);
  15. $form->handleRequest($request);
  16.  
  17. // registration pass
  18. if ($form->isSubmitted() && $form->isValid() && $this->captchaverify($request->get('g-recaptcha-response'))) {
  19.  
  20. $entityManager = $this->getDoctrine()->getManager();
  21.  
  22. // find and set account type for new user
  23. $member = $entityManager->getRepository(AccountType::class)
  24. ->findOneBy(array('typeName' => 'Member'));
  25. $user->setAccountType($member);
  26.  
  27. // encrypt password -> assign to user
  28. $password = $passwordEncoder->encodePassword($user, $user->getPlainPassword());
  29. $user->setPassword($password);
  30.  
  31. // assign create account date
  32. $user->setJoinDate(new \DateTime);
  33. $user->setEnable(false);
  34.  
  35. // generate and assign link for email confirmation
  36. $key = $this->getToken(50);
  37. $confirm = new Confirm($user, $key);
  38.  
  39. // push data
  40. $entityManager->persist($user);
  41. $entityManager->persist($confirm);
  42. $entityManager->flush();
  43.  
  44. // set-up email and send it to user
  45. $email = 'gargulec5218@gmail.com';
  46. $message = (new \Swift_Message(
  47. 'Hello '. $user->getUsername()))
  48. ->setFrom($email)
  49. ->setTo($user->getEmail())
  50. ->setBody(
  51. $this->renderView('emails/registration.html.twig', array(
  52. 'confirmLink' => $this->generateUrl('registrationConfirm', array(
  53. 'confirmKey' => $key
  54. ),
  55. UrlGeneratorInterface::ABSOLUTE_URL)
  56. )),
  57. 'text/html'
  58. );
  59. $mailer->send($message);
  60.  
  61. return $this->render('security/confirm.html.twig', array(
  62. 'confirmed' => 'no'
  63. ));
  64. }
  65.  
  66. // bad captcha
  67. if($form->isSubmitted() && $form->isValid() && !$this->captchaverify($request->get('g-recaptcha-response'))){
  68. $this->addFlash(
  69. 'error',
  70. 'Captcha Require'
  71. );
  72. }
  73.  
  74. // render registration page
  75. return $this->render('security/register.html.twig', array(
  76. 'form' => $form->createView()
  77. ));
  78. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement