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\Swift_Mailer $mailerRoleService $roleService) {
  17.         $this->em $entityManager;
  18.         $this->mailer $mailer;
  19.         $this->roleService $roleService;
  20.     }
  21.     /**
  22.      * @Route("/login", name="app_login")
  23.      */
  24.     public function login(AuthenticationUtils $authenticationUtils): Response
  25.     {
  26.         if ($this->getUser()) {
  27.             $userCurrent $this->get('security.token_storage')->getToken()->getUser();
  28.             //update last login
  29.             $userCurrent->setLastLogin(new \DateTime());
  30.             $this->em->persist($userCurrent);
  31.             $this->em->flush();
  32.             if($this->roleService->isGranted('ROLE_ADMIN'$userCurrent)){
  33.                 return $this->redirectToRoute('seller_list');
  34.             }
  35.             else if($this->roleService->isGranted('ROLE_ADMIN_MANAGER'$userCurrent)){
  36.                 return $this->redirectToRoute('manager_list');
  37.             }
  38.             else if($this->roleService->isGranted('ROLE_SELLER'$userCurrent)){
  39.                 return $this->redirectToRoute('customer_list');
  40.             }
  41.             else if(
  42.                 $this->roleService->isGranted('ROLE_MANAGER'$userCurrent) ||
  43.                 $this->roleService->isGranted('ROLE_SIGNATORY'$userCurrent)
  44.             ) {
  45.                 return $this->redirectToRoute('document_list');
  46.             }
  47.             else if($this->roleService->isGranted('ROLE_CUSTOMER'$userCurrent)){
  48.                 
  49.                 if($userCurrent->isIsAutoSignup()){
  50.                     return $this->redirectToRoute('member_signup', array("token" => $userCurrent->getTokenSignupContinue()));
  51.                 }
  52.                 else{
  53.                     return $this->redirectToRoute('membership_list', array("userID" => "me"));
  54.                 }
  55.                 
  56.             }
  57.             
  58.         }
  59.         // get the login error if there is one
  60.         $error $authenticationUtils->getLastAuthenticationError();
  61.         // last username entered by the user
  62.         $lastUsername $authenticationUtils->getLastUsername();
  63.         $userAgent $_SERVER['HTTP_USER_AGENT'];
  64.         if(strstr($userAgent"MSIE") || strstr($userAgent"Trident")) { 
  65.             $userAgent "IE";
  66.         } 
  67.         return $this->render('security/login.html.twig', [
  68.             'last_username' => $lastUsername
  69.             'error' => $error,
  70.             'userAgent' => $userAgent
  71.         ]);
  72.     }
  73.     /**
  74.      * @Route("/logout", name="app_logout")
  75.      */
  76.     public function logout()
  77.     {
  78.         throw new \LogicException('This method can be blank - it will be intercepted by the logout key on your firewall.');
  79.     }
  80.     /**
  81.      * @Route("/signin-end/{token}/", name="signin_end")
  82.      */
  83.     public function signinEndAction(Request $request$tokenUserPasswordEncoderInterface $passwordEncoder)
  84.     {
  85.         //Load parameter
  86.         $token $request->get('token');
  87.         $user $this->em->getRepository(User::class)
  88.                          ->findOneBy(Array("tokenReset" => $token));
  89.         $userCurrent $this->get('security.token_storage')->getToken();
  90.         if($userCurrent){
  91.             $this->addFlash('danger','Vous êtes déjà connecté sur un autre compte, vous ne pouvez pas terminer une inscription.');
  92.             return $this->redirectToRoute('app_login');
  93.         }
  94.         if(!$user){
  95.             $this->addFlash('danger',"Cette URL n'est pas fonctionnelle");
  96.             return $this->redirectToRoute('app_login');
  97.         }
  98.         $form $this->createForm(SigninEndType::class, $user);
  99.         $form->handleRequest($request);
  100.         if($form->isSubmitted() && $form->isValid()) {
  101.             $password $passwordEncoder->encodePassword($user$user->getPlainPassword());
  102.             $user->setPassword($password);
  103.             $user->setTokenReset(null);
  104.             $user->setStatus('ACTIVE');
  105.             $this->em->persist($user);
  106.             $this->em->flush();
  107.             //Send mail activation
  108.             //$this->get('fos_user.mailer')->sendConfirmationEmailMessage($user);
  109.             $this->addFlash('success','Votre mot de passe a bien été complété. Vous pouvez maintenant vous connecter');
  110.             return $this->redirectToRoute('app_login');
  111.         }
  112.         return $this->render('registration/signinEnd.html.twig', Array(
  113.             'form' => $form->createView(),
  114.             'token' => $token
  115.         )); 
  116.     }
  117. }