Implemented FOS bundle

This commit is contained in:
Kevin Frantz
2018-09-13 02:24:25 +02:00
parent 777b4aee61
commit 629ed13c51
46 changed files with 451 additions and 202 deletions

View File

@@ -1,61 +0,0 @@
<?php
namespace App\Controller;
use App\Form\UserType;
use App\Entity\User;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\Routing\Annotation\Route;
use Symfony\Component\Security\Core\Encoder\UserPasswordEncoderInterface;
use Symfony\Component\HttpFoundation\RedirectResponse;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Translation\TranslatorInterface;
use Symfony\Component\Form\FormInterface;
class RegistrationController extends AbstractController
{
/**
*
* @var User
*/
private $user;
/**
*
* @Route("/register", name="user_register")
*/
public function register(Request $request, UserPasswordEncoderInterface $passwordEncoder,TranslatorInterface $translator): Response
{
$this->user = new User();
$form = $this->createForm(UserType::class, $this->user);
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
$this->encodePassword($passwordEncoder);
$this->saveUser($translator);
return $this->redirectToRoute('login');
}
return $this->render("user/register.html.twig", array(
'form' => $form->createView()
));
}
public function encodePassword(UserPasswordEncoderInterface $passwordEncoder): void
{
$password = $passwordEncoder->encodePassword($this->user, $this->user->getPlainPassword());
$this->user->setPassword($password);
}
private function saveUser(TranslatorInterface $translator): void
{
$entityManager = $this->getDoctrine()->getManager();
$entityManager->persist($this->user);
try {
$entityManager->flush();
$this->addFlash('success', $translator->trans('User "%username%" created!',['%username%'=>$this->user->getUsername()]));
} catch (\Exception $exception) {
$this->addFlash('danger', $translator->trans('User "%username%" could not be created!',['%username%'=>$this->user->getUsername()]));
}
}
}

View File

@@ -1,37 +0,0 @@
<?php
namespace App\Controller;
use Symfony\Component\Security\Http\Authentication\AuthenticationUtils;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\Translation\TranslatorInterface;
/**
*
* @author kevinfrantz
*
*/
class SecurityController extends AbstractController
{
/**
*
* @Route("/login", name="login")
*/
public function login(AuthenticationUtils $authenticationUtils,TranslatorInterface $translator): Response
{
$error = $authenticationUtils->getLastAuthenticationError();
if ($error) {
$this->addFlash('danger', $translator->trans($error->getMessageKey(),$error->getMessageData(),'security'));
}else{
$lastUsername = $authenticationUtils->getLastUsername();
if($lastUsername){
$this->addFlash('success', $translator->trans('User %user% loged in.',['%user%'=>$lastUsername]));
}
}
return $this->render("user/login.html.twig",[
'last_username'=>$authenticationUtils->getLastUsername(),
]);
}
}