<?php
namespace App\Entity;
use ApiPlatform\Core\Annotation\ApiResource;
use ApiPlatform\Core\Annotation\ApiSubresource;
use ApiPlatform\Core\Annotation\ApiProperty;
use App\Repository\UserRepository;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
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\UserInterface;
use Symfony\Component\Validator\Constraints as Assert;
use Symfony\Component\Serializer\Annotation\Groups;
use Symfony\Component\Uid\Ulid;
use Symfony\Component\Serializer\Annotation\MaxDepth;
#[ORM\Entity(repositoryClass: UserRepository::class)]
#[UniqueEntity(fields: "email", message: "Un compte existe déjà avec cette boîte mail." )]
#[ApiResource(
normalizationContext: ['groups' => ['user_read'], 'enable_max_depth' => true ],
denormalizationContext: ['groups' => ['user_write']],
collectionOperations: [
"get" => ["security" => "is_granted('ROLE_ADMIN')"],
"post" => ["security" => "is_granted('ROLE_ADMIN')"]
],
itemOperations: [
"get" => ["security" => "is_granted('ROLE_ADMIN') or object.id == user.id"],
"patch" => ["security" => "is_granted('ROLE_ADMIN') or object.id == user.id"],
],
attributes: ["pagination_client_items_per_page" => true],
forceEager: false
)]
class User implements UserInterface, PasswordAuthenticatedUserInterface
{
#[ORM\Id]
#[ORM\GeneratedValue]
#[ORM\Column(type: 'integer')]
#[ApiProperty(identifier: false)]
#[Groups(['user_read'])]
public $id;
#[ORM\GeneratedValue]
#[ORM\Column(type: 'string', length: 26, unique: true)]
#[ApiProperty(identifier: true)]
#[Groups(['user_read'])]
public $ulid = "";
#[Assert\Email()]
#[ORM\Column(type: 'string', length: 255, unique: true)]
#[Groups(['user_read','user_write'])]
private $email;
#[Groups(['user_read','user_write'])]
#[ORM\Column(type: 'string', length: 255, nullable: true)]
private $firstname;
#[Groups(['user_read','user_write'])]
#[ORM\Column(type: 'string', length: 255, nullable: true)]
private $lastname;
#[ORM\Column(type: 'string', nullable: true)]
#[Groups(['user_write'])]
private $password;
#[ORM\Column(type: 'json')]
#[Groups(['user_read','user_write'])]
private $roles = [];
#[ORM\Column(type: 'string', length: 255, nullable: true)]
#[Groups(['user_read','user_write'])]
private $phoneNumber;
#[ORM\ManyToOne(targetEntity: Role::class, inversedBy: 'users')]
#[ORM\JoinColumn(nullable: true)]
#[Groups(['user_read','user_write'])]
private $role;
#[ORM\ManyToMany(targetEntity: Group::class, inversedBy: 'users')]
#[Groups(['user_read'])]
#[ApiSubresource()]
private $groups;
#[ORM\OneToMany(mappedBy: 'creator', targetEntity: Accomodation::class, orphanRemoval: true)]
#[Groups(['user_read'])]
#[ApiSubresource()]
private $accomodation;
public function __construct()
{
$this->groups = new ArrayCollection();
$this->accomodation = new ArrayCollection();
}
public function __toString(): string
{
return $this->email;
}
public function getId(): ?int
{
return $this->id;
}
public function getEmail(): ?string
{
return $this->email;
}
public function setEmail(string $email): self
{
$this->email = $email;
return $this;
}
/**
* A visual identifier that represents this user.
*
* @see UserInterface
*/
public function getUserIdentifier(): string
{
return (string) $this->email;
}
/**
* @deprecated since Symfony 5.3, use getUserIdentifier instead
*/
public function getUsername(): string
{
return (string) $this->email;
}
/**
* @see UserInterface
*/
public function getRoles(): array
{
$roles = $this->roles;
// guarantee every user at least has ROLE_USER
$roles[] = 'ROLE_USER';
return array_unique($roles);
}
public function setRoles(array $roles): self
{
$this->roles = $roles;
return $this;
}
/**
* @see PasswordAuthenticatedUserInterface
*/
public function getPassword(): string
{
return $this->password;
}
public function setPassword(string $password): self
{
$this->password = $password;
return $this;
}
public function getUlid()
{
return $this->ulid;
}
public function setUlid(): self
{
$ulid = new Ulid();
$this->ulid = $ulid;
return $this;
}
public function getFirstname(): ?string
{
return $this->firstname;
}
public function setFirstname(?string $firstname): self
{
$this->firstname = $firstname;
return $this;
}
public function getLastname(): ?string
{
return $this->lastname;
}
public function setLastname(string $lastname): self
{
$this->lastname = $lastname;
return $this;
}
/**
* Returning a salt is only needed, if you are not using a modern
* hashing algorithm (e.g. bcrypt or sodium) in your security.yaml.
*
* @see UserInterface
*/
public function getSalt(): ?string
{
return null;
}
/**
* @see UserInterface
*/
public function eraseCredentials()
{
// If you store any temporary, sensitive data on the user, clear it here
// $this->plainPassword = null;
}
/**
* @return Collection|Qrcode[]
*/
public function getQrcodes(): Collection
{
return $this->qrcodes;
}
public function getIsVerified(): bool
{
return $this->isVerified;
}
public function setIsVerified(bool $isVerified): self
{
$this->isVerified = $isVerified;
return $this;
}
public function getPhoneNumber(): ?string
{
return $this->phoneNumber;
}
public function setPhoneNumber(string $phoneNumber): self
{
$this->phoneNumber = $phoneNumber;
return $this;
}
public function getRole(): ?Role
{
return $this->role;
}
public function setRole(?Role $role): self
{
$this->role = $role;
return $this;
}
/**
* @return Collection<int, Group>
*/
public function getGroups(): Collection
{
return $this->groups;
}
public function addGroup(Group $group): self
{
if (!$this->groups->contains($group)) {
$this->groups[] = $group;
}
return $this;
}
public function removeGroup(Group $group): self
{
$this->groups->removeElement($group);
return $this;
}
/**
* @return Collection<int, Accomodation>
*/
public function getAccomodation(): Collection
{
return $this->accomodation;
}
public function addAccomodation(Accomodation $accomodation): self
{
if (!$this->accomodation->contains($accomodation)) {
$this->accomodation[] = $accomodation;
$accomodation->setCreator($this);
}
return $this;
}
public function removeAccomodation(Accomodation $accomodation): self
{
if ($this->accomodation->removeElement($accomodation)) {
// set the owning side to null (unless already changed)
if ($accomodation->getCreator() === $this) {
$accomodation->setCreator(null);
}
}
return $this;
}
}