Some checks failed
Auto Tag Develop / tag (push) Has been cancelled
| Numéro du ticket | Titre du ticket | |------------------|-----------------| | #325 | Corrections diverses | ## Description de la PR ## Modification du .env ## Check list - [x] Pas de régression - [ ] TU/TI/TF rédigée - [x] TU/TI/TF OK - [x] CHANGELOG modifié Reviewed-on: #26 Reviewed-by: Autin <tristan@yuno.malio.fr> Co-authored-by: kevin <kevin@yuno.malio.fr> Co-committed-by: kevin <kevin@yuno.malio.fr>
45 lines
1.4 KiB
PHP
45 lines
1.4 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace App\State;
|
|
|
|
use ApiPlatform\Metadata\Operation;
|
|
use ApiPlatform\State\ProcessorInterface;
|
|
use App\Entity\User;
|
|
use Symfony\Component\DependencyInjection\Attribute\Autowire;
|
|
use Symfony\Component\PasswordHasher\Hasher\UserPasswordHasherInterface;
|
|
|
|
final class UserPasswordProcessor implements ProcessorInterface
|
|
{
|
|
public function __construct(
|
|
private readonly UserPasswordHasherInterface $hasher,
|
|
#[Autowire(service: 'api_platform.doctrine.orm.state.persist_processor')]
|
|
private readonly ProcessorInterface $persistProcessor
|
|
) {}
|
|
|
|
public function process(mixed $data, Operation $operation, array $uriVariables = [], array $context = []): mixed
|
|
{
|
|
if ($data instanceof User) {
|
|
$plain = $data->getPassword();
|
|
$previous = $context['previous_data'] ?? null;
|
|
if ($previous instanceof User && $plain === $previous->getPassword()) {
|
|
// Password not changed in payload: keep existing hash.
|
|
$data->setPassword($previous->getPassword());
|
|
} elseif ('' !== $plain) {
|
|
$data->setPassword($this->hasher->hashPassword(
|
|
$data,
|
|
$plain
|
|
));
|
|
}
|
|
}
|
|
|
|
return $this->persistProcessor->process(
|
|
$data,
|
|
$operation,
|
|
$uriVariables,
|
|
$context
|
|
);
|
|
}
|
|
}
|