<?php
namespace App\Controller;
use App\Entity\User;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Mailer\MailerInterface;
use Symfony\Component\Mime\Address;
use Symfony\Component\Mime\Email;
use Symfony\Component\Routing\Annotation\Route;
use Symfony\Component\Security\Core\Encoder\UserPasswordEncoderInterface;
use Symfony\Component\Security\Http\Authentication\AuthenticationUtils;
class SecurityController extends BaseController
{
/**
* @Route("/login", name="app_login")
*/
public function login(AuthenticationUtils $authenticationUtils): Response
{
if ($this->getUser()) {
return $this->redirectToRoute('panel_homepage');
}
// get the login error if there is one
$error = $authenticationUtils->getLastAuthenticationError();
// last username entered by the user
$lastUsername = $authenticationUtils->getLastUsername();
return $this->render('security/login.html.twig', ['last_username' => $lastUsername, 'error' => $error]);
}
/**
* @Route("/logout", name="app_logout")
*/
public function logout()
{
throw new \LogicException('This method can be blank - it will be intercepted by the logout key on your firewall.');
}
/**
* @Route("/forgot-password", name="app_forgot_password")
*/
public function forgot_password(Request $request, MailerInterface $mailer)
{
$v_email = $request->request->get('email');
if ($v_email){
$em = $this->getDoctrine()->getManager();
$user = $em->getRepository(User::class)->findOneBy(array('email'=>$v_email));
if (!$user){
return $this->render('security/forgot_password.html.twig',array('error'=>'Lütfen kayıtlı e-posta giriniz.'));
}
$token = sha1(md5($v_email));
$user->setPasswordToken($token);
$em->persist($user);
$em->flush();
$email = (new Email())
->from(new Address($this->ayarlar('sistemPosta'),$this->ayarlar('siteAdi')))
->to($v_email)
//->cc('cc@example.com')
//->bcc('bcc@example.com')
//->replyTo('fabien@example.com')
->priority(Email::PRIORITY_HIGH)
->subject('Şifremi unuttum - '.$this->ayarlar('siteAdi'))
//->text('Sending emails is fun again!')
->html($this->renderView('mail/sifremi_unuttum.html.twig',array('konu'=>'Şifremi unuttum','token'=>$token,'user'=>$user)));
$mailer->send($email);
return $this->render('security/forgot_password.html.twig',array('success'=>'Şifreni yenileme bağlantısı e-posta adresine gönderildi.'));
}
return $this->render('security/forgot_password.html.twig');
}
/**
* @Route("/reset-password/{token}", name="app_reset_password")
*/
public function reset_password(Request $request, $token, UserPasswordEncoderInterface $passwordEncoder)
{
$em = $this->getDoctrine()->getManager();
$user = $em->getRepository(User::class)->findOneBy(array('passwordToken'=>$token));
if ($user) {
$password = $request->request->get('password');
if ($password){
$user->setPassword($passwordEncoder->encodePassword($user, $password));
$user->setPasswordToken(null);
$em->persist($user);
$em->flush();
return $this->render('security/reset_password.html.twig',array('success'=>'Şifren yenilendi. Artık giriş yapabilirsin.'));
}
} else {
return $this->redirectToRoute('app_login');
}
return $this->render('security/reset_password.html.twig');
}
/**
* @Route("/replace-password", name="app_replace_password")
*/
public function replace_password(Request $request, UserPasswordEncoderInterface $passwordEncoder)
{
$em = $this->getDoctrine()->getManager();
$user = $this->getUser();
if ($user->getIsPassword()==true){
return $this->redirectToRoute('panel_homepage');
}
$geciciSifre = $request->request->get('geciciSifre');
$password = $request->request->get('password');
if ($password){
if (!$passwordEncoder->isPasswordValid($user,$geciciSifre)){
return $this->render('security/replace_password.html.twig',array('error'=>'Geçici şifreni yanlış girdin. Tekrar dene.'));
}
$user->setPassword($passwordEncoder->encodePassword($user, $password));
$user->setIsPassword(1);
$em->persist($user);
$em->flush();
return $this->render('security/replace_password.html.twig',array('success'=>'Şifren değiştirildi. Yönlendiriliyorsun..'));
}
return $this->render('security/replace_password.html.twig');
}
}