Last active
November 27, 2022 17:32
-
-
Save nikrou/73df58d0dd9a6ff9ad53767a64d82fa8 to your computer and use it in GitHub Desktop.
User and UserDataPersister
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace App\Entity; | |
use ApiPlatform\Core\Annotation\ApiResource; | |
use App\Repository\UserRepository; | |
use Doctrine\Common\Collections\ArrayCollection; | |
use Doctrine\Common\Collections\Collection; | |
use Doctrine\ORM\Mapping as ORM; | |
use Symfony\Component\Security\Core\User\EquatableInterface; | |
use Symfony\Component\Security\Core\User\PasswordAuthenticatedUserInterface; | |
use Symfony\Component\Security\Core\User\UserInterface; | |
use Symfony\Component\Serializer\Annotation\Groups; | |
use Symfony\Component\Serializer\Annotation\SerializedName; | |
#[ORM\Table(name: 'users')] | |
#[ORM\Entity(repositoryClass: UserRepository::class)] | |
#[ApiResource( | |
normalizationContext: ['groups' => ['read:User:collection', 'read:User:item']], | |
denormalizationContext: ['groups' => ['write:User:collection', 'write:User:item']], | |
paginationEnabled: true, | |
)] | |
class User implements UserInterface, PasswordAuthenticatedUserInterface, EquatableInterface, \Serializable | |
{ | |
#[ORM\Id] | |
#[ORM\GeneratedValue] | |
#[ORM\Column(type: 'integer')] | |
#[Groups(['read:User:collection', 'read:User:item'])] | |
private int $id; | |
#[ORM\Column(type: 'string', unique: true, length: 100)] | |
#[Groups(['read:User:collection', 'read:User:item', 'write:User:collection', 'write:User:item'])] | |
private string $username; | |
#[ORM\Column(type: 'string', length: 255, nullable: true)] | |
private ?string $password = null; | |
#[SerializedName('password')] | |
#[Groups(['write:User:collection', 'write:User:item'])] | |
private ?string $plain_password = null; | |
// setter and getter for other fields | |
public function setPassword(?string $password): self | |
{ | |
$this->password = $password; | |
return $this; | |
} | |
public function getPlainPassword(): ?string | |
{ | |
return $this->plain_password; | |
} | |
public function setPlainPassword(?string $plain_password): self | |
{ | |
$this->plain_password = $plain_password; | |
return $this; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace App\DataPersister; | |
use ApiPlatform\Core\DataPersister\DataPersisterInterface; | |
use App\Entity\User; | |
use Doctrine\ORM\EntityManagerInterface; | |
use Symfony\Component\PasswordHasher\Hasher\UserPasswordHasherInterface; | |
class UserDataPersister implements DataPersisterInterface | |
{ | |
public function __construct(private EntityManagerInterface $entityManager, private UserPasswordHasherInterface $passwordHasher) | |
{ | |
} | |
public function supports($data): bool | |
{ | |
return $data instanceof User; | |
} | |
/** | |
* @param User $data | |
*/ | |
public function persist($data): object | |
{ | |
if ($data->getPlainPassword()) { | |
$data->setPassword($this->passwordHasher->hashPassword($data, $data->getPlainPassword())); | |
$data->eraseCredentials(); | |
} | |
$this->entityManager->persist($data); | |
$this->entityManager->flush(); | |
return $data; | |
} | |
public function remove($data) | |
{ | |
$this->entityManager->remove($data); | |
$this->entityManager->flush(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment