Auth working

main
František Špaček 2 years ago
parent 1e6fae451f
commit 5350952bd3

@ -12,10 +12,11 @@ doctrine:
auto_mapping: true auto_mapping: true
mappings: mappings:
App: App:
is_bundle: false #is_bundle: false
dir: '%kernel.project_dir%/src/Entity' #dir: '%kernel.project_dir%/src/Entity'
prefix: 'App\Entity' dir: '%kernel.project_dir%/src/Document'
alias: App prefix: 'App\Document'
#alias: App
when@test: when@test:
doctrine: doctrine:

@ -4,14 +4,21 @@ security:
Symfony\Component\Security\Core\User\PasswordAuthenticatedUserInterface: 'auto' Symfony\Component\Security\Core\User\PasswordAuthenticatedUserInterface: 'auto'
# https://symfony.com/doc/current/security.html#loading-the-user-the-user-provider # https://symfony.com/doc/current/security.html#loading-the-user-the-user-provider
providers: 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: firewalls:
dev: #dev:
pattern: ^/(_(profiler|wdt)|css|images|js)/ # pattern: ^/(_(profiler|wdt)|css|images|js)/
security: false # security: false
main: main:
lazy: true lazy: true
provider: users_in_memory provider: my_mongo_provider
# activate different ways to authenticate # activate different ways to authenticate
# https://symfony.com/doc/current/security.html#the-firewall # 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 # Easy way to control access for large sections of your site
# Note: Only the *first* access control that matches will be used # Note: Only the *first* access control that matches will be used
access_control: access_control:
- { path: ^/admin, roles: ROLE_ADMIN } # - { path: ^/admin, roles: ROLE_ADMIN }
role_hierarchy: #role_hierarchy:
ROLE_ADMIN: ROLE_USER # ROLE_ADMIN: ROLE_USER
when@test: when@test:
security: security:

@ -1,6 +1,6 @@
when@dev: when@dev:
web_profiler: web_profiler:
toolbar: false toolbar: true
intercept_redirects: false intercept_redirects: false
framework: framework:
@ -10,7 +10,7 @@ when@dev:
when@test: when@test:
web_profiler: web_profiler:
toolbar: false toolbar: true
intercept_redirects: false intercept_redirects: false
framework: framework:

@ -8,6 +8,7 @@ parameters:
env(MONGODB_DB): '' env(MONGODB_DB): ''
services: services:
Symfony\Component\HttpKernel\Profiler\Profiler: '@profiler'
# default configuration for services in *this* file # default configuration for services in *this* file
_defaults: _defaults:
autowire: true # Automatically injects dependencies in your services. autowire: true # Automatically injects dependencies in your services.

@ -8,6 +8,7 @@ use App\Form\Type\LoginType;
use Doctrine\ODM\MongoDB\DocumentManager; use Doctrine\ODM\MongoDB\DocumentManager;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController; use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\Security\Http\Authentication\AuthenticationUtils; use Symfony\Component\Security\Http\Authentication\AuthenticationUtils;
use Symfony\Component\PasswordHasher\Hasher\UserPasswordHasherInterface;
use Symfony\Component\HttpFoundation\Response; use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpFoundation\Request; use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Redirect; use Symfony\Component\HttpFoundation\Redirect;
@ -19,7 +20,7 @@ class UserController extends AbstractController
#[Route('/create', name: 'create', defaults: ['id' => null])] #[Route('/create', name: 'create', defaults: ['id' => null])]
#[Route('/{id}/edit', name: 'edit')] #[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); $user = $dm->getRepository(User::class)->find($id);
if ($user == null) if ($user == null)
@ -31,6 +32,9 @@ class UserController extends AbstractController
if ($form->isSubmitted() && $form->isValid()) { if ($form->isSubmitted() && $form->isValid()) {
$user = $form->getData(); $user = $form->getData();
$hashedPassword = $passwordHasher->hashPassword($user, $user->getPassword());
$user->setPassword($hashedPassword);
$dm->persist($user); $dm->persist($user);
$dm->flush(); $dm->flush();
@ -46,7 +50,7 @@ class UserController extends AbstractController
public function login(AuthenticationUtils $authenticationUtils): Response public function login(AuthenticationUtils $authenticationUtils): Response
{ {
return $this->render('login.html.twig', [ return $this->render('login.html.twig', [
'last_username' => $authenticationUtils->getLastUsername(), 'last_email' => $authenticationUtils->getLastUsername(),
'error' => $authenticationUtils->getLastAuthenticationError(), 'error' => $authenticationUtils->getLastAuthenticationError(),
]); ]);
} }

@ -17,8 +17,8 @@ class Chart
#[MongoDB\Id] #[MongoDB\Id]
protected string $id; protected string $id;
#[MongoDB\ReferenceOne(targetDocument: User::class, inversedBy: "charts")] //#[MongoDB\ReferenceOne(targetDocument: User::class, inversedBy: "charts")]
protected $user; //protected $user;
#[MongoDB\Field(type: 'string')] #[MongoDB\Field(type: 'string')]
#[Assert\NotBlank] #[Assert\NotBlank]
@ -46,20 +46,20 @@ class Chart
* *
* @return User * @return User
*/ */
public function getUser(): ?User //public function getUser(): ?User
{ //{
return $this->user; /// return $this->user;
} //}
/** /**
* Set the user associated with the chart. * Set the user associated with the chart.
* *
* @param User $user The user to set * @param User $user The user to set
*/ */
public function setUser(?User $user): void //public function setUser(?User $user): void
{ //{
$this->user = $user; // $this->user = $user;
} //}
public function getName(): ?string public function getName(): ?string
{ {

@ -23,34 +23,35 @@ class User implements UserInterface, PasswordAuthenticatedUserInterface
#[MongoDB\Field(type: 'string')] #[MongoDB\Field(type: 'string')]
#[Assert\NotBlank] #[Assert\NotBlank]
#[Assert\Email] #[Assert\Email]
protected ?string $email = null; protected string $email;
#[MongoDB\Field(type: 'string')] #[MongoDB\Field(type: 'string')]
#[Assert\NotBlank] #[Assert\NotBlank]
protected ?string $password = null; private string $password;
#[MongoDB\Field(type: 'collection')] #[MongoDB\Field(type: 'collection')]
private array $roles = ['ROLE_USER']; private array $roles = ['ROLE_USER'];
#[MongoDB\ReferenceMany(targetDocument: Chart::class, mappedBy: "user")] //#[MongoDB\ReferenceMany(targetDocument: Chart::class, mappedBy: "user")]
protected $charts; //protected $charts;
public function getId(): string public function getId(): string
{ {
return $this->id; return $this->id;
} }
public function getEmail(): ?string public function getEmail(): string
{ {
return $this->email; return $this->email;
} }
public function setEmail(?string $email): void public function setEmail(string $email): self
{ {
$this->email = $email; $this->email = $email;
return $this;
} }
public function getPassword(): ?string public function getPassword(): string
{ {
return $this->password; return $this->password;
} }
@ -65,26 +66,26 @@ class User implements UserInterface, PasswordAuthenticatedUserInterface
/** /**
* @return Collection<int, Chart> * @return Collection<int, Chart>
*/ */
public function getCharts(): Collection //public function getCharts(): Collection
{ //{
return $this->charts; // return $this->charts;
} //}
/** /**
* Adds a chart to the user. * Adds a chart to the user.
* *
* @param Chart $chart The chart to add * @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 // 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 // Add the chart to the collection
$this->charts->add($chart); // $this->charts->add($chart);
// Set the user reference in the chart entity // Set the user reference in the chart entity
$chart->setUser($this); // $chart->setUser($this);
} // }
} //}
public function getRoles(): array public function getRoles(): array
{ {
@ -111,7 +112,7 @@ class User implements UserInterface, PasswordAuthenticatedUserInterface
public function getUserIdentifier(): string public function getUserIdentifier(): string
{ {
return $this->email; return (string) $this->email;
} }

@ -8,25 +8,22 @@
{{ parent() }} {{ parent() }}
<main> <main>
<div class="loginDiv"> <div class="loginDiv">
{% block body %} {% if error %}
{% if error %} <div>{{ error.messageKey|trans(error.messageData, 'security') }}</div>
<div>{{ error.messageKey|trans(error.messageData, 'security') }}</div> {% endif %}
{% endif %}
<form action="{{ path('users_login') }}" method="post"> <form action="{{ path('users_login') }}" method="post">
<label for="username">Email:</label> <label for="email">Email:</label>
<input type="text" id="username" name="_username" value="{{ last_username }}"> <input type="text" id="email" name="_username" value="{{ last_email }}">
<label for="password">Heslo:</label> <label for="password">Heslo:</label>
<input <input type="password" id="password" name="_password">
type="password" id="password" name="_password">
{# 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
<input type="hidden" name="_target_path" value="/account"> #} <input type="hidden" name="_target_path" value="/account"> #}
<button type="submit">Přihlásit se</button> <button type="submit">Přihlásit se</button>
</form> </form>
{% endblock %}
</div> </div>
</main> </main>
{% endblock %} {% endblock %}

Loading…
Cancel
Save

Powered by TurnKey Linux.