<?php
namespace App\Controller;
use App\Entity\User;
use App\Repository\UserRepository;
use Doctrine\ORM\EntityManagerInterface;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\Form\Extension\Core\Type\EmailType;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\Routing\Annotation\Route;
use Symfony\Component\Security\Core\Encoder\UserPasswordEncoderInterface;
use Symfony\Component\Security\Core\Security;
use Symfony\Component\Security\Http\Authentication\AuthenticationUtils;
class SecurityController extends AbstractController
{
/**
* @Route("/", name="login")
*/
public function login(AuthenticationUtils $authenticationUtils)
{
//Check if user connected, redirect to Project
if ($this->getUser()) {
return $this->redirectToRoute("files_users_manage_home_admin");
}
$error = $authenticationUtils->getLastAuthenticationError();
$lastUsername = $authenticationUtils->getLastUsername();
return $this->render('security/login.html.twig', [
'current_menu' => 'login',
'last_username' => $lastUsername,
'error' => $error
]);
}
/**
* @Route("/login_success", name="login_success")
*/
public function postLoginRedirectAction(Security $security)
{
$user = $security->getUser();
if(!$user->getIsActive()){
//check if userIsActive == true
$this->addFlash('error','Your account has been disabled ');
return $this->redirectToRoute("logout");
} else if ($user->checkIfAllInformationsAreFullfilled()) {
//On vérifie que l'utilisateur a rempli toutes ces informations
// return $this->redirectToRoute("your_tasks"); ////// CHANGED TO /////
//return $this->redirectToRoute("admin_files_users",[]);
//list_tasks_user
return $this->redirectToRoute("files_users_manage_home_admin",[]);
//return $this->redirectToRoute("files_users_home_admin",[]);
} else{
return $this->redirectToRoute("register_infos");
}
}
/**
* @Route("/logout", name="logout")
*/
public function logout(AuthenticationUtils $authenticationUtils)
{
return $this->render('');
}
/**
* @Route("/reset-password", name="reset_password")
*/
public function resetPassword(Request $request, \Swift_Mailer $mailer, UserRepository $userRepository, UserPasswordEncoderInterface $encoder, EntityManagerInterface $entityManager)
{
$form = $this->createFormBuilder()
->add('email', EmailType::class)
->getForm();
$form->handleRequest($request);
if($form->isSubmitted() && $form->isValid())
{
$email = $form->get('email')->getData();
//generate new password
$newPassword = substr(str_shuffle(str_repeat("0123456789abcdefghijklmnopqrstuvwxyz", 5)), 0, 5);
//we get the user with email selected
$user = $userRepository->findUserWithEmail($email);
//if no user has this email we redirect to login with an error message
if($user == null){
$this->addFlash('error','Impossible to reset the password. No account with this email. ');
return $this->redirectToRoute('login');
}
//change password in the database for this user
$user->setPassword($encoder->encodePassword($user,$newPassword));
$entityManager->flush();
//Send mail reset password
$message = (new \Swift_Message('Your Metask account'))
// sender
->setFrom("no-reply@meridec.ch")
// recipient
->setTo($email)
// body email
->setBody(
$this->renderView(
'emails/reset_password.html.twig', [
'email'=> $email,
'password' => $newPassword
]
),
'text/html'
)
;
$mailer->send($message);
$this->addFlash('success','Your password has been reset with success ! An email has been sent to '.$email);
return $this->redirectToRoute('login');
}
return $this->render('security/reset_password.html.twig', [
'form'=>$form->createView()
]);
}
}