<?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, RoleService $roleService) {
$this->em = $entityManager;
$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('recipe_list');
}
else if($this->roleService->isGranted('ROLE_BRAND_AMBASSADOR', $userCurrent)){
return $this->redirectToRoute('recipe_list');
}
else if($this->roleService->isGranted('ROLE_SELLER', $userCurrent)){
return $this->redirectToRoute('recipe_list');
}
}
// 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("/signin-end/{token}/", name="signin_end")
*/
public function signinEndAction(Request $request, $token, UserPasswordEncoderInterface $passwordEncoder)
{
//Load parameter
$token = $request->get('token');
$user = $this->em->getRepository('App:User')
->findOneBy(Array("tokenReset" => $token));
$userCurrent = $this->get('security.token_storage')->getToken()->getUser();
if($userCurrent != "anon."){
$this->addFlash('danger','You are already logged into another account, you cannot complete a registration.');
return $this->redirectToRoute('app_login');
}
if(!$user){
$this->addFlash('danger',"This URL is not functional");
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','Your password has been completed successfully. You can now connect');
return $this->redirectToRoute('app_login');
}
return $this->render('registration/signinEnd.html.twig', Array(
'form' => $form->createView(),
'token' => $token
));
}
}