src/Controller/SecurityController.php line 24

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 $entityManagerRoleService $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.         }
  38.         // get the login error if there is one
  39.         $error $authenticationUtils->getLastAuthenticationError();
  40.         // last username entered by the user
  41.         $lastUsername $authenticationUtils->getLastUsername();
  42.         return $this->render('security/login.html.twig', [
  43.             'last_username' => $lastUsername
  44.             'error' => $error
  45.         ]);
  46.     }
  47.     /**
  48.      * @Route("/logout", name="app_logout")
  49.      */
  50.     public function logout()
  51.     {
  52.         throw new \LogicException('This method can be blank - it will be intercepted by the logout key on your firewall.');
  53.     }
  54.     /**
  55.      * @Route("/signin-end/{token}/", name="signin_end")
  56.      */
  57.     public function signinEndAction(Request $request$tokenUserPasswordEncoderInterface $passwordEncoder)
  58.     {
  59.         //Load parameter
  60.         $token $request->get('token');
  61.         $user $this->em->getRepository('App:User')
  62.                          ->findOneBy(Array("tokenReset" => $token));
  63.         $userCurrent $this->get('security.token_storage')->getToken()->getUser();
  64.         if($userCurrent != "anon."){
  65.             $this->addFlash('danger','You are already logged into another account, you cannot complete a registration.');
  66.             return $this->redirectToRoute('app_login');
  67.         }
  68.         if(!$user){
  69.             $this->addFlash('danger',"This URL is not functional");
  70.             return $this->redirectToRoute('app_login');
  71.         }
  72.         $form $this->createForm(SigninEndType::class, $user);
  73.         $form->handleRequest($request);
  74.         if($form->isSubmitted() && $form->isValid()) {
  75.             $password $passwordEncoder->encodePassword($user$user->getPlainPassword());
  76.             $user->setPassword($password);
  77.             $user->setTokenReset(null);
  78.             $user->setStatus('ACTIVE');
  79.             $this->em->persist($user);
  80.             $this->em->flush();
  81.             //Send mail activation
  82.             //$this->get('fos_user.mailer')->sendConfirmationEmailMessage($user);
  83.             $this->addFlash('success','Your password has been completed successfully. You can now connect');
  84.             return $this->redirectToRoute('app_login');
  85.         }
  86.         return $this->render('registration/signinEnd.html.twig', Array(
  87.             'form' => $form->createView(),
  88.             'token' => $token
  89.         )); 
  90.     }
  91. }