From 5350952bd30de2a1c645d2096e994343eb5144b3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Franti=C5=A1ek=20=C5=A0pa=C4=8Dek?= Date: Wed, 15 May 2024 04:49:04 +0200 Subject: [PATCH] Auth working --- config/packages/doctrine.yaml | 9 +++---- config/packages/security.yaml | 23 +++++++++++------- config/packages/web_profiler.yaml | 4 ++-- config/services.yaml | 1 + src/Controller/UserController.php | 8 +++++-- src/Document/Chart.php | 20 ++++++++-------- src/Document/User.php | 39 ++++++++++++++++--------------- templates/login.html.twig | 27 ++++++++++----------- 8 files changed, 71 insertions(+), 60 deletions(-) diff --git a/config/packages/doctrine.yaml b/config/packages/doctrine.yaml index e517e07..2f9b4b5 100644 --- a/config/packages/doctrine.yaml +++ b/config/packages/doctrine.yaml @@ -12,10 +12,11 @@ doctrine: auto_mapping: true mappings: App: - is_bundle: false - dir: '%kernel.project_dir%/src/Entity' - prefix: 'App\Entity' - alias: App + #is_bundle: false + #dir: '%kernel.project_dir%/src/Entity' + dir: '%kernel.project_dir%/src/Document' + prefix: 'App\Document' + #alias: App when@test: doctrine: diff --git a/config/packages/security.yaml b/config/packages/security.yaml index 0b0d691..73ba0a4 100644 --- a/config/packages/security.yaml +++ b/config/packages/security.yaml @@ -4,14 +4,21 @@ security: Symfony\Component\Security\Core\User\PasswordAuthenticatedUserInterface: 'auto' # https://symfony.com/doc/current/security.html#loading-the-user-the-user-provider providers: - users_in_memory: { memory: null } + #users_in_memory: { memory: null } + #app_user_provider: + # entity: + # class: App\Document\User + # property: email + my_mongo_provider: + mongodb: {class: App\Document\User, property: email} + firewalls: - dev: - pattern: ^/(_(profiler|wdt)|css|images|js)/ - security: false + #dev: + # pattern: ^/(_(profiler|wdt)|css|images|js)/ + # security: false main: lazy: true - provider: users_in_memory + provider: my_mongo_provider # activate different ways to authenticate # https://symfony.com/doc/current/security.html#the-firewall @@ -25,9 +32,9 @@ security: # Easy way to control access for large sections of your site # Note: Only the *first* access control that matches will be used access_control: - - { path: ^/admin, roles: ROLE_ADMIN } - role_hierarchy: - ROLE_ADMIN: ROLE_USER + # - { path: ^/admin, roles: ROLE_ADMIN } + #role_hierarchy: + # ROLE_ADMIN: ROLE_USER when@test: security: diff --git a/config/packages/web_profiler.yaml b/config/packages/web_profiler.yaml index d1a73dc..3198f31 100644 --- a/config/packages/web_profiler.yaml +++ b/config/packages/web_profiler.yaml @@ -1,6 +1,6 @@ when@dev: web_profiler: - toolbar: false + toolbar: true intercept_redirects: false framework: @@ -10,7 +10,7 @@ when@dev: when@test: web_profiler: - toolbar: false + toolbar: true intercept_redirects: false framework: diff --git a/config/services.yaml b/config/services.yaml index 1f42286..239c57e 100644 --- a/config/services.yaml +++ b/config/services.yaml @@ -8,6 +8,7 @@ parameters: env(MONGODB_DB): '' services: + Symfony\Component\HttpKernel\Profiler\Profiler: '@profiler' # default configuration for services in *this* file _defaults: autowire: true # Automatically injects dependencies in your services. diff --git a/src/Controller/UserController.php b/src/Controller/UserController.php index 1acb19e..fb7cd9d 100644 --- a/src/Controller/UserController.php +++ b/src/Controller/UserController.php @@ -8,6 +8,7 @@ use App\Form\Type\LoginType; use Doctrine\ODM\MongoDB\DocumentManager; use Symfony\Bundle\FrameworkBundle\Controller\AbstractController; use Symfony\Component\Security\Http\Authentication\AuthenticationUtils; +use Symfony\Component\PasswordHasher\Hasher\UserPasswordHasherInterface; use Symfony\Component\HttpFoundation\Response; use Symfony\Component\HttpFoundation\Request; use Symfony\Component\HttpFoundation\Redirect; @@ -19,7 +20,7 @@ class UserController extends AbstractController #[Route('/create', name: 'create', defaults: ['id' => null])] #[Route('/{id}/edit', name: 'edit')] - public function editAction(DocumentManager $dm, Request $request, ?int $id) + public function editAction(DocumentManager $dm, Request $request, UserPasswordHasherInterface $passwordHasher, ?string $id) { $user = $dm->getRepository(User::class)->find($id); if ($user == null) @@ -31,6 +32,9 @@ class UserController extends AbstractController if ($form->isSubmitted() && $form->isValid()) { $user = $form->getData(); + $hashedPassword = $passwordHasher->hashPassword($user, $user->getPassword()); + $user->setPassword($hashedPassword); + $dm->persist($user); $dm->flush(); @@ -46,7 +50,7 @@ class UserController extends AbstractController public function login(AuthenticationUtils $authenticationUtils): Response { return $this->render('login.html.twig', [ - 'last_username' => $authenticationUtils->getLastUsername(), + 'last_email' => $authenticationUtils->getLastUsername(), 'error' => $authenticationUtils->getLastAuthenticationError(), ]); } diff --git a/src/Document/Chart.php b/src/Document/Chart.php index faa9eb8..dcbb47c 100644 --- a/src/Document/Chart.php +++ b/src/Document/Chart.php @@ -17,8 +17,8 @@ class Chart #[MongoDB\Id] protected string $id; - #[MongoDB\ReferenceOne(targetDocument: User::class, inversedBy: "charts")] - protected $user; + //#[MongoDB\ReferenceOne(targetDocument: User::class, inversedBy: "charts")] + //protected $user; #[MongoDB\Field(type: 'string')] #[Assert\NotBlank] @@ -46,20 +46,20 @@ class Chart * * @return User */ - public function getUser(): ?User - { - return $this->user; - } + //public function getUser(): ?User + //{ + /// return $this->user; + //} /** * Set the user associated with the chart. * * @param User $user The user to set */ - public function setUser(?User $user): void - { - $this->user = $user; - } + //public function setUser(?User $user): void + //{ + // $this->user = $user; + //} public function getName(): ?string { diff --git a/src/Document/User.php b/src/Document/User.php index b138b47..4432159 100644 --- a/src/Document/User.php +++ b/src/Document/User.php @@ -23,34 +23,35 @@ class User implements UserInterface, PasswordAuthenticatedUserInterface #[MongoDB\Field(type: 'string')] #[Assert\NotBlank] #[Assert\Email] - protected ?string $email = null; + protected string $email; #[MongoDB\Field(type: 'string')] #[Assert\NotBlank] - protected ?string $password = null; + private string $password; #[MongoDB\Field(type: 'collection')] private array $roles = ['ROLE_USER']; - #[MongoDB\ReferenceMany(targetDocument: Chart::class, mappedBy: "user")] - protected $charts; + //#[MongoDB\ReferenceMany(targetDocument: Chart::class, mappedBy: "user")] + //protected $charts; public function getId(): string { return $this->id; } - public function getEmail(): ?string + public function getEmail(): string { return $this->email; } - public function setEmail(?string $email): void + public function setEmail(string $email): self { $this->email = $email; + return $this; } - public function getPassword(): ?string + public function getPassword(): string { return $this->password; } @@ -65,26 +66,26 @@ class User implements UserInterface, PasswordAuthenticatedUserInterface /** * @return Collection */ - public function getCharts(): Collection - { - return $this->charts; - } + //public function getCharts(): Collection + //{ + // return $this->charts; + //} /** * Adds a chart to the user. * * @param Chart $chart The chart to add */ - public function addChart(Chart $chart): void - { + //public function addChart(Chart $chart): void + //{ // Check if the chart already exists in the collection - if (!$this->charts->contains($chart)) { + // if (!$this->charts->contains($chart)) { // Add the chart to the collection - $this->charts->add($chart); + // $this->charts->add($chart); // Set the user reference in the chart entity - $chart->setUser($this); - } - } + // $chart->setUser($this); + // } + //} public function getRoles(): array { @@ -111,7 +112,7 @@ class User implements UserInterface, PasswordAuthenticatedUserInterface public function getUserIdentifier(): string { - return $this->email; + return (string) $this->email; } diff --git a/templates/login.html.twig b/templates/login.html.twig index 41ff2da..37c7174 100644 --- a/templates/login.html.twig +++ b/templates/login.html.twig @@ -8,25 +8,22 @@ {{ parent() }}
- {% block body %} - {% if error %} -
{{ error.messageKey|trans(error.messageData, 'security') }}
- {% endif %} + {% if error %} +
{{ error.messageKey|trans(error.messageData, 'security') }}
+ {% endif %} -
- - + + + - - + + - {# If you want to control the URL the user is redirected to on success - #} + {# If you want to control the URL the user is redirected to on success + #} - -
- {% endblock %} + +
{% endblock %}