src/Controller/ResetLozinkeController.php line 84

Open in your IDE?
  1. <?php
  2. namespace App\Controller;
  3. use App\Entity\User;
  4. use App\Form\EmailZaResetLozinkeForm;
  5. use App\Form\PromjenaLozinkeForm;
  6. use App\Form\ProvjeraTokenaForm;
  7. use App\Handler\MessageTrait;
  8. use Doctrine\ORM\EntityManagerInterface;
  9. use Swift_Mailer;
  10. use Swift_Message;
  11. use Symfony\Component\HttpFoundation\Request;
  12. use Symfony\Component\HttpFoundation\Response;
  13. use Symfony\Component\Routing\Annotation\Route;
  14. use Symfony\Component\Security\Core\Encoder\UserPasswordEncoderInterface;
  15. class ResetLozinkeController extends AppController
  16. {
  17.     protected $mailer;
  18.     protected $manager;
  19.     /**
  20.      * @param EntityManagerInterface $manager
  21.      * @param Swift_Mailer $mailer
  22.      */
  23.     public function __construct(EntityManagerInterface $managerSwift_Mailer $mailer)
  24.     {
  25.         $this->mailer $mailer;
  26.         $this->manager $manager;
  27.     }
  28.     private function sendMail($email $token)
  29.     {
  30.         $appName $_SERVER['SERVER_NAME'];
  31.         $poveznica $appName '/reset-lozinke/promjena-lozinke/' $token;
  32.         $entityManager $this->manager;
  33.         $id $entityManager->getRepository('App:User')->findIdViaEmail($email);
  34.         $user $entityManager->getRepository(User::class)->find($id);
  35.         $message = new Swift_Message('Obnova lozinke ');
  36.         $headers =$message->getHeaders();
  37.         $headers->addIdHeader('Message-ID'"b3eb7202-d2f1-11e4-b9d6-1681e6b88ec1@domain.com");
  38.         $headers->addTextHeader('MIME-Version''1.0');
  39.         $headers->addTextHeader('X-Mailer''PHP v' phpversion());
  40.         $headers->addParameterizedHeader('Content-type''text/html', ['charset' => 'utf-8']);
  41.         $message->setFrom(['obavijesti@' $appName => 'e-udruge']);
  42.         $message->setTo($email);
  43.         $message->setPriority(1);
  44.         $message->setBody(
  45.             $this->renderView(
  46.                 'userSecurity/resetLozinke/emailBody.html.twig',
  47.                 [
  48.                     'poveznica' => $poveznica,
  49.                     'token' => $token,
  50.                     'user' => $user
  51.                 ]
  52.             ),
  53.             'text/html'"utf-8");
  54.         return $this->mailer->send($message);
  55.     }
  56.     private function generateToken()
  57.     {
  58.         $string '0123456789qwertzuiopasdfghjklyxcvbnmQWERTZUIOPASDFGHJKLYXCVBNM';
  59.         $string str_shuffle($string);
  60.         $string =substr($string,0,50);
  61.         $token uniqid() . '_' $string;
  62.         return $token;
  63.     }
  64.     /**
  65.      * @Route("/reset-lozinke" , name="reset_lozinka")
  66.      * @param Request $request
  67.      * @return Response
  68.      */
  69.     public function sendToken(Request $request)
  70.     {
  71.         $entityManager $this->manager;
  72.         $form $this->createForm(EmailZaResetLozinkeForm::class);
  73.         $form->handleRequest($request);
  74.         $email $form["email"]->getData();
  75.         $users $entityManager->getRepository('App:User')->findBy(['email' =>$email]);
  76.         if ($form->isSubmitted() && $form->isValid()) {
  77.             if (!empty($users) ) {
  78.                 $token $this->generateToken();
  79.                 $id $entityManager->getRepository('App:User')->findIdViaEmail($email);
  80.                 $user $entityManager->getRepository(User::class)->find($id);
  81.                 $this->sendCustomMessage(MessageTrait::$success'Poslan Vam je e-mail sa daljnjim uputama');
  82.                 $user->setToken($token);
  83.                 $entityManager->flush();
  84.                 $this->sendMail($email $token);
  85.                 return $this->redirectToRoute('reset_lozinka');
  86.             } else {
  87.                 $this->sendCustomMessage(MessageTrait::$error'Ovaj korisnički račun ne postoji!');
  88.                 return $this->render(
  89.                     'userSecurity/resetLozinke/resetLozinke.html.twig' ,
  90.                     [
  91.                         'form' => $form->createView()
  92.                     ]
  93.                 );
  94.             }
  95.         }
  96.         return $this->render(
  97.             'userSecurity/resetLozinke/resetLozinke.html.twig' ,
  98.             [
  99.                 'form' => $form->createView()
  100.             ]
  101.         );
  102.     }
  103.     /**
  104.      * @param Request $request
  105.      * @param UserPasswordEncoderInterface $encoder
  106.      * @return Response
  107.      * @Route("/reset-lozinke/promjena-lozinke/{token}" , name="promijeni_lozinku")
  108.      */
  109.     public function promjenaLozinke(Request $request ,UserPasswordEncoderInterface $encoder)
  110.     {
  111.         $user null;
  112.         $entityManager $this->manager;
  113.         $routeParams $request->attributes->get('_route_params');
  114.         $email $entityManager->getRepository('App:User')->findEmailViaToken($routeParams['token']);
  115.         $id $entityManager->getRepository('App:User')->findIdViaEmail($email['email']);
  116.         if ($id) {
  117.             $user $entityManager->getRepository(User::class)->find($id);
  118.         } else {
  119.             return $this->redirectToRoute('security_login');
  120.         }
  121.         $form $this->createForm(PromjenaLozinkeForm::class);
  122.         $form->handleRequest($request);
  123.         $pass $form["plainPassword"]->getData();
  124.         if ($form->isSubmitted() && $form->isValid()) {
  125.             $password $encoder
  126.                 ->encodePassword($user$pass);
  127.             $user->setPassword($password);
  128.             $user->setToken(null);
  129.             $entityManager->flush();
  130.             $this->sendCustomMessage(MessageTrait::$success'Uspješno promijenjena lozinka');
  131.             return $this->redirectToRoute('security_login');
  132.         }
  133.         return $this->render(
  134.             'userSecurity/resetLozinke/promjenaLozinke.html.twig' ,
  135.             [
  136.                 'email' =>$email['email'],
  137.                 'form' => $form->createView()
  138.             ]
  139.         );
  140.     }
  141. }