src/Controller/SecurityController.php line 22

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 Symfony\Component\Security\Http\Authentication\AuthenticationUtils;
  8. use Symfony\Component\Security\Core\Encoder\UserPasswordEncoderInterface;
  9. use Doctrine\ORM\EntityManagerInterface;
  10. use App\Services\RoleService;
  11. use App\Form\SigninEndType;
  12. use App\Entity\User;
  13. class SecurityController extends AbstractController
  14. {
  15. protected $em;
  16. public function __construct(EntityManagerInterface $entityManager, RoleService $roleService) {
  17. $this->em = $entityManager;
  18. $this->roleService = $roleService;
  19. }
  20. /**
  21. * @Route("/login", name="app_login")
  22. */
  23. public function login(AuthenticationUtils $authenticationUtils): Response
  24. {
  25. if ($this->getUser()) {
  26. $userCurrent = $this->get('security.token_storage')->getToken()->getUser();
  27. if($this->roleService->isGranted('ROLE_ADMIN', $userCurrent)){
  28. return $this->redirectToRoute('recipe_list');
  29. }
  30. else if($this->roleService->isGranted('ROLE_BRAND_AMBASSADOR', $userCurrent)){
  31. return $this->redirectToRoute('recipe_list');
  32. }
  33. else if($this->roleService->isGranted('ROLE_SELLER', $userCurrent)){
  34. return $this->redirectToRoute('recipe_list');
  35. }
  36. }
  37. // get the login error if there is one
  38. $error = $authenticationUtils->getLastAuthenticationError();
  39. // last username entered by the user
  40. $lastUsername = $authenticationUtils->getLastUsername();
  41. return $this->render('security/login.html.twig', [
  42. 'last_username' => $lastUsername,
  43. 'error' => $error
  44. ]);
  45. }
  46. /**
  47. * @Route("/logout", name="app_logout")
  48. */
  49. public function logout()
  50. {
  51. throw new \LogicException('This method can be blank - it will be intercepted by the logout key on your firewall.');
  52. }
  53. /**
  54. * @Route("/signin-end/{token}/", name="signin_end")
  55. */
  56. public function signinEndAction(Request $request, $token, UserPasswordEncoderInterface $passwordEncoder)
  57. {
  58. //Load parameter
  59. $token = $request->get('token');
  60. $user = $this->em->getRepository('App:User')
  61. ->findOneBy(Array("tokenReset" => $token));
  62. $userCurrent = $this->get('security.token_storage')->getToken()->getUser();
  63. if($userCurrent != "anon."){
  64. $this->addFlash('danger','You are already logged into another account, you cannot complete a registration.');
  65. return $this->redirectToRoute('app_login');
  66. }
  67. if(!$user){
  68. $this->addFlash('danger',"This URL is not functional");
  69. return $this->redirectToRoute('app_login');
  70. }
  71. $form = $this->createForm(SigninEndType::class, $user);
  72. $form->handleRequest($request);
  73. if($form->isSubmitted() && $form->isValid()) {
  74. $password = $passwordEncoder->encodePassword($user, $user->getPlainPassword());
  75. $user->setPassword($password);
  76. $user->setTokenReset(null);
  77. $user->setStatus('ACTIVE');
  78. $this->em->persist($user);
  79. $this->em->flush();
  80. //Send mail activation
  81. //$this->get('fos_user.mailer')->sendConfirmationEmailMessage($user);
  82. $this->addFlash('success','Your password has been completed successfully. You can now connect');
  83. return $this->redirectToRoute('app_login');
  84. }
  85. return $this->render('registration/signinEnd.html.twig', Array(
  86. 'form' => $form->createView(),
  87. 'token' => $token
  88. ));
  89. }
  90. }