<?php
namespace App\FHI360\Access\Exchange\Controller;
use App\Entity\User;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route;
use App\FHI360\Access\Suite\Security\ExchangeAuthenticator;
use Symfony\Component\Security\Core\Encoder\UserPasswordEncoderInterface;
use Symfony\Component\Security\Guard\GuardAuthenticatorHandler;
use Symfony\Component\Security\Http\Authentication\AuthenticationUtils;
class SecurityController extends Controller
{
/**
* @Route("/login", name="exchange_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('Exchange/Security/login.html.twig', [
'last_username' => $lastUsername,
'error' => $error
]);
}
/**
* @Route("/logout", name="exchange_logout")
*/
public function logout(): void
{
throw new \LogicException('This method can be blank - it will be intercepted by the logout key on your firewall.');
}
/**
* @Route("/register", name="exchange_register")
*/
public function register(Request $request, UserPasswordEncoderInterface $passwordEncoder, GuardAuthenticatorHandler $guardHandler, ExchangeAuthenticator $formAuthenticator)
{
// TODO - use Symfony forms & validation
if ($request->isMethod('POST')) {
$user = new User();
$user->setEmail($request->request->get('email'));
$user->setUsername($request->request->get('username'));
$user->setParticipantId($request->request->get('participant_id'));
$user->setPassword($passwordEncoder->encodePassword(
$user,
$request->request->get('password')
));
$em = $this->getDoctrine()->getManager();
$em->persist($user);
$em->flush();
return $guardHandler->authenticateUserAndHandleSuccess(
$user,
$request,
$formAuthenticator,
'main'
);
}
return $this->render('Exchange/Security/register.html.twig');
}
}