src/AppBundle/Controller/AccountController.php line 28

Open in your IDE?
  1. <?php
  2. namespace AppBundle\Controller;
  3. use App\Service\Message;
  4. use AppBundle\Entity\Admin;
  5. use AppBundle\Entity\Employer;
  6. use AppBundle\Entity\Participant;
  7. use AppBundle\Entity\Reseller;
  8. use AppBundle\Entity\Supervisor;
  9. use AppBundle\Entity\User;
  10. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  11. use Symfony\Component\Routing\Annotation\Route;
  12. use Symfony\Component\HttpFoundation\Request;
  13. use Symfony\Component\Security\Core\Security;
  14. class AccountController extends AbstractController
  15. {
  16.     private $user;
  17.     public function __construct(Security $security)
  18.     {            
  19.         $this->user $security->getUser();
  20.     }
  21.     /**
  22.      * @Route("/", methods={"GET"}, name="login_home")
  23.      * @Route("/account/login", methods={"GET"}, name="login")
  24.      */
  25.     public function loginAction()
  26.     {
  27.         
  28.         $user $this->getUser();
  29.         
  30.         if ($user) {
  31.             $this->redirectToUser($user);
  32.         }
  33.         
  34.         return $this->render('account/login.html.twig');
  35.     }
  36.     /**
  37.      * @Route("/account/forgot", methods={"GET"}, name="forgot_password")
  38.      */
  39.     public function forgotAction()
  40.     {
  41.         return $this->render('account/password_forgot.html.twig');
  42.     }
  43.     /**
  44.      * @Route("/account/forgot", methods={"POST"}, name="forgot_password_post")
  45.      */
  46.     public function forgotPostAction(Request $request)
  47.     {
  48.         $login $request->request->get('login');
  49.         if (!$login) {
  50.             $this->addFlash('account''Please provide a user name or email address');
  51.             return $this->redirectToRoute('forgot_password');
  52.         }
  53.         $service $this->getUserService();
  54.         
  55.         if (!$service->sendResetEmail($login)) {
  56.             $this->addFlash('account''Account not found');
  57.             return $this->redirectToRoute('forgot_password');
  58.         }
  59.         $this->getDoctrine()->getManager()->flush();
  60.         return $this->redirectToRoute('password_request_sent');
  61.     }
  62.     /**
  63.      * @Route("/account/request_sent", methods={"GET"}, name="password_request_sent")
  64.      */
  65.     public function requestSentAction()
  66.     {
  67.         return $this->render('account/password_request_sent.html.twig');
  68.     }
  69.     /**
  70.      * @param Request $request
  71.      * @Route("/account/reset", methods={"GET"}, name="reset_password")
  72.      */
  73.     public function resetAction(Request $request)
  74.     {
  75.         try {
  76.             $user $this->getResetUser($request->query->get('login'), $request->query->get('code'));
  77.         } catch (\Throwable $e) {
  78.             $this->addFlash('account'$e->getMessage());
  79.             return $this->redirectToRoute('login_home');
  80.         }
  81.         return $this->render('account/password_reset.html.twig', array(
  82.             'user' => $user
  83.         ));
  84.     }
  85.     /**
  86.      * @Route("/account/reset", methods={"POST"}, name="reset_password_post")
  87.      */
  88.     public function resetPostAction(Request $request)
  89.     {
  90.         try {
  91.             $user $this->getResetUser($request->request->get('original_login'), $request->request->get('code'));
  92.         } catch (\Throwable $e) {
  93.             $this->addFlash('account'$e->getMessage());
  94.             return $this->redirectToRoute('login_home');
  95.         }
  96.         $this->getUserService()->changePassword($user$request->request->get('password'));
  97.         $this->getDoctrine()->getManager()->flush();
  98.         $this->addFlash('account''Your password has been reset. Please log in with your new credentials');
  99.         return $this->redirectToRoute('login_home', array('login'=>$user->getUsername()));
  100.     }
  101.     /**
  102.      * @param string $login
  103.      * @param string $code
  104.      * @return User
  105.      * @throws \RuntimeException
  106.      */
  107.     protected function getResetUser($login$code)
  108.     {
  109.         $user $this->getUserProviderService()->loadUserByUsername($login);
  110.         if (!$user) {
  111.             throw new \RuntimeException('No user account found');
  112.         }
  113.         if ($user->getPassword() !== $code) {
  114.             throw new \RuntimeException('Your password reset link has expired');
  115.         }
  116.         return $user;
  117.     }
  118.     /**
  119.      * If a user is logged in, redirect to their portal
  120.      * @param User $user
  121.      */
  122.     protected function redirectToUser(User $user)
  123.     {
  124.         if ($user instanceof Admin) {
  125.             header('Location: /admin/index.php?LoggedIn=true');
  126.         }
  127.         elseif ($user instanceof Employer) {
  128.             header('Location: /employer/welcome.php?LoggedIn=true');
  129.         }
  130.         elseif ($user instanceof Participant) {
  131.             header('Location: /participant/index.php?LoggedIn=true');
  132.         }
  133.         elseif ($user instanceof Reseller) {
  134.             header('Location: /reseller/index.php?LoggedIn=true');
  135.         }
  136.         elseif ($user instanceof Supervisor) {
  137.             header('Location: /supervisor/index.php');
  138.         }
  139.         
  140.         exit();
  141.     }
  142.     /**
  143.      * @return \AppBundle\Users\UserService
  144.      */
  145.     protected function getUserService()
  146.     {
  147.         return $this->get2('users');
  148.     }
  149.     /**
  150.      * @return \AppBundle\Security\UserProvider
  151.      */
  152.     protected function getUserProviderService()
  153.     {
  154.         return $this->get2('user_provider');
  155.     }
  156. }