src/Controller/SecurityController.php line 46

Open in your IDE?
  1. <?php
  2. namespace App\Controller;
  3. use App\Entity\User;
  4. use App\Helpers\AppHelper;
  5. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  6. use Symfony\Component\HttpFoundation\Request;
  7. use Symfony\Component\Routing\Annotation\Route;
  8. use Symfony\Component\Security\Http\Authentication\AuthenticationUtils;
  9. use Symfony\Component\Security\Core\Encoder\UserPasswordEncoderInterface;
  10. class SecurityController extends AbstractController
  11. {
  12.     public static function getSubscribedServices(){
  13.         return array_merge(parent::getSubscribedServices(), [
  14.             'Basecamp' => \App\Helper\Basecamp::class,
  15.             'settings' => \App\Helper\Settings::class
  16.         ]);
  17.     }
  18.     /**
  19.      * @Route("/login", name="login")
  20.      */
  21.     public function login(Request $requestAuthenticationUtils $authenticationUtils)
  22.     {
  23.         // get the login error if there is one
  24.         $error $authenticationUtils->getLastAuthenticationError();
  25.         // last username entered by the user
  26.         $lastUsername $authenticationUtils->getLastUsername();
  27. //        var_dump($request);exit;
  28.         return $this->render('security/login.html.twig', array(
  29.             'last_username' => $lastUsername,
  30.             'error'         => $error,
  31.         ));
  32.     }
  33.     /**
  34.      * @Route("/remind_password", name="remind_password")
  35.      */
  36.     public function remind_password(Request $request\Swift_Mailer $mailer)
  37.     {
  38.         $error false;
  39.         $msg "Jeżeli istnieje użytkownik z podanym adresem email, na podany adres zostanie wysłany link do zmiany hasła.";
  40.         $sended false;
  41.         if ($request->isMethod('POST') ) {
  42.             $email $request->request->get('email');
  43.             if( $email != '' ){
  44.                 $user $this->getDoctrine()->getRepository(User::class)->findOneBy(['email' => $email]);
  45.                 if( $user ){
  46.                     $this->sendRemindPasswordEmail($user$mailer);
  47.                     $sended true;
  48.                 }else{
  49. //                    $error = 'Użytkownik o podanym adresie nie istnieje.';
  50.                     $sended true;
  51.                 }
  52.             }else{
  53.                 $error 'Podaj poprawny adres email';
  54.             }
  55.         }
  56.         return $this->render('security/remind.html.twig', array(
  57.             'error' => $error,
  58.             'sended' => $sended,
  59.             'msg' => $msg
  60.         ));
  61.     }
  62.     /**
  63.      * @Route("/reset_password", name="reset_password")
  64.      */
  65.     public function reset_password(Request $requestUserPasswordEncoderInterface $encoder)
  66.     {
  67.         $hash $request->query->get('hash'false);
  68.         if( !$hash ){
  69.             return $this->redirectToRoute('home');
  70.         }
  71.         $user $this->getDoctrine()->getRepository(User::class)->findOneBy(['hash' => $hash]);
  72.         $changed false;
  73.         $error false;
  74.         $username '';
  75.         if ($request->isMethod('POST') ) {
  76.             $password $request->request->get('_password');
  77.             $passwordConfirm $request->request->get('_password_confirm');
  78.             if( $password == $passwordConfirm ){
  79.                 try {
  80.                     $password $encoder->encodePassword($user$password);
  81.                     $user->setPassword($password);
  82.                     $user->setHash(NULL);
  83.                     $this->getDoctrine()->getManager()->persist($user);
  84.                     $this->getDoctrine()->getManager()->flush();
  85.                     $changed true;
  86.                 }catch(\Exception $e){
  87.                     $error $e->getMessage();
  88.                 }
  89.             }else{
  90.                 $error "Podane hasła muszą być identyczne";
  91.             }
  92.         }
  93.         return $this->render('security/reset.html.twig', array(
  94.             'user' => $user,
  95.             'changed' => $changed,
  96.             'error' => $error,
  97.             'username' => $username
  98.         ));
  99.     }
  100.     public function sendRemindPasswordEmail($user$mailer){
  101.         $hash md5(time() . '-/-' $user->getEmail() . '-/-' $user->__toString() );
  102.         $user->setHash($hash);
  103.         $this->getDoctrine()->getManager()->persist($user);
  104.         $this->getDoctrine()->getManager()->flush();
  105.         $emailSubject '['.$this->container->get('settings')->get('appTitle').'] ' "Reset hasła";
  106.         $emailBody $this->renderView(
  107.             '_emails/remind_password.html.twig',
  108.             array( 'user' => $user'hash' => $hash )
  109.         );
  110.         $emailMessage = (new \Swift_Message($emailSubject))
  111. //                        ->setFrom( [$this->container->getParameter('mailer_user') => $this->container->getParameter('mailer_sender')] )
  112.             ->setFrom($this->container->get('settings')->get('mailSender'), $this->container->get('settings')->get('appTitle'))
  113.             ->setTo$user->getEmail() )
  114.             ->setBody($emailBody'text/html');
  115.         $mailer->send($emailMessage);
  116.     }
  117.     /**
  118.      * @Route("/login_check", name="login_check")
  119.      */
  120.     public function loginAction()
  121.     {
  122.         // The security layer will intercept this request, else redirect to login page
  123.         $this->addFlash('warning'$this->get('translator')->trans('login_expired'));
  124.         return $this->redirect($this->generateUrl('home'));
  125. //        die("LOL");
  126.     }
  127.     /**
  128.      * @Route("/logout", name="logout")
  129.      */
  130.     public function logoutAction()
  131.     {
  132.         // The security layer will intercept this request, else redirect to login page
  133.         $this->addFlash('warning'$this->get('translator')->trans('login_expired'));
  134.         return $this->redirect($this->generateUrl('login'));
  135.     }
  136.     /**
  137.      * @Route("/access_denied", name="forbidden")
  138.      */
  139.     public function forbidden($parent false)
  140.     {
  141.         if( $parent )
  142.             return $this->render('security/forbidden_parent.html.twig', ['parent' => $parent]);
  143.         else
  144.             return $this->render('security/forbidden.html.twig', array());
  145.     }
  146.     /**
  147.      * @Route("/not_found", name="notfound")
  148.      */
  149.     public function notfound()
  150.     {
  151.         return $this->render('security/notfound.html.twig', array());
  152.     }
  153. }