src/Controller/Admin/Security/SecurityController.php line 53

Open in your IDE?
  1. <?php
  2. namespace App\Controller\Admin\Security;
  3. use App\Entity\Users\User;
  4. use App\Form\Admin\Security\RegisterForm;
  5. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  6. use Symfony\Component\HttpFoundation\Request;
  7. use Doctrine\ORM\EntityManagerInterface;
  8. use Symfony\Component\Routing\Annotation\Route;
  9. use Symfony\Component\Security\Http\Authentication\AuthenticationUtils;
  10. use Symfony\Component\Security\Core\User\UserInterface;
  11. use Symfony\Component\Form\Extension\Core\Type AS Type;
  12. use Symfony\Component\Security\Core\Authentication\Token\UsernamePasswordToken;
  13. use Symfony\Component\Security\Http\Event\InteractiveLoginEvent;
  14. use Symfony\Component\EventDispatcher\EventDispatcherInterface;
  15. use Symfony\Component\Form\FormError;
  16. use Symfony\Component\PasswordHasher\Hasher\UserPasswordHasherInterface;
  17. use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
  18. use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface;
  19. use Symfony\Component\Routing\RouterInterface;
  20. class SecurityController extends AbstractController
  21. {
  22.     public function __construct(EntityManagerInterface $entityManagerEventDispatcherInterface $eventDispatcherRouterInterface $router)
  23.     {
  24.         $this->em $entityManager;
  25.         $this->eventDispatcher $eventDispatcher;
  26.         $this->router $router;
  27.     }
  28.     /**
  29.     * @Route("/admin", name="admin_security_index")
  30.     */
  31.     public function index(Request $requestUserInterface $user null)
  32.     {
  33.         return $this->redirect($this->generateUrl('admin_dashboard_index'));
  34.     }
  35.     /**
  36.     * @Route("/admin/login", name="admin_security_login")
  37.     */
  38.     public function login(Request $requestAuthenticationUtils $authUtilsUserInterface $user null)
  39.     {
  40.         // Already logged in?
  41.         if($user)
  42.             return $this->redirect($this->generateUrl('admin_dashboard_index'));
  43.         // Get the login error
  44.         $authenticationError $authUtils->getLastAuthenticationError();
  45.         // Get existing user so we know to show the register button or not
  46.         $existingUser $this->em->getRepository(User::class)->findOneFiltered();
  47.         // Render view
  48.         return $this->render('Admin/Security/login.html.twig', array(
  49.             'authenticationError' => ($authenticationError $authenticationError->getMessage() : null),
  50.             'showRegisterButton' => $existingUser false true
  51.         ));
  52.     }
  53.     /**
  54.     * @Route("/admin/login_check", name="admin_security_logincheck")
  55.     */
  56.     public function loginCheck()
  57.     {
  58.     }
  59.     /**
  60.     * @Route("/admin/logout", name="admin_security_logout")
  61.     */
  62.     public function logout()
  63.     {
  64.     }
  65.     /**
  66.     * @Route("/admin/login_failure", name="admin_security_loginfailure")
  67.     */
  68.     public function loginFailure()
  69.     {
  70.         // Redirect
  71.         return $this->redirect($this->generateUrl("admin_security_login"));
  72.     }
  73.     /**
  74.     * @Route("/admin/register", name="admin_security_register")
  75.     */
  76.     public function register(Request $requestUserPasswordHasherInterface $passwordHasherTokenStorageInterface $tokenStorageUserInterface $user null)
  77.     {
  78.         // Already logged in?
  79.         if($user)
  80.             return $this->redirect($this->generateUrl('admin_dashboard_index'));
  81.         // Get existing users - this can only happen if there are no users already
  82.         $existingUser $this->em->getRepository(User::class)->findOneFiltered();
  83.         if($existingUser)
  84.         {
  85.             $this->addFlash('error'"Registration is not available because a user already exists");
  86.             return $this->redirect($this->generateUrl('admin_security_login'));
  87.         }
  88.         // New user instance
  89.         $user = new User();
  90.         // Get form
  91.         $registerForm $this->createForm(RegisterForm::class, $user)
  92.             ->add('save'Type\SubmitType::class, array(
  93.                 'label' => "Register"
  94.             ));
  95.         // Handle the form submission
  96.         $registerForm->handleRequest($request);
  97.         if($registerForm->isSubmitted())
  98.         {
  99.             // Check name length
  100.             if(!strlen($registerForm->get('name')->getData()))
  101.             {
  102.                 // Add error
  103.                 $registerForm->get('name')->addError(new FormError("You must provide your name"));
  104.             }
  105.             // Check password length
  106.             if(strlen($registerForm->get('password')->getData()) < 5)
  107.             {
  108.                 // Add error
  109.                 $registerForm->get('password')->addError(new FormError("Must be at least 5 characters"));
  110.             }
  111.             // Form is valid?
  112.             if($registerForm->isValid())
  113.             {
  114.                 // Email taken?
  115.                 $conflictingUser $this->em->getRepository(User::class)->findOneFiltered(array(
  116.                     array("email""eq"$registerForm->get('email')->getData())
  117.                 ));
  118.                 if($conflictingUser)
  119.                     $registerForm->get('email')->addError(new FormError("Email is already in use"));
  120.             }
  121.             // Form is valid?
  122.             if($registerForm->isValid())
  123.             {
  124.                 // Hash password
  125.                 $hashedPassword $passwordHasher->hashPassword($user$registerForm->get('password')->getData());
  126.                 $user->setPassword($hashedPassword);
  127.                 // Persist & flush
  128.                 $this->em->persist($user);
  129.                 $this->em->flush();
  130.                 // Generate a token
  131.                 $token = new UsernamePasswordToken($user$registerForm->get('password')->getData(), $user->getRoles());
  132.                 // Set the token
  133.                 $tokenStorage->setToken($token);
  134.                 // Fire the login event
  135.                 $event = new InteractiveLoginEvent($request$token);
  136.                 $this->eventDispatcher->dispatch($event"security.interactive_login");
  137.                 // Redirect
  138.                 return $this->redirect($this->generateUrl('admin_dashboard_index'));
  139.             }
  140.         }
  141.         // Render view
  142.         return $this->render('Admin/Security/register.html.twig', array(
  143.             'registerForm' => $registerForm->createView()
  144.         ));
  145.     }
  146. }