src/Controller/SecurityController.php line 20

Open in your IDE?
  1. <?php
  2. namespace App\Controller;
  3. use App\Entity\User;
  4. use Sensio\Bundle\FrameworkExtraBundle\Configuration\IsGranted;
  5. use App\Form\RegistrationFormType;
  6. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  7. use Symfony\Component\HttpFoundation\Request;
  8. use Symfony\Component\HttpFoundation\Response;
  9. use Symfony\Component\Routing\Annotation\Route;
  10. use Symfony\Component\Security\Core\Encoder\UserPasswordEncoderInterface;
  11. use Symfony\Component\Security\Http\Authentication\AuthenticationUtils;
  12. class SecurityController extends AbstractController
  13. {
  14.     /**
  15.      * @Route("/login", name="login")
  16.      */
  17.     public function login(AuthenticationUtils $authenticationUtils): Response
  18.     {
  19.          if ($this->getUser() && $this->getUser()->isActive()) {
  20.              return $this->redirectToRoute('home');
  21.          }
  22.         // get the login error if there is one
  23.         $error = $authenticationUtils->getLastAuthenticationError();
  24.         // last username entered by the user
  25.         $email = $authenticationUtils->getLastUsername();
  26.         return $this->render('security/login.html.twig', ['email' => $email, 'error' => $error]);
  27.     }
  28.     /**
  29.      * @Route("/register", name="register")
  30.      * @IsGranted("ROLE_REGISTER")
  31.      */
  32.     public function register(Request $request, UserPasswordEncoderInterface $passwordEncoder): Response
  33.     {
  34.         $user = new User();
  35.         $form = $this->createForm(RegistrationFormType::class, $user);
  36.         $form->handleRequest($request);
  37.         if ($form->isSubmitted() && $form->isValid()) {
  38.             // encode the plain password
  39.             $user->setPassword(
  40.                 $passwordEncoder->encodePassword(
  41.                     $user,
  42.                     $form->get('plainPassword')->getData()
  43.                 )
  44.             );
  45.             if($this->isGranted("ROLE_ADMIN") || $this->isGranted("ROLE_SUPER_ADMIN")){
  46.                 $user->setEmailValidate(TRUE);
  47.                 $user->setActive(TRUE);
  48.             }
  49.             
  50.             $entityManager = $this->getDoctrine()->getManager();
  51.             $entityManager->persist($user);
  52.             $entityManager->flush();
  53.             // do anything else you need here, like send an email
  54.             return $this->redirectToRoute('home');
  55.         }
  56.         return $this->render('security/register.html.twig', [
  57.             'registrationForm' => $form->createView(),
  58.         ]);
  59.     }
  60.     /**
  61.      * @Route("/logout", name="logout")
  62.      */
  63.     public function logout()
  64.     {
  65.         throw new \Exception('This method can be blank - it will be intercepted by the logout key on your firewall');
  66.     }
  67.     /**
  68.      * @Route("/maintenance", name="maintenance")
  69.      */
  70.     public function maintenance(): Response
  71.     {
  72.         return $this->render('security/maintenance.html.twig');
  73.     }
  74. }