<?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 Symfony\Component\EventDispatcher\EventDispatcherInterface;
use Symfony\Component\Security\Core\Authentication\Token\UsernamePasswordToken;
use Symfony\Component\Security\Http\Event\InteractiveLoginEvent;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\IsGranted;
use Symfony\Contracts\Translation\TranslatorInterface;
use App\Entity\User;
use App\Form\UserType;
use App\Helper\EmailHelper;
class SecurityController extends AbstractController
{
/**
* @Route({
* "en": "/signin",
* "es": "/ingresar",
* "de": "/signin_de",
* "fr": "/signin_fr",
* "it": "/signin_it",
* "pt": "/signin_pt",
* "cn": "/signin_cn",
* "cn2": "/signin_cn2",
* }, name="app_login")
*/
public function login(AuthenticationUtils $authenticationUtils): Response
{
// if ($this->getUser()) {
// return $this->redirectToRoute('target_path');
// }
// get the login error if there is one
$error = $authenticationUtils->getLastAuthenticationError();
// last username entered by the user
$lastUsername = $authenticationUtils->getLastUsername();
return $this->render('jelly/security/login.html.twig', [
'last_username' => $lastUsername,
'error' => $error
]);
}
/**
* @Route("/logout", name="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({
* "en": "/signup",
* "es": "/registro",
* "de": "/signup_de",
* "fr": "/signup_fr",
* "it": "/signup_it",
* "pt": "/signup_pt",
* "cn": "/signup_cn",
* "cn2": "/signup_cn2",
* }, name="signup")
*/
public function signup(Request $request, UserPasswordEncoderInterface $passwordEncoder, EventDispatcherInterface $eventDispatcher, EmailHelper $emailHelper, TranslatorInterface $translator): Response
{
$form = $this->createForm(UserType::class, new User(), ['validation_groups' => ['Registration']] );
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
$user = $form->getData();
$user->setUsername($user->getEmail());
$user->setPassword($passwordEncoder->encodePassword(
$user,
$user->getPlainPassword()
));
$user->eraseCredentials();
$em = $this->getDoctrine()->getManager();
$em->persist($user);
$em->flush();
// Here, "public" is the name of the firewall in your security.yml
$token = new UsernamePasswordToken($user, $user->getPassword(), "public", $user->getRoles());
// For older versions of Symfony, use security.context here
$this->get("security.token_storage")->setToken($token);
// Fire the login event
// Logging the user in above the way we do it doesn't do this automatically
$event = new InteractiveLoginEvent($request, $token);
$eventDispatcher->dispatch($event, "security.interactive_login");
$emailHelper->sendMail( $user->getEmail(), $translator->trans("¡Bienvenido a beconnected!"), "email/registration.html.twig", ['firstName' => $user->getFirstName(), "user_locale" => $request->getLocale() ] );
return $this->redirectToRoute('home', ['locale' => $request->getLocale()]);
}
return $this->render('jelly/security/signup.html.twig', ['form' => $form->createView()]);
}
/**
* @Route({
* "en": "/profile",
* "es": "/perfil",
* "de": "/profil",
* "fr": "/profil_fr",
* "it": "/profilo",
* "pt": "/perfil_pt",
* "cn": "/profile_cn",
* "cn2": "/profile_cn2",
* }, name="profile")
* @IsGranted("ROLE_USER")
*/
public function profile(Request $request, UserPasswordEncoderInterface $passwordEncoder, EventDispatcherInterface $eventDispatcher, EmailHelper $emailHelper): Response
{
$form = $this->createForm(UserType::class, $this->getUser(), ['validation_groups' => ['Profile']] );
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
$user = $form->getData();
if($user->getPlainPassword()){
$user->setPassword($passwordEncoder->encodePassword(
$user,
$user->getPlainPassword()
));
$user->eraseCredentials();
}
$user->setIsAuthenticationless(false);
$em = $this->getDoctrine()->getManager();
$em->persist($user);
$em->flush();
return $this->redirectToRoute('home');
}
return $this->render('jelly/security/profile.html.twig', ['form' => $form->createView()]);
}
/**
* @Route("/admin/login", name="admin_app_login")
*/
public function adminLogin(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('angle/security/login.html.twig', [
'last_username' => $lastUsername,
'error' => $error
]);
}
}