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