refs #375. Corregidos errores con Uuid

pull/5/head
Manuel Aranda Rosales 2024-05-24 13:28:55 +02:00
parent 68ea45a1f9
commit 1350735e5c
4 changed files with 20 additions and 5 deletions

View File

@ -5,6 +5,8 @@ api_platform:
path_segment_name_generator: 'api_platform.path_segment_name_generator.dash' path_segment_name_generator: 'api_platform.path_segment_name_generator.dash'
formats: formats:
jsonld: ['application/ld+json', 'application/json'] jsonld: ['application/ld+json', 'application/json']
patch_formats:
jsonld: ['application/ld+json', 'application/json']
mapping: mapping:
paths: ['%kernel.project_dir%/config/api_platform', '%kernel.project_dir%/src/Dto'] paths: ['%kernel.project_dir%/config/api_platform', '%kernel.project_dir%/src/Dto']
defaults: defaults:

View File

@ -6,11 +6,13 @@ use App\Repository\UserRepository;
use Doctrine\Common\Collections\ArrayCollection; use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection; use Doctrine\Common\Collections\Collection;
use Doctrine\ORM\Mapping as ORM; use Doctrine\ORM\Mapping as ORM;
use Symfony\Bridge\Doctrine\Validator\Constraints\UniqueEntity;
use Symfony\Component\Security\Core\User\PasswordAuthenticatedUserInterface; use Symfony\Component\Security\Core\User\PasswordAuthenticatedUserInterface;
use Symfony\Component\Security\Core\User\UserInterface; use Symfony\Component\Security\Core\User\UserInterface;
#[ORM\Entity(repositoryClass: UserRepository::class)] #[ORM\Entity(repositoryClass: UserRepository::class)]
#[ORM\UniqueConstraint(name: 'UNIQ_IDENTIFIER_USERNAME', fields: ['username'])] #[ORM\UniqueConstraint(name: 'UNIQ_IDENTIFIER_USERNAME', fields: ['username'])]
#[UniqueEntity(fields: ['username'], message: 'There is already an account with this username')]
class User extends AbstractEntity implements UserInterface, PasswordAuthenticatedUserInterface class User extends AbstractEntity implements UserInterface, PasswordAuthenticatedUserInterface
{ {
use ToggleableTrait; use ToggleableTrait;

View File

@ -12,16 +12,21 @@ use ApiPlatform\Validator\ValidatorInterface;
use App\Dto\Input\UserInput; use App\Dto\Input\UserInput;
use App\Dto\Output\UserOutput; use App\Dto\Output\UserOutput;
use App\Repository\UserRepository; use App\Repository\UserRepository;
use Symfony\Component\PasswordHasher\Hasher\UserPasswordHasherInterface;
class UserProcessor implements ProcessorInterface class UserProcessor implements ProcessorInterface
{ {
public function __construct( public function __construct(
private UserRepository $userRepository, private readonly UserRepository $userRepository,
private ValidatorInterface $validator private readonly ValidatorInterface $validator,
private readonly UserPasswordHasherInterface $userPasswordHasher
) )
{ {
} }
/**
* @throws \Exception
*/
public function process(mixed $data, Operation $operation, array $uriVariables = [], array $context = []) public function process(mixed $data, Operation $operation, array $uriVariables = [], array $context = [])
{ {
switch ($operation){ switch ($operation){
@ -34,6 +39,9 @@ class UserProcessor implements ProcessorInterface
} }
} }
/**
* @throws \Exception
*/
private function processCreateOrUpdate($data, Operation $operation, array $uriVariables = [], array $context = []): UserOutput private function processCreateOrUpdate($data, Operation $operation, array $uriVariables = [], array $context = []): UserOutput
{ {
if (!($data instanceof UserInput)) { if (!($data instanceof UserInput)) {
@ -42,10 +50,13 @@ class UserProcessor implements ProcessorInterface
$entity = null; $entity = null;
if (isset($uriVariables['uuid'])) { if (isset($uriVariables['uuid'])) {
$entity = $this->userRepository->findOneByUuid($uriVariables['id']); $entity = $this->userRepository->findOneByUuid($uriVariables['uuid']);
} }
$user = $data->createOrUpdateEntity($entity); $user = $data->createOrUpdateEntity($entity);
$user->setPassword($this->userPasswordHasher->hashPassword($user, $data->password));
$this->validator->validate($user); $this->validator->validate($user);
$this->userRepository->save($user); $this->userRepository->save($user);
@ -54,7 +65,7 @@ class UserProcessor implements ProcessorInterface
private function processDelete($data, Operation $operation, array $uriVariables = [], array $context = []): null private function processDelete($data, Operation $operation, array $uriVariables = [], array $context = []): null
{ {
$user = $this->userRepository->findOneByUuid($uriVariables['id']); $user = $this->userRepository->findOneByUuid($uriVariables['uuid']);
$this->userRepository->delete($user); $this->userRepository->delete($user);
return null; return null;

View File

@ -63,7 +63,7 @@ class UserProvider implements ProviderInterface
if (isset($uriVariables['uuid'])) { if (isset($uriVariables['uuid'])) {
$item = $this->itemProvider->provide($operation, $uriVariables, $context); $item = $this->itemProvider->provide($operation, $uriVariables, $context);
return !$item ? new UserInput($item) : null; return $item !== null ? new UserInput($item) : null;
} }
return new UserInput(); return new UserInput();