mirror of
https://github.com/kevinveenbirkenbach/infinito.git
synced 2025-01-09 14:07:25 +01:00
Added new registration form
This commit is contained in:
parent
a35ae03b7e
commit
a57ead18e2
61
application/src/Controller/RegistrationController.php
Normal file
61
application/src/Controller/RegistrationController.php
Normal file
@ -0,0 +1,61 @@
|
|||||||
|
<?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()]));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -1,66 +0,0 @@
|
|||||||
<?php
|
|
||||||
namespace App\Controller;
|
|
||||||
|
|
||||||
use Symfony\Component\HttpFoundation\Response;
|
|
||||||
use Symfony\Component\Routing\Annotation\Route;
|
|
||||||
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
|
|
||||||
use Symfony\Component\Security\Http\Authentication\AuthenticationUtils;
|
|
||||||
use App\Entity\User;
|
|
||||||
use Symfony\Component\Form\Extension\Core\Type\PasswordType;
|
|
||||||
use Symfony\Component\Form\Extension\Core\Type\SubmitType;
|
|
||||||
use Symfony\Component\Form\Extension\Core\Type\TextType;
|
|
||||||
use Symfony\Component\HttpFoundation\Request;
|
|
||||||
use Symfony\Component\Security\Core\Encoder\UserPasswordEncoderInterface;
|
|
||||||
|
|
||||||
/**
|
|
||||||
*
|
|
||||||
* @author kevinfrantz
|
|
||||||
*
|
|
||||||
*/
|
|
||||||
class UserController extends AbstractController implements UserControllerInterface
|
|
||||||
{
|
|
||||||
|
|
||||||
/**
|
|
||||||
*
|
|
||||||
* @Route("/user/logout", name="user_logout")
|
|
||||||
*/
|
|
||||||
public function logout(): Response
|
|
||||||
{
|
|
||||||
return $this->render("user/login.html.twig");
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
*
|
|
||||||
* @Route("/user/register", name="user_register")
|
|
||||||
*/
|
|
||||||
public function register(Request $request,UserPasswordEncoderInterface $encoder): Response
|
|
||||||
{
|
|
||||||
$user = new User();
|
|
||||||
$form = $this->createFormBuilder($user)
|
|
||||||
->add('username', TextType::class)
|
|
||||||
->add('password', PasswordType::class)
|
|
||||||
->add('save', SubmitType::class, [
|
|
||||||
'label' => 'register'
|
|
||||||
])
|
|
||||||
->getForm();
|
|
||||||
$form->handleRequest($request);
|
|
||||||
if ($form->isSubmitted() && $form->isValid()) {
|
|
||||||
$user = $form->getData();
|
|
||||||
$encoded = $encoder->encodePassword($user, $request->get('password'));
|
|
||||||
$user->setPassword($encoded);
|
|
||||||
$entityManager = $this->getDoctrine()->getManager();
|
|
||||||
$entityManager->persist($user);
|
|
||||||
try {
|
|
||||||
$entityManager->flush();
|
|
||||||
$this->addFlash('success', 'User created!');
|
|
||||||
} catch (\Exception $exception) {
|
|
||||||
$this->addFlash('danger', 'User could not be created!');
|
|
||||||
$this->addFlash('info', $exception->getMessage());
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
return $this->render("user/register.html.twig", [
|
|
||||||
'form' => $form->createView()
|
|
||||||
]);
|
|
||||||
}
|
|
||||||
}
|
|
@ -1,18 +0,0 @@
|
|||||||
<?php
|
|
||||||
namespace App\Controller;
|
|
||||||
|
|
||||||
use Symfony\Component\HttpFoundation\Response;
|
|
||||||
use Symfony\Component\Security\Http\Authentication\AuthenticationUtils;
|
|
||||||
use Symfony\Component\HttpFoundation\Request;
|
|
||||||
|
|
||||||
/**
|
|
||||||
*
|
|
||||||
* @author kevinfrantz
|
|
||||||
*
|
|
||||||
*/
|
|
||||||
interface UserControllerInterface
|
|
||||||
{
|
|
||||||
public function logout():Response;
|
|
||||||
|
|
||||||
#public function register(Request $request):Response;
|
|
||||||
}
|
|
33
application/src/Form/UserType.php
Normal file
33
application/src/Form/UserType.php
Normal file
@ -0,0 +1,33 @@
|
|||||||
|
<?php
|
||||||
|
namespace App\Form;
|
||||||
|
|
||||||
|
use App\Entity\User;
|
||||||
|
use Symfony\Component\Form\AbstractType;
|
||||||
|
use Symfony\Component\Form\FormBuilderInterface;
|
||||||
|
use Symfony\Component\OptionsResolver\OptionsResolver;
|
||||||
|
use Symfony\Component\Form\Extension\Core\Type\EmailType;
|
||||||
|
use Symfony\Component\Form\Extension\Core\Type\TextType;
|
||||||
|
use Symfony\Component\Form\Extension\Core\Type\RepeatedType;
|
||||||
|
use Symfony\Component\Form\Extension\Core\Type\PasswordType;
|
||||||
|
|
||||||
|
class UserType extends AbstractType
|
||||||
|
{
|
||||||
|
public function buildForm(FormBuilderInterface $builder, array $options)
|
||||||
|
{
|
||||||
|
$builder
|
||||||
|
->add('username', TextType::class)
|
||||||
|
->add('plainPassword', RepeatedType::class, array(
|
||||||
|
'type' => PasswordType::class,
|
||||||
|
'first_options' => array('label' => 'Password'),
|
||||||
|
'second_options' => array('label' => 'Repeat Password'),
|
||||||
|
))
|
||||||
|
;
|
||||||
|
}
|
||||||
|
|
||||||
|
public function configureOptions(OptionsResolver $resolver)
|
||||||
|
{
|
||||||
|
$resolver->setDefaults(array(
|
||||||
|
'data_class' => User::class,
|
||||||
|
));
|
||||||
|
}
|
||||||
|
}
|
@ -6,5 +6,6 @@
|
|||||||
<h1>{% trans %}register{% endtrans %}</h1>
|
<h1>{% trans %}register{% endtrans %}</h1>
|
||||||
{{ form_start(form) }}
|
{{ form_start(form) }}
|
||||||
{{ form_widget(form) }}
|
{{ form_widget(form) }}
|
||||||
|
<button type="submit" class="btn btn-primary">{% trans %}register{% endtrans %}</button>
|
||||||
{{ form_end(form) }}
|
{{ form_end(form) }}
|
||||||
{% endblock %}
|
{% endblock %}
|
||||||
|
Loading…
Reference in New Issue
Block a user