<?php
namespace App\Controller;
use App\Entity\User;
use KnpU\OAuth2ClientBundle\Client\ClientRegistry;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route;
use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
use Symfony\Component\Security\Core\Encoder\UserPasswordEncoderInterface;
use Symfony\Component\Security\Csrf\TokenGenerator\TokenGeneratorInterface;
use Symfony\Component\Security\Http\Authentication\AuthenticationUtils;
class SecurityController extends AbstractController
{
/**
* @Route("/login", name="app_login")
*/
public function login(AuthenticationUtils $authenticationUtils): Response
{
// 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("/login-modal", name="app_login_modal",options={"expose"=true})
*/
public function loginModal(AuthenticationUtils $authenticationUtils): Response
{
// get the login error if there is one
$error = $authenticationUtils->getLastAuthenticationError();
// last username entered by the user
$lastUsername = $authenticationUtils->getLastUsername();
return $this->render('front/myaccount/login-modal.html.twig', ['last_username' => $lastUsername, 'error' => $error]);
}
/**
* @Route("/connect/google", name="google_connect")
*/
public function connectGoole(ClientRegistry $clientRegistry): Response
{
$client = $clientRegistry->getClient('google');
return $client->redirect(['profile', 'email']);
}
/**
* @Route("/connect/microsoft", name="microsoft_connect")
*/
public function connectMicrosoft(ClientRegistry $clientRegistry): Response
{
$client = $clientRegistry->getClient('microsoft');
return $client->redirect(['wl.basic', 'wl.signin','wl.emails']);
}
/**
* @Route("/connect/apple", name="apple_connect")
*/
public function connectApple(ClientRegistry $clientRegistry): Response
{
$client = $clientRegistry->getClient('apple');
return $client->redirect(['name', 'email']);
}
/**
* @Route("/logout", name="app_logout", methods={"GET"})
*/
public function logout()
{
// controller can be blank: it will never be executed!
throw new \Exception('Don\'t forget to activate logout in security.yaml');
}
/**
* @Route("/mot-de-passe-oublie", name="app_forgotten_password")
*/
public function forgottenPassword(Request $request, UserPasswordEncoderInterface $encoder, \Swift_Mailer $mailer, TokenGeneratorInterface $tokenGenerator): Response
{
if ($request->isMethod('POST')) {
$email = $request->request->get('email');
$entityManager = $this->getDoctrine()->getManager();
$user = $entityManager->getRepository(User::class)->findOneByEmail($email);
/* @var $user User */
if ($user === null) {
$this->addFlash('danger', 'Email Inconnu');
return $this->redirectToRoute('app_login');
}
$token = $tokenGenerator->generateToken();
try{
$user->setResetToken($token);
$entityManager->flush();
} catch (\Exception $e) {
$this->addFlash('warning', $e->getMessage());
return $this->redirectToRoute('app_login');
}
$url = $this->generateUrl('app_reset_password', array('token' => $token), UrlGeneratorInterface::ABSOLUTE_URL);
$html = $this->renderView(
'emails/forgotten_password.html.twig',
[
'user' => $user,
'url' => $url
]
);
$message = (new \Swift_Message('Mot de passe oublié'))
->setFrom([$this->getParameter('admin_email') => 'COPEES'])
->setTo($user->getEmail())
->setBody(
$html,
'text/html'
);
$mailer->send($message);
$this->addFlash('notice', 'Mail envoyé');
return $this->redirectToRoute('app_login');
}
return $this->render('security/forgotten_password.html.twig');
}
/**
* @Route("/changer-mot-de-passe/{token}", name="app_reset_password")
*/
public function resetPassword(Request $request, string $token, UserPasswordEncoderInterface $passwordEncoder)
{
if ($request->isMethod('POST')) {
$entityManager = $this->getDoctrine()->getManager();
$user = $entityManager->getRepository(User::class)->findOneByResetToken($token);
/* @var $user User */
if ($user === null) {
$this->addFlash('danger', 'Token Inconnu');
return $this->redirectToRoute('app_login');
}
$user->setResetToken(null);
$user->setPassword($passwordEncoder->encodePassword($user, $request->request->get('password')));
$entityManager->flush();
$this->addFlash('notice', 'Mot de passe mis à jour');
return $this->redirectToRoute('app_login');
}else {
return $this->render('security/reset_password.html.twig', ['token' => $token]);
}
}
}