src/SymfonyDev/AppBundle/Controller/SecurityController.php line 16

Open in your IDE?
  1. <?php
  2. namespace SymfonyDev\AppBundle\Controller;
  3. use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
  4. use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template;
  5. use Symfony\Component\HttpFoundation\Request;
  6. use SymfonyDev\AppBundle\Entity\User;
  7. class SecurityController extends BaseController
  8. {
  9.     /**
  10.      * @Route("/login", name="app_security_login")
  11.      * @Template
  12.      */
  13.     public function loginAction(Request $request)
  14.     {
  15.         if ($this->get('security.authorization_checker')->isGranted('IS_AUTHENTICATED_FULLY')) {
  16.             return $this->redirect($this->generateUrl('app_security_redirect'));
  17.         }
  18.         
  19.         $authUtils $this->get('security.authentication_utils');
  20.         return array(
  21.             'last_username' => $authUtils->getLastUsername(),
  22.             'error'         => $authUtils->getLastAuthenticationError(),
  23.         );
  24.     }
  25.     
  26.     /**
  27.      * @Route("/logout", name="app_security_logout")
  28.      * @Template
  29.      */
  30.     public function logoutAction(Request $request)
  31.     {
  32.         $this->get('security.token_storage')->setToken(null);
  33.         $request->getSession()->invalidate();
  34.         
  35.         return $this->redirect($this->generateUrl('app_security_login'));
  36.     }
  37.     
  38.     /**
  39.      * @Route("/redirect", name="app_security_redirect")
  40.      */
  41.     public function redirectAfterLoginAction()
  42.     {
  43.         $user $this->getUser();
  44.         
  45.         $route 'app_security_login';
  46.         if ($user instanceof \Symfony\Component\Security\Core\User\UserInterface) {
  47.             if ($user->hasRole(\SymfonyDev\AppBundle\Entity\Role::ROLE_USER)) {
  48.                 $route 'app_dashboard_index';
  49.             } else {
  50.                 $route 'app_dashboard_index';
  51.             }
  52.         }
  53.         return $this->redirect($this->generateUrl($route));
  54.     }
  55.     
  56.     /**
  57.      * @Route("/register/{type}", name="app_security_register")
  58.      * @Template
  59.      */
  60.     public function registerAction(Request $request$type)
  61.     {
  62.         if ($this->get('security.authorization_checker')->isGranted('IS_AUTHENTICATED_FULLY')) {
  63.             return $this->redirect($this->generateUrl('app_security_redirect'));
  64.         }
  65.         $type ucwords($type);
  66.         if (!in_array(ucwords($type), array(User::USER_USER_TYPE_PRACTITIONERUser::USER_USER_TYPE_STUDENTUser::USER_USER_TYPE_USER))) {
  67.             throw new \Symfony\Component\HttpKernel\Exception\NotFoundHttpException();
  68.         }
  69.         
  70.         $user = new User();
  71.         $user->addAddress(new \SymfonyDev\AppBundle\Entity\Address());
  72.         
  73.         $form $this->createForm(\SymfonyDev\AppBundle\Form\RegisterType::class, $user, array('user_type' => $type));
  74.         
  75.         $form->handleRequest($request);
  76.         if ($form->isSubmitted() && $form->isValid()) {
  77.             $data $form->getData();
  78.             $data->setUserType($type);
  79.             $data->setIsEnable(false);
  80.             $data->setIsPwdSecure(true);
  81.             $this->get('user_manager')
  82.                 ->create($data);
  83.             
  84.             $this->get('session')->getFlashBag()->add(
  85.                 'ALERT_SUCCESS',
  86.                 'You are registered successfully.'
  87.             );
  88.             return $this->redirectToRoute('app_dashboard_index');
  89.         }
  90.         
  91.         return array(
  92.             'form' => $form->createView()
  93.         );
  94.     }
  95.     
  96.     /**
  97.      * @Route("/forgot-password", name="app_security_forgot_password")
  98.      * @Template
  99.      */
  100.     public function forgotPasswordAction(Request $request)
  101.     {
  102.         if ($this->get('security.authorization_checker')->isGranted('IS_AUTHENTICATED_FULLY')) {
  103.             return $this->redirect($this->generateUrl('app_security_redirect'));
  104.         }
  105.         $dbm $this->getDBM();
  106.         $obj = new \SymfonyDev\AppBundle\Entity\ForgotPassword();
  107.         $form $this->createForm(\SymfonyDev\AppBundle\Form\ForgotPasswordType::class, $obj);
  108.         $form->handleRequest($request);
  109.         if ($form->isSubmitted() && $form->isValid()) {
  110.             $obj $form->getData();
  111.             
  112.             $user $dbm->getRepository('SymfonyDevAppBundle:User')
  113.                 ->findOneBy(array('email' => $obj->getTmpEmail()));
  114.             
  115.             if (!$user) {
  116.                 $this->get('session')->getFlashBag()->add(
  117.                     'ALERT_ERROR',
  118.                     'User is not registed with this email. Please check your email ID.'
  119.                 );
  120.                 
  121.                 return $this->redirectToRoute('app_security_forgot_password');
  122.             } else {
  123.                 $token uniqid(rand(100999));
  124.                 $resetPasswordLink $this->generateUrl('app_security_reset_password', array('id' => $user->getId(), 'key' => $token), \Symfony\Component\Routing\Generator\UrlGeneratorInterface::ABSOLUTE_URL);
  125.                 
  126.                 $this->sendEmailToForgotPasswordUser($user$resetPasswordLink);
  127.                 
  128.                 $this->get('session')->getFlashBag()->add(
  129.                     'ALERT_SUCCESS',
  130.                     'Please check your registered email with us, click on the link in email to reset your password.'
  131.                 );
  132.                 
  133.                 $user->setResetPasswordToken($token);
  134.                 $dbm->persist($user);
  135.                 $dbm->flush();
  136.                 
  137.                 return $this->redirectToRoute('app_security_login');
  138.             }
  139.         }
  140.         return array(
  141.             'form' => $form->createView()
  142.         );
  143.     }
  144.     
  145.     /**
  146.      * Send forget password email to user
  147.      */
  148.     private function sendEmailToForgotPasswordUser($obj$resetPasswordLink)
  149.     {
  150.         $fromEmail $this->get('site_parameter_manager')->getParam('from_email''info@safflower.com.au');
  151.         
  152.         $message = \Swift_Message::newInstance()
  153.             ->setSubject('Safflower: Reset Password')
  154.             ->setFrom(array($fromEmail => 'Safflower Staff'))
  155.             ->setTo($obj->getEmail())
  156.             ->setContentType('text/html')
  157.             ->setBody(
  158.                 $this->renderView(
  159.                     '@SymfonyDevApp/email_template/forgot_password.html.twig',
  160.                     array('user' => $obj'resetLink' => $resetPasswordLink)
  161.                 ),
  162.                 'text/html'
  163.             );
  164.         
  165.         return $this->get('mailer')->send($message);
  166.     }
  167.     
  168.     
  169.     /**
  170.      * @Route("/reset-password/{id}/{key}", name="app_security_reset_password")
  171.      * @Template
  172.      */
  173.     public function resetPasswordAction(Request $request$id$key)
  174.     {
  175.         if ($this->get('security.authorization_checker')->isGranted('IS_AUTHENTICATED_FULLY')) {
  176.             return $this->redirect($this->generateUrl('app_security_redirect'));
  177.         }
  178.         $user $this->getRepository('SymfonyDevAppBundle:User')->find($id);
  179.         
  180.         if (!$user) {
  181.             $this->get('session')->getFlashBag()->add('ALERT_ERROR''Invalid request!');
  182.             return $this->redirectToRoute('app_security_login');
  183.         }
  184.         if ($key != $user->getResetPasswordToken()) {
  185.             $this->get('session')->getFlashBag()->add('ALERT_ERROR''Invalid request!');
  186.             return $this->redirectToRoute('app_security_login');
  187.         }
  188.         
  189.         $dbm $this->getDBM();
  190.         $form $this->createForm(\SymfonyDev\AppBundle\Form\UserChangePasswordType::class, $user);
  191.         $form->handleRequest($request);
  192.         
  193.         if ($form->isSubmitted() && $form->isValid()) {
  194.             $data $form->getData();
  195.             $data->setResetPasswordToken(null);
  196.             $data->setIsPwdSecure(true);
  197.             $dbm->persist($data);
  198.             $dbm->flush();
  199.             
  200.             $this->get('session')->getFlashBag()->add(
  201.                 'ALERT_SUCCESS',
  202.                 'Your password is reset successfully.'
  203.             );
  204.             
  205.             return $this->redirectToRoute('app_security_login');
  206.         }
  207.         
  208.         return array(
  209.             'form' => $form->createView()
  210.         );
  211.     }
  212.     
  213.     /**
  214.      * @Route("/online-consultation", name="app_security_online_consultation")
  215.      * @Template
  216.      */
  217.     public function onlineConsultationAction(Request $request)
  218.     {
  219.         $isSuccess false;
  220.         $dbm $this->getDBM();
  221.         
  222.         $obj = new \SymfonyDev\AppBundle\Entity\Consultation();
  223.         $form $this->createForm(\SymfonyDev\AppBundle\Form\ConsultationType::class, $obj);
  224.         $form->handleRequest($request);
  225.         
  226.         if ($form->isSubmitted() && $form->isValid()) {
  227.             $data $form->getData();
  228.             $dbm->persist($data);
  229.             $dbm->flush();
  230.             
  231.             $this->get('session')->getFlashBag()->add(
  232.                 'ALERT_SUCCESS',
  233.                 'Your details submitted successfully.'
  234.             );
  235.             
  236.             $isSuccess true;
  237.         }
  238.         
  239.         return array(
  240.             'form' => $form->createView(),
  241.             'qKeys' => \SymfonyDev\AppBundle\Entity\Consultation::Q_KEY,
  242.             'isSuccess' => $isSuccess
  243.         );
  244.     }
  245. }