Auth working

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

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

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

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

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

@ -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(),
]);
}

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

@ -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<int, Chart>
*/
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;
}

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

Loading…
Cancel
Save

Powered by TurnKey Linux.