src/FHI360/Access/Exchange/Controller/SecurityController.php line 34

Open in your IDE?
  1. <?php
  2. namespace App\FHI360\Access\Exchange\Controller;
  3. use App\Entity\User;
  4. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  5. use Symfony\Bundle\FrameworkBundle\Controller\Controller;
  6. use Symfony\Component\HttpFoundation\Request;
  7. use Symfony\Component\HttpFoundation\Response;
  8. use Symfony\Component\Routing\Annotation\Route;
  9. use App\FHI360\Access\Suite\Security\ExchangeAuthenticator;
  10. use Symfony\Component\Security\Core\Encoder\UserPasswordEncoderInterface;
  11. use Symfony\Component\Security\Guard\GuardAuthenticatorHandler;
  12. use Symfony\Component\Security\Http\Authentication\AuthenticationUtils;
  13. class SecurityController extends Controller
  14. {
  15.     /**
  16.      * @Route("/login", name="exchange_login")
  17.      */
  18.     public function login(AuthenticationUtils $authenticationUtils): Response
  19.     {
  20.         // if ($this->getUser()) {
  21.         //     return $this->redirectToRoute('target_path');
  22.         // }
  23.         // get the login error if there is one
  24.         $error $authenticationUtils->getLastAuthenticationError();
  25.         // last username entered by the user
  26.         $lastUsername $authenticationUtils->getLastUsername();
  27.         return $this->render('Exchange/Security/login.html.twig', [
  28.             'last_username' => $lastUsername,
  29.             'error' => $error
  30.         ]);
  31.     }
  32.     /**
  33.      * @Route("/logout", name="exchange_logout")
  34.      */
  35.     public function logout(): void
  36.     {
  37.         throw new \LogicException('This method can be blank - it will be intercepted by the logout key on your firewall.');
  38.     }
  39.     /**
  40.      * @Route("/register", name="exchange_register")
  41.      */
  42.     public function register(Request $requestUserPasswordEncoderInterface $passwordEncoderGuardAuthenticatorHandler $guardHandlerExchangeAuthenticator $formAuthenticator)
  43.     {
  44.         // TODO - use Symfony forms & validation
  45.         if ($request->isMethod('POST')) {
  46.             $user = new User();
  47.             $user->setEmail($request->request->get('email'));
  48.             $user->setUsername($request->request->get('username'));
  49.             $user->setParticipantId($request->request->get('participant_id'));
  50.             $user->setPassword($passwordEncoder->encodePassword(
  51.                 $user,
  52.                 $request->request->get('password')
  53.             ));
  54.             $em $this->getDoctrine()->getManager();
  55.             $em->persist($user);
  56.             $em->flush();
  57.             return $guardHandler->authenticateUserAndHandleSuccess(
  58.                 $user,
  59.                 $request,
  60.                 $formAuthenticator,
  61.                 'main'
  62.             );
  63.         }
  64.         return $this->render('Exchange/Security/register.html.twig');
  65.     }
  66. }