From bf83e5aabd8692a7b5c1aca3f252aa773eeddba8 Mon Sep 17 00:00:00 2001 From: tim Date: Mon, 3 Apr 2023 22:19:28 +0200 Subject: [PATCH] Expand user and allow everybody to register Automatically login after registering --- config/packages/security.yaml | 2 +- config/packages/twig.yaml | 1 + migrations/Version20230403161503.php | 33 ++++++++++++++ src/Controller/SecurityController.php | 8 +++- src/Entity/Email.php | 20 ++++++++ src/Entity/User.php | 30 ++++++++++++ src/Repository/EmailRepository.php | 66 +++++++++++++++++++++++++++ templates/base/navbar.html.twig | 4 ++ 8 files changed, 161 insertions(+), 3 deletions(-) create mode 100644 migrations/Version20230403161503.php create mode 100644 src/Entity/Email.php create mode 100644 src/Repository/EmailRepository.php diff --git a/config/packages/security.yaml b/config/packages/security.yaml index bfb1ff6..2c0256c 100644 --- a/config/packages/security.yaml +++ b/config/packages/security.yaml @@ -39,8 +39,8 @@ security: # Note: Only the *first* access control that matches will be used access_control: - { path: ^/login$, role: PUBLIC_ACCESS } + - { path: ^/register, role: PUBLIC_ACCESS } - { path: ^/logout$, role: ROLE_USER } - - { path: ^/register, role: ROLE_ADMIN } - { path: ^/admin, role: ROLE_ADMIN } - { path: ^/, role: ROLE_USER } diff --git a/config/packages/twig.yaml b/config/packages/twig.yaml index f9f4cc5..3be7d52 100644 --- a/config/packages/twig.yaml +++ b/config/packages/twig.yaml @@ -1,5 +1,6 @@ twig: default_path: '%kernel.project_dir%/templates' + form_themes: ['bootstrap_5_layout.html.twig'] when@test: twig: diff --git a/migrations/Version20230403161503.php b/migrations/Version20230403161503.php new file mode 100644 index 0000000..b6fab8a --- /dev/null +++ b/migrations/Version20230403161503.php @@ -0,0 +1,33 @@ +addSql('CREATE TABLE email (id INT AUTO_INCREMENT NOT NULL, PRIMARY KEY(id)) DEFAULT CHARACTER SET utf8mb4 COLLATE `utf8mb4_unicode_ci` ENGINE = InnoDB'); + $this->addSql('ALTER TABLE user ADD name VARCHAR(255) NOT NULL, ADD email VARCHAR(255) NOT NULL'); + } + + public function down(Schema $schema): void + { + // this down() migration is auto-generated, please modify it to your needs + $this->addSql('DROP TABLE email'); + $this->addSql('ALTER TABLE `user` DROP name, DROP email'); + } +} diff --git a/src/Controller/SecurityController.php b/src/Controller/SecurityController.php index 81d0e4e..51f10b3 100644 --- a/src/Controller/SecurityController.php +++ b/src/Controller/SecurityController.php @@ -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'); } } diff --git a/src/Entity/Email.php b/src/Entity/Email.php new file mode 100644 index 0000000..ca5d18c --- /dev/null +++ b/src/Entity/Email.php @@ -0,0 +1,20 @@ +id; + } +} diff --git a/src/Entity/User.php b/src/Entity/User.php index ee129ff..5a2b350 100644 --- a/src/Entity/User.php +++ b/src/Entity/User.php @@ -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; + } } diff --git a/src/Repository/EmailRepository.php b/src/Repository/EmailRepository.php new file mode 100644 index 0000000..cb6ba35 --- /dev/null +++ b/src/Repository/EmailRepository.php @@ -0,0 +1,66 @@ + + * + * @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() +// ; +// } +} diff --git a/templates/base/navbar.html.twig b/templates/base/navbar.html.twig index b68a61e..984f013 100644 --- a/templates/base/navbar.html.twig +++ b/templates/base/navbar.html.twig @@ -26,6 +26,10 @@ + {% else %} + {% endif %}