<?php
namespace App\Controller;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\Routing\Annotation\Route;
use Symfony\Component\Security\Http\Authentication\AuthenticationUtils;
use Symfony\Component\Security\Core\Encoder\UserPasswordEncoderInterface;
use Doctrine\ORM\EntityManagerInterface;
use App\Services\RoleService;
use App\Form\SigninEndType;
use App\Entity\User;
class SecurityController extends AbstractController
{
protected $em;
public function __construct(EntityManagerInterface $entityManager, \Swift_Mailer $mailer, RoleService $roleService) {
$this->em = $entityManager;
$this->mailer = $mailer;
$this->roleService = $roleService;
}
/**
* @Route("/login", name="app_login")
*/
public function login(AuthenticationUtils $authenticationUtils): Response
{
if ($this->getUser()) {
$userCurrent = $this->get('security.token_storage')->getToken()->getUser();
if($this->roleService->isGranted('ROLE_ADMIN', $userCurrent)){
return $this->redirectToRoute('seller_list');
}
else if($this->roleService->isGranted('ROLE_ADMIN_MANAGER', $userCurrent)){
return $this->redirectToRoute('manager_list');
}
else if($this->roleService->isGranted('ROLE_SELLER', $userCurrent)){
return $this->redirectToRoute('customer_list');
}
else if(
$this->roleService->isGranted('ROLE_MANAGER', $userCurrent) ||
$this->roleService->isGranted('ROLE_SIGNATORY', $userCurrent)
) {
return $this->redirectToRoute('document_list');
}
else if($this->roleService->isGranted('ROLE_CUSTOMER', $userCurrent)){
if($userCurrent->isIsAutoSignup()){
return $this->redirectToRoute('member_signup', array("token" => $userCurrent->getTokenSignupContinue()));
}
else{
return $this->redirectToRoute('customer_view', array("userID" => "me"));
}
}
}
// get the login error if there is one
$error = $authenticationUtils->getLastAuthenticationError();
// last username entered by the user
$lastUsername = $authenticationUtils->getLastUsername();
$userAgent = $_SERVER['HTTP_USER_AGENT'];
if(strstr($userAgent, "MSIE") || strstr($userAgent, "Trident")) {
$userAgent = "IE";
}
return $this->render('security/login.html.twig', [
'last_username' => $lastUsername,
'error' => $error,
'userAgent' => $userAgent
]);
}
/**
* @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("/signin-end/{token}/", name="signin_end")
*/
public function signinEndAction(Request $request, $token, UserPasswordEncoderInterface $passwordEncoder)
{
//Load parameter
$token = $request->get('token');
$user = $this->em->getRepository(User::class)
->findOneBy(Array("tokenReset" => $token));
$userCurrent = $this->get('security.token_storage')->getToken();
if($userCurrent){
$this->addFlash('danger','Vous êtes déjà connecté sur un autre compte, vous ne pouvez pas terminer une inscription.');
return $this->redirectToRoute('app_login');
}
if(!$user){
$this->addFlash('danger',"Cette URL n'est pas fonctionnelle");
return $this->redirectToRoute('app_login');
}
$form = $this->createForm(SigninEndType::class, $user);
$form->handleRequest($request);
if($form->isSubmitted() && $form->isValid()) {
$password = $passwordEncoder->encodePassword($user, $user->getPlainPassword());
$user->setPassword($password);
$user->setTokenReset(null);
$user->setStatus('ACTIVE');
$this->em->persist($user);
$this->em->flush();
//Send mail activation
//$this->get('fos_user.mailer')->sendConfirmationEmailMessage($user);
$this->addFlash('success','Votre mot de passe a bien été complété. Vous pouvez maintenant vous connecter');
return $this->redirectToRoute('app_login');
}
return $this->render('registration/signinEnd.html.twig', Array(
'form' => $form->createView(),
'token' => $token
));
}
}