src/Controller/SecurityController.php line 21

Open in your IDE?
  1. <?php
  2. namespace App\Controller;
  3. use App\Entity\User;
  4. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  5. use Symfony\Component\HttpFoundation\Request;
  6. use Symfony\Component\HttpFoundation\Response;
  7. use Symfony\Component\Mailer\MailerInterface;
  8. use Symfony\Component\Mime\Address;
  9. use Symfony\Component\Mime\Email;
  10. use Symfony\Component\Routing\Annotation\Route;
  11. use Symfony\Component\Security\Core\Encoder\UserPasswordEncoderInterface;
  12. use Symfony\Component\Security\Http\Authentication\AuthenticationUtils;
  13. class SecurityController extends BaseController
  14. {
  15.     /**
  16.      * @Route("/login", name="app_login")
  17.      */
  18.     public function login(AuthenticationUtils $authenticationUtils): Response
  19.     {
  20.          if ($this->getUser()) {
  21.              return $this->redirectToRoute('panel_homepage');
  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.         return $this->render('security/login.html.twig', ['last_username' => $lastUsername'error' => $error]);
  28.     }
  29.     /**
  30.      * @Route("/logout", name="app_logout")
  31.      */
  32.     public function logout()
  33.     {
  34.         throw new \LogicException('This method can be blank - it will be intercepted by the logout key on your firewall.');
  35.     }
  36.     /**
  37.      * @Route("/forgot-password", name="app_forgot_password")
  38.      */
  39.     public function forgot_password(Request $requestMailerInterface $mailer)
  40.     {
  41.         $v_email $request->request->get('email');
  42.         if ($v_email){
  43.             $em $this->getDoctrine()->getManager();
  44.             $user $em->getRepository(User::class)->findOneBy(array('email'=>$v_email));
  45.             if (!$user){
  46.                 return $this->render('security/forgot_password.html.twig',array('error'=>'Lütfen kayıtlı e-posta giriniz.'));
  47.             }
  48.             $token sha1(md5($v_email));
  49.             $user->setPasswordToken($token);
  50.             $em->persist($user);
  51.             $em->flush();
  52.             $email = (new Email())
  53.                 ->from(new Address($this->ayarlar('sistemPosta'),$this->ayarlar('siteAdi')))
  54.                 ->to($v_email)
  55.                 //->cc('cc@example.com')
  56.                 //->bcc('bcc@example.com')
  57.                 //->replyTo('fabien@example.com')
  58.                 ->priority(Email::PRIORITY_HIGH)
  59.                 ->subject('Şifremi unuttum - '.$this->ayarlar('siteAdi'))
  60.                 //->text('Sending emails is fun again!')
  61.                 ->html($this->renderView('mail/sifremi_unuttum.html.twig',array('konu'=>'Şifremi unuttum','token'=>$token,'user'=>$user)));
  62.             $mailer->send($email);
  63.             return $this->render('security/forgot_password.html.twig',array('success'=>'Şifreni yenileme bağlantısı e-posta adresine gönderildi.'));
  64.         }
  65.         return $this->render('security/forgot_password.html.twig');
  66.     }
  67.     /**
  68.      * @Route("/reset-password/{token}", name="app_reset_password")
  69.      */
  70.     public function reset_password(Request $request$tokenUserPasswordEncoderInterface $passwordEncoder)
  71.     {
  72.         $em $this->getDoctrine()->getManager();
  73.         $user $em->getRepository(User::class)->findOneBy(array('passwordToken'=>$token));
  74.         if ($user) {
  75.             $password $request->request->get('password');
  76.             if ($password){
  77.                     $user->setPassword($passwordEncoder->encodePassword($user$password));
  78.                     $user->setPasswordToken(null);
  79.                     $em->persist($user);
  80.                     $em->flush();
  81.                 return $this->render('security/reset_password.html.twig',array('success'=>'Şifren yenilendi. Artık giriş yapabilirsin.'));
  82.             }
  83.         } else {
  84.           return $this->redirectToRoute('app_login');
  85.         }
  86.         return $this->render('security/reset_password.html.twig');
  87.     }
  88.     /**
  89.      * @Route("/replace-password", name="app_replace_password")
  90.      */
  91.     public function replace_password(Request $requestUserPasswordEncoderInterface $passwordEncoder)
  92.     {
  93.         $em $this->getDoctrine()->getManager();
  94.         $user $this->getUser();
  95.         if ($user->getIsPassword()==true){
  96.             return $this->redirectToRoute('panel_homepage');
  97.         }
  98.         $geciciSifre $request->request->get('geciciSifre');
  99.         $password $request->request->get('password');
  100.         if ($password){
  101.             if (!$passwordEncoder->isPasswordValid($user,$geciciSifre)){
  102.                 return $this->render('security/replace_password.html.twig',array('error'=>'Geçici şifreni yanlış girdin. Tekrar dene.'));
  103.             }
  104.             $user->setPassword($passwordEncoder->encodePassword($user$password));
  105.             $user->setIsPassword(1);
  106.             $em->persist($user);
  107.             $em->flush();
  108.             return $this->render('security/replace_password.html.twig',array('success'=>'Şifren değiştirildi. Yönlendiriliyorsun..'));
  109.         }
  110.         return $this->render('security/replace_password.html.twig');
  111.     }
  112. }