feature: add Authenticator

This commit is contained in:
Jan Klattenhoff 2024-01-18 12:01:43 +01:00
parent b102519154
commit 2380c13c37
15 changed files with 1370 additions and 17 deletions

View file

@ -3,32 +3,86 @@
namespace App\Controller;
use App\Entity\Printer;
use App\Enum\ErrorMessages;
use App\Repository\PrinterRepository;
use App\Service\PrinterService;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\Routing\Annotation\Route;
class PrinterCrudController extends AbstractController
{
public function __construct(private readonly PrinterRepository $printerRepository)
public function __construct(
private readonly PrinterRepository $printerRepository,
private readonly PrinterService $printerService,
)
{
}
#[Route('/printer', name: 'printer')]
public function printer(): JsonResponse
#[Route('/printer', name: 'printer', methods: ['GET'])]
public function getAllPrinters(): JsonResponse
{
return $this->json($this->printerRepository->findAll());
}
#[Route('/printer/{id}', name: 'app_printer', methods: ["GET"])]
public function index(?Printer $printer): JsonResponse
#[Route('/printer/{id}', name: 'get_printer', methods: ['GET'])]
public function getPrinter(?Printer $printer): JsonResponse
{
if (!$printer) {
return $this->json([
'message' => 'printer does not exist',
'message' => ErrorMessages::DOESNT_EXIST->value,
]);
}
return $this->json($printer);
}
#[Route('/printer/{id}', name: 'delete_printer', methods: ['DELETE'])]
public function deletePrinter(?Printer $printer): JsonResponse
{
if (!$printer) {
return $this->json([
'message' => ErrorMessages::DOESNT_EXIST->value,
]);
}
$this->printerService->deletePrinter($printer);
return $this->json([
'message' => 'printer was deleted',
]);
}
#[Route('/printer', name: 'create_printer', methods: ['POST'])]
public function createPrinter(Request $request): JsonResponse
{
$jsonContent = $request->getContent();
if (!$this->printerService->validateJson($jsonContent)) {
return $this->json([
'message' => ErrorMessages::DATA_INCOMPLETE->value,
]);
}
$printer = $this->printerService->createPrinter($jsonContent);
return $this->json($printer);
}
#[Route('/printer/{id}', name: 'edit_printer', methods: ['PUT'])]
public function editPrinter(?Printer $printer, Request $request): JsonResponse
{
if (!$printer) {
return $this->json([
'message' => ErrorMessages::DOESNT_EXIST->value,
]);
}
if (!$this->printerService->validateJson($request->getContent())) {
return $this->json([
'message' => ErrorMessages::DATA_INCOMPLETE,
]);
}
}
}

View file

@ -0,0 +1,9 @@
<?php
namespace App\Enum;
enum ErrorMessages: string
{
case DATA_INCOMPLETE = 'The provided data is incomplete';
case DOESNT_EXIST = 'printer does not exist';
}

View file

@ -0,0 +1,44 @@
<?php
namespace App\Security;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
use Symfony\Component\Security\Core\Exception\AuthenticationException;
use Symfony\Component\Security\Http\Authenticator\AbstractAuthenticator;
use Symfony\Component\Security\Http\Authenticator\Passport\Passport;
class PrinterAuthenticator extends AbstractAuthenticator
{
public function supports(Request $request): ?bool
{
// TODO: Implement supports() method.
}
public function authenticate(Request $request): Passport
{
// TODO: Implement authenticate() method.
}
public function onAuthenticationSuccess(Request $request, TokenInterface $token, string $firewallName): ?Response
{
// TODO: Implement onAuthenticationSuccess() method.
}
public function onAuthenticationFailure(Request $request, AuthenticationException $exception): ?Response
{
// TODO: Implement onAuthenticationFailure() method.
}
// public function start(Request $request, AuthenticationException $authException = null): Response
// {
// /*
// * If you would like this class to control what happens when an anonymous user accesses a
// * protected page (e.g. redirect to /login), uncomment this method and make this class
// * implement Symfony\Component\Security\Http\EntryPoint\AuthenticationEntryPointInterface.
// *
// * For more details, see https://symfony.com/doc/current/security/experimental_authenticators.html#configuring-the-authentication-entry-point
// */
// }
}

View file

@ -1,20 +1,35 @@
<?php
<?php declare(strict_types=1);
namespace App\Service;
use App\Entity\Printer;
use Doctrine\ORM\EntityManagerInterface;
use Doctrine\ORM\Mapping\Entity;
use Symfony\Component\Serializer\SerializerInterface;
class PrinterService
{
public function __construct(private readonly EntityManagerInterface $entityManager)
public function __construct(private readonly EntityManagerInterface $entityManager, private readonly SerializerInterface $serializer)
{
}
public function deletePrinter(Printer $printer)
public function deletePrinter(Printer $printer): void
{
$this->entityManager->remove($printer);
$this->entityManager->flush();
}
public function createPrinter(string $jsonString): Printer
{
$printer = $this->serializer->deserialize($jsonString, Printer::class, 'json');
$this->entityManager->persist($printer);
$this->entityManager->flush();
return $printer;
}
public function validateJson(string $jsonString): bool
{
$array = json_decode($jsonString, true);
return isset($array['name'], $array['price'], $array['max_speed'], $array['build_volume']);
}
}