Expand user and allow everybody to register

Automatically login after registering
This commit is contained in:
Tim
2023-04-03 22:19:28 +02:00
parent 5be77eeba1
commit bf83e5aabd
8 changed files with 161 additions and 3 deletions

View File

@ -7,6 +7,7 @@ use App\Form\RegistrationFormType;
use Doctrine\ORM\EntityManagerInterface;
use Exception;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Bundle\SecurityBundle\Security;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\PasswordHasher\Hasher\UserPasswordHasherInterface;
@ -39,6 +40,7 @@ class SecurityController extends AbstractController
Request $request,
UserPasswordHasherInterface $userPasswordHasher,
EntityManagerInterface $entityManager,
Security $security,
): Response
{
$user = new User();
@ -59,9 +61,11 @@ class SecurityController extends AbstractController
$entityManager->persist($user);
$entityManager->flush();
// do anything else you need here, like send an email
$this->addFlash('success', sprintf('Successfully registered user %s', $user->getUsername()));
$this->addFlash('success', sprintf('Successfully registered user %s and logged in', $user->getUsername()));
return $this->redirectToRoute('register');
$security->login($user, 'remember_me');
return $this->redirectToRoute('user_profile');
}
}

20
src/Entity/Email.php Normal file
View File

@ -0,0 +1,20 @@
<?php
namespace App\Entity;
use App\Repository\EmailRepository;
use Doctrine\ORM\Mapping as ORM;
#[ORM\Entity(repositoryClass: EmailRepository::class)]
class Email
{
#[ORM\Id]
#[ORM\GeneratedValue]
#[ORM\Column]
private ?int $id = null;
public function getId(): ?int
{
return $this->id;
}
}

View File

@ -28,6 +28,12 @@ class User implements UserInterface, PasswordAuthenticatedUserInterface
#[ORM\Column]
private ?string $password = null;
#[ORM\Column(length: 255)]
private ?string $name = null;
#[ORM\Column(length: 255)]
private ?string $email = null;
public function getId(): ?int
{
return $this->id;
@ -97,4 +103,28 @@ class User implements UserInterface, PasswordAuthenticatedUserInterface
// If you store any temporary, sensitive data on the user, clear it here
// $this->plainPassword = null;
}
public function getName(): ?string
{
return $this->name;
}
public function setName(string $name): self
{
$this->name = $name;
return $this;
}
public function getEmail(): ?string
{
return $this->email;
}
public function setEmail(string $email): self
{
$this->email = $email;
return $this;
}
}

View File

@ -0,0 +1,66 @@
<?php
namespace App\Repository;
use App\Entity\Email;
use Doctrine\Bundle\DoctrineBundle\Repository\ServiceEntityRepository;
use Doctrine\Persistence\ManagerRegistry;
/**
* @extends ServiceEntityRepository<Email>
*
* @method Email|null find($id, $lockMode = null, $lockVersion = null)
* @method Email|null findOneBy(array $criteria, array $orderBy = null)
* @method Email[] findAll()
* @method Email[] findBy(array $criteria, array $orderBy = null, $limit = null, $offset = null)
*/
class EmailRepository extends ServiceEntityRepository
{
public function __construct(ManagerRegistry $registry)
{
parent::__construct($registry, Email::class);
}
public function save(Email $entity, bool $flush = false): void
{
$this->getEntityManager()->persist($entity);
if ($flush) {
$this->getEntityManager()->flush();
}
}
public function remove(Email $entity, bool $flush = false): void
{
$this->getEntityManager()->remove($entity);
if ($flush) {
$this->getEntityManager()->flush();
}
}
// /**
// * @return Email[] Returns an array of Email objects
// */
// public function findByExampleField($value): array
// {
// return $this->createQueryBuilder('e')
// ->andWhere('e.exampleField = :val')
// ->setParameter('val', $value)
// ->orderBy('e.id', 'ASC')
// ->setMaxResults(10)
// ->getQuery()
// ->getResult()
// ;
// }
// public function findOneBySomeField($value): ?Email
// {
// return $this->createQueryBuilder('e')
// ->andWhere('e.exampleField = :val')
// ->setParameter('val', $value)
// ->getQuery()
// ->getOneOrNullResult()
// ;
// }
}