I'm working with Api-platform and I tried to inject my user authenticated in object, for a post request.
My problem is, when send my user in object:
obj->setUser($this->security->getUser());
I need a password and I have a error:
An exception occurred while executing a query: SQLSTATE[23000]: Integrity constraint violation: 1048 Le champ 'password' ne peut ĂȘtre vide (null)
I authenticated my user with a JWT Token and I create my user in createFromPayload()
(JWTUserInterface)
public static function createFromPayload($username, array $payload)
{
$user = new User();
$user->setId($username)->setEmail($payload['username'] ?? '');
$user->setRoles($payload['roles']);
return $user;
}
I debug my User object and it's logic my password is null, but how assign my user in my object if password is null?
^ App\Entity\User {#13193
-id: 8
-email: "test@test.fr"
-roles: array:1 [
0 => "ROLE_USER"
]
-password: null
-wallets: Doctrine\Common\Collections\ArrayCollection {#13417
-elements: []
}
}
My class : UserOwnedDenormalizer
class UserOwnedDenormalizer implements ContextAwareDenormalizerInterface, DenormalizerAwareInterface
{
use DenormalizerAwareTrait;
private const ALREADY_CALLED_DENORMALIZE = 'UserOwnedDenormalizer';
public function __construct(private UserRepository $userRepository, private Security $security)
{
}
public function supportsDenormalization($data, string $type, ?string $format = null, array $context = [])
{
$reflectionClass = new \ReflectionClass($type);
$alreadyCalled = $context[self::ALREADY_CALLED_DENORMALIZE] ?? false;
return $reflectionClass->implementsInterface(UserOwnedInterface::class) && $alreadyCalled === false;
}
public function denormalize($data, string $type, ?string $format = null, array $context = [])
{
$context[self::ALREADY_CALLED_DENORMALIZE] = true;
/** @var UserOwnedInterface $obj */
$obj = $this->denormalizer->denormalize($data, $type, $format, $context);
$obj->setUser($this->security->getUser());
return $obj;
}
}
My objective is to assign to user in object without passing by Doctrine and my Database.