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

@ -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 }

View File

@ -1,5 +1,6 @@
twig:
default_path: '%kernel.project_dir%/templates'
form_themes: ['bootstrap_5_layout.html.twig']
when@test:
twig:

View File

@ -0,0 +1,33 @@
<?php
declare(strict_types=1);
namespace DoctrineMigrations;
use Doctrine\DBAL\Schema\Schema;
use Doctrine\Migrations\AbstractMigration;
/**
* Auto-generated Migration: Please modify to your needs!
*/
final class Version20230403161503 extends AbstractMigration
{
public function getDescription(): string
{
return '';
}
public function up(Schema $schema): void
{
// this up() migration is auto-generated, please modify it to your needs
$this->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');
}
}

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()
// ;
// }
}

View File

@ -26,6 +26,10 @@
<li class="nav-item">
<a class="nav-link" href="{{ path('logout') }}">Logout</a>
</li>
{% else %}
<li class="nav-item">
<a class="nav-link" href="{{ path('register') }}">Register</a>
</li>
{% endif %}
</ul>
</div>