<?php
namespace App\Controller\Admin\Security;
use App\Entity\Users\User;
use App\Form\Admin\Security\RegisterForm;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Request;
use Doctrine\ORM\EntityManagerInterface;
use Symfony\Component\Routing\Annotation\Route;
use Symfony\Component\Security\Http\Authentication\AuthenticationUtils;
use Symfony\Component\Security\Core\User\UserInterface;
use Symfony\Component\Form\Extension\Core\Type AS Type;
use Symfony\Component\Security\Core\Authentication\Token\UsernamePasswordToken;
use Symfony\Component\Security\Http\Event\InteractiveLoginEvent;
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
use Symfony\Component\Form\FormError;
use Symfony\Component\PasswordHasher\Hasher\UserPasswordHasherInterface;
use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface;
use Symfony\Component\Routing\RouterInterface;
class SecurityController extends AbstractController
{
public function __construct(EntityManagerInterface $entityManager, EventDispatcherInterface $eventDispatcher, RouterInterface $router)
{
$this->em = $entityManager;
$this->eventDispatcher = $eventDispatcher;
$this->router = $router;
}
/**
* @Route("/admin", name="admin_security_index")
*/
public function index(Request $request, UserInterface $user = null)
{
return $this->redirect($this->generateUrl('admin_dashboard_index'));
}
/**
* @Route("/admin/login", name="admin_security_login")
*/
public function login(Request $request, AuthenticationUtils $authUtils, UserInterface $user = null)
{
// Already logged in?
if($user)
return $this->redirect($this->generateUrl('admin_dashboard_index'));
// Get the login error
$authenticationError = $authUtils->getLastAuthenticationError();
// Get existing user so we know to show the register button or not
$existingUser = $this->em->getRepository(User::class)->findOneFiltered();
// Render view
return $this->render('Admin/Security/login.html.twig', array(
'authenticationError' => ($authenticationError ? $authenticationError->getMessage() : null),
'showRegisterButton' => $existingUser ? false : true
));
}
/**
* @Route("/admin/login_check", name="admin_security_logincheck")
*/
public function loginCheck()
{
}
/**
* @Route("/admin/logout", name="admin_security_logout")
*/
public function logout()
{
}
/**
* @Route("/admin/login_failure", name="admin_security_loginfailure")
*/
public function loginFailure()
{
// Redirect
return $this->redirect($this->generateUrl("admin_security_login"));
}
/**
* @Route("/admin/register", name="admin_security_register")
*/
public function register(Request $request, UserPasswordHasherInterface $passwordHasher, TokenStorageInterface $tokenStorage, UserInterface $user = null)
{
// Already logged in?
if($user)
return $this->redirect($this->generateUrl('admin_dashboard_index'));
// Get existing users - this can only happen if there are no users already
$existingUser = $this->em->getRepository(User::class)->findOneFiltered();
if($existingUser)
{
$this->addFlash('error', "Registration is not available because a user already exists");
return $this->redirect($this->generateUrl('admin_security_login'));
}
// New user instance
$user = new User();
// Get form
$registerForm = $this->createForm(RegisterForm::class, $user)
->add('save', Type\SubmitType::class, array(
'label' => "Register"
));
// Handle the form submission
$registerForm->handleRequest($request);
if($registerForm->isSubmitted())
{
// Check name length
if(!strlen($registerForm->get('name')->getData()))
{
// Add error
$registerForm->get('name')->addError(new FormError("You must provide your name"));
}
// Check password length
if(strlen($registerForm->get('password')->getData()) < 5)
{
// Add error
$registerForm->get('password')->addError(new FormError("Must be at least 5 characters"));
}
// Form is valid?
if($registerForm->isValid())
{
// Email taken?
$conflictingUser = $this->em->getRepository(User::class)->findOneFiltered(array(
array("email", "eq", $registerForm->get('email')->getData())
));
if($conflictingUser)
$registerForm->get('email')->addError(new FormError("Email is already in use"));
}
// Form is valid?
if($registerForm->isValid())
{
// Hash password
$hashedPassword = $passwordHasher->hashPassword($user, $registerForm->get('password')->getData());
$user->setPassword($hashedPassword);
// Persist & flush
$this->em->persist($user);
$this->em->flush();
// Generate a token
$token = new UsernamePasswordToken($user, $registerForm->get('password')->getData(), $user->getRoles());
// Set the token
$tokenStorage->setToken($token);
// Fire the login event
$event = new InteractiveLoginEvent($request, $token);
$this->eventDispatcher->dispatch($event, "security.interactive_login");
// Redirect
return $this->redirect($this->generateUrl('admin_dashboard_index'));
}
}
// Render view
return $this->render('Admin/Security/register.html.twig', array(
'registerForm' => $registerForm->createView()
));
}
}