Advertisement
Guest User

Untitled

a guest
May 6th, 2019
124
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.85 KB | None | 0 0
  1. <?php
  2.  
  3. namespace App\Controller;
  4.  
  5. use App\Entity\Account\Account;
  6. use App\Form\ForgottenPasswordType;
  7. use App\Form\RegisterType;
  8. use App\Form\ResetPasswordType;
  9. use App\Repository\AccountRepository;
  10. use App\Repository\RoleRepository;
  11. use App\Service\Mailer;
  12. use App\Service\PIN\PINGeneratorInterface;
  13. use App\Service\Recaptcha;
  14. use App\Service\TokenGenerator;
  15. use App\Service\UserLogger;
  16. use Sensio\Bundle\FrameworkExtraBundle\Configuration\Method;
  17. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  18. use Symfony\Component\HttpFoundation\Request;
  19. use Symfony\Component\HttpFoundation\Response;
  20. use Symfony\Component\Routing\Annotation\Route;
  21. use Symfony\Component\Security\Core\Encoder\UserPasswordEncoderInterface;
  22. use Symfony\Component\Security\Core\Exception\LogicException;
  23. use Symfony\Component\Security\Http\Authentication\AuthenticationUtils;
  24. use Symfony\Component\Translation\TranslatorInterface;
  25.  
  26. class SecurityController extends AbstractController implements UserControllerInterface
  27. {
  28. /**
  29. * @Route("/register", name="register")
  30. * @Method({"GET", "POST"})
  31. */
  32. public function register(Request $request, UserPasswordEncoderInterface $passwordEncoder, PINGeneratorInterface $PINGenerator, TranslatorInterface $translator, RoleRepository $repository): Response
  33. {
  34. $form = $this->createForm(RegisterType::class);
  35. $form->handleRequest($request);
  36.  
  37. if ($form->isSubmitted() && $form->isValid()) {
  38. /** @var Account $account */
  39. $account = $form->getData();
  40.  
  41. $password = $passwordEncoder->encodePassword($account, $account->getPlainPassword());
  42. $account->setPassword($password);
  43. $account->setPIN($PINGenerator->generate());
  44. $account->setCreateTime(new \DateTime());
  45. $account->setRole($repository->findOneBy([
  46. 'role' => 'ROLE_USER'
  47. ]));
  48.  
  49. $em = $this->getDoctrine()->getManager();
  50.  
  51. $em->persist($account);
  52. $em->flush();
  53.  
  54. $this->addFlash('success', $translator->trans('registration.success', [
  55. '%PIN%' => $account->getPIN()
  56. ]));
  57. return $this->redirectToRoute('register');
  58. }
  59.  
  60. return $this->render('user/register.html.twig', [
  61. 'register_form' => $form->createView()
  62. ]);
  63. }
  64.  
  65. /**
  66. * @Route("/forgotten", name="forgotten")
  67. * @Method({"GET", "POST"})
  68. */
  69. public function forgotten(Request $request, AccountRepository $accountRepository, TokenGenerator $tokenGenerator, Mailer $mailer, TranslatorInterface $translator): Response
  70. {
  71. $form = $this->createForm(ForgottenPasswordType::class);
  72. $form->handleRequest($request);
  73.  
  74. if ($form->isSubmitted() && $form->isValid()) {
  75. $em = $this->getDoctrine()->getManager();
  76.  
  77. $user = $accountRepository->findAccountByEmail($form->get('email')->getData());
  78. $user->setResetPasswordToken($tokenGenerator->generateToken());
  79. $user->setResetPasswordTokenCreatedAt(new \DateTime());
  80.  
  81. $em->flush();
  82.  
  83. $mailer->sendResettingMessage($user);
  84.  
  85. $this->addFlash('success', $translator->trans('reset-password.message_send.success'));
  86. return $this->redirectToRoute('forgotten');
  87. }
  88.  
  89. return $this->render('security/forgotten.html.twig', [
  90. 'form' => $form->createView()
  91. ]);
  92. }
  93.  
  94. /**
  95. * @Route("/reset-password", name="reset_password")
  96. */
  97. public function resetPassword(Request $request, AccountRepository $accountRepository, UserPasswordEncoderInterface $passwordEncoder, TranslatorInterface $translator, UserLogger $logger): Response
  98. {
  99. $em = $this->getDoctrine()->getManager();
  100.  
  101. if (!$request->get('token') || !$user = $accountRepository->findAccountByResetPasswordToken($request->get('token'))) {
  102. return $this->redirectToRoute('forgotten');
  103. }
  104.  
  105. if (($user->getResetPasswordTokenCreatedAt())->add(new \DateInterval('PT' . Account::PASSWORD_TOKEN_EXPIRES_HOURS . 'H')) < new \DateTime()) {
  106. $user->setResetPasswordToken(null);
  107. $user->setResetPasswordTokenCreatedAt(null);
  108.  
  109. $em->flush();
  110.  
  111. $this->addFlash('alert', $translator->trans('reset-password.token.expired'));
  112. return $this->redirectToRoute('forgotten');
  113. }
  114.  
  115. $form = $this->createForm(ResetPasswordType::class);
  116. $form->handleRequest($request);
  117.  
  118. if ($form->isSubmitted() && $form->isValid()) {
  119. $user->setPassword($passwordEncoder->encodePassword($user, $form->get('plainPassword')->getData()));
  120. $user->setResetPasswordToken(null);
  121. $user->setResetPasswordTokenCreatedAt(null);
  122.  
  123. $logger->addLog($user, 'RESET_PASSWORD');
  124. $em->flush();
  125.  
  126. $this->addFlash('success', $translator->trans('reset-password.success'));
  127. return $this->redirectToRoute('login');
  128. }
  129.  
  130. return $this->render('security/reset-password.html.twig', [
  131. 'form' => $form->createView()
  132. ]);
  133. }
  134.  
  135. /**
  136. * @Route("/login", name="login")
  137. */
  138. public function login(AuthenticationUtils $authenticationUtils): Response
  139. {
  140. // get the login error if there is one
  141. $error = $authenticationUtils->getLastAuthenticationError();
  142. // last username entered by the user
  143. $lastUsername = $authenticationUtils->getLastUsername();
  144.  
  145. return $this->render('user/homepage.html.twig', ['last_username' => $lastUsername, 'error' => $error, 'articles' => []]);
  146. }
  147.  
  148. /**
  149. * @Route("/logout", name="logout")
  150. */
  151. public function logout()
  152. {
  153. throw new LogicException('logout path is set wrong');
  154. }
  155. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement