Authentication

main
František Špaček 2 years ago
parent 8ea1084eb4
commit a9b0121e0f

@ -22,8 +22,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 }
# - { path: ^/profile, roles: ROLE_USER } role_hierarchy:
ROLE_ADMIN: ROLE_USER
when@test: when@test:
security: security:

@ -1,7 +1,7 @@
/** /**
* Donut Chart * Donut Chart
*/ */
class DonutChart extends Chart { class DonutChart extends PieChart {
/** /**
* Creates a new instance of the DonutChart class * Creates a new instance of the DonutChart class
* *

@ -1,7 +1,7 @@
/** /**
* Stacked Chart * Stacked Chart
*/ */
class StackedChart extends Chart { class StackedChart extends BarChart {
/** /**
* Creates a new instance of the StackedChart class * Creates a new instance of the StackedChart class
* *

@ -6,27 +6,24 @@ use App\Document\User;
use App\Form\Type\UserType; use App\Form\Type\UserType;
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\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;
use Symfony\Component\Routing\Annotation\Route; use Symfony\Component\Routing\Annotation\Route;
#[Route(path: '/users', name: 'users_')]
class UserController extends AbstractController class UserController extends AbstractController
{ {
#[Route('/register', name: 'register')]
public function registerAction(): Response
{
$form = $this->createForm(UserType::class, new User());
return $this->render('register.html.twig', [ #[Route('/create', name: 'create', defaults: ['id' => null])]
'form' => $form->createView() #[Route('/{id}/edit', name: 'edit', requirements: ['id' => '\d+'])]
]); public function editAction(DocumentManager $dm, Request $request, User $user)
}
#[Route('/create', name: 'create')]
public function createAction(DocumentManager $dm, Request $request)
{ {
$form = $this->createForm(UserType::class, new User()); if ($user == null)
$user = new User();
$form = $this->createForm(UserType::class, $user);
$form->handleRequest($request); $form->handleRequest($request);
@ -43,4 +40,27 @@ class UserController extends AbstractController
'form' => $form->createView() 'form' => $form->createView()
]); ]);
} }
#[Route('/login', name: 'login')]
public function login(AuthenticationUtils $authenticationUtils): Response
{
return $this->render('auth/login.html.twig', [
'last_username' => $authenticationUtils->getLastUsername(),
'error' => $authenticationUtils->getLastAuthenticationError(),
]);
}
/*#[Route('/{id}', name: 'detail', requirements: ['id' => '\d+'])]
public function detail(DocumentManager $dm, Request $request, User $user): Response
{
if ($user === null)
throw $this->createNotFoundException('Uživatel nenalezen!');
//$this->denyAccessUnlessGranted(UserVoter::VIEW, $user);
return $this->render('users/detail.html.twig', [
'user' => $user,
]);
}*/
} }

@ -2,6 +2,7 @@
namespace App\Document; namespace App\Document;
use App\Document\User;
use Doctrine\Bundle\MongoDBBundle\Validator\Constraints\Unique as MongoDBUnique; use Doctrine\Bundle\MongoDBBundle\Validator\Constraints\Unique as MongoDBUnique;
use Doctrine\ODM\MongoDB\Mapping\Annotations as MongoDB; use Doctrine\ODM\MongoDB\Mapping\Annotations as MongoDB;
use Symfony\Component\Validator\Constraints as Assert; use Symfony\Component\Validator\Constraints as Assert;
@ -16,6 +17,9 @@ class Chart
#[MongoDB\Id] #[MongoDB\Id]
protected string $id; protected string $id;
#[MongoDB\ReferenceOne(targetDocument: User::class, inversedBy: "charts")]
protected $user;
#[MongoDB\Field(type: 'string')] #[MongoDB\Field(type: 'string')]
#[Assert\NotBlank] #[Assert\NotBlank]
protected ?string $name = null; protected ?string $name = null;
@ -37,6 +41,26 @@ class Chart
return $this->id; return $this->id;
} }
/**
* Get the user associated with the chart.
*
* @return 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 getName(): ?string public function getName(): ?string
{ {
return $this->name; return $this->name;

@ -2,13 +2,17 @@
namespace App\Document; namespace App\Document;
use App\Document\Chart;
use Doctrine\Common\Collections\Collection;
use Doctrine\Bundle\MongoDBBundle\Validator\Constraints\Unique as MongoDBUnique; use Doctrine\Bundle\MongoDBBundle\Validator\Constraints\Unique as MongoDBUnique;
use Symfony\Component\Security\Core\User\PasswordAuthenticatedUserInterface;
use Symfony\Component\Security\Core\User\UserInterface;
use Doctrine\ODM\MongoDB\Mapping\Annotations as MongoDB; use Doctrine\ODM\MongoDB\Mapping\Annotations as MongoDB;
use Symfony\Component\Validator\Constraints as Assert; use Symfony\Component\Validator\Constraints as Assert;
#[MongoDB\Document(collection: 'users')] #[MongoDB\Document(collection: 'users')]
#[MongoDB\Unique(fields: 'email')] #[MongoDB\Unique(fields: 'email')]
class User class User implements UserInterface, PasswordAuthenticatedUserInterface
{ {
/** /**
* @MongoDB\Id * @MongoDB\Id
@ -25,6 +29,12 @@ class User
#[Assert\NotBlank] #[Assert\NotBlank]
protected ?string $password = null; protected ?string $password = null;
#[MongoDB\Field(type: 'collection')]
private array $roles = ['ROLE_USER'];
#[MongoDB\ReferenceMany(targetDocument: Chart::class, mappedBy: "user")]
protected $charts;
public function getId(): string public function getId(): string
{ {
return $this->id; return $this->id;
@ -45,9 +55,64 @@ class User
return $this->password; return $this->password;
} }
// stupid simple encryption (please don't copy it!) public function setPassword(string $password): static
public function setPassword(?string $password): void {
$this->password = $password;
return $this;
}
/**
* @return Collection<int, Chart>
*/
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
{
// Check if the chart already exists in the collection
if (!$this->charts->contains($chart)) {
// Add the chart to the collection
$this->charts->add($chart);
// Set the user reference in the chart entity
$chart->setUser($this);
}
}
public function getRoles(): array
{ {
$this->password = sha1($password); $roles = $this->roles;
// guarantee every user at least has ROLE_USER
$roles[] = 'ROLE_USER';
return array_unique($roles);
}
public function setRoles(array $roles): self
{
$this->roles = $roles;
return $this;
}
public function eraseCredentials(): void
{
// $this->password = null;
}
public function getUserIdentifier(): string
{
return $this->email;
} }
} }

@ -8,7 +8,11 @@
{{ parent() }} {{ parent() }}
<main> <main>
<div class="loginDiv"> <div class="loginDiv">
{{ form_start(form, {'action': path('create'), 'method': 'POST'}) }}
{{ form_widget(form) }}
<input type="submit"/>
{{ form_end(form) }}
</div> </div>
</main> </main>
{% endblock %} {% endblock %}
Loading…
Cancel
Save

Powered by TurnKey Linux.