<?php
namespace App\Entity;
use ApiPlatform\Core\Annotation\ApiProperty;
use ApiPlatform\Core\Annotation\ApiResource;
use ApiPlatform\Core\Annotation\ApiFilter;
use ApiPlatform\Core\Bridge\Doctrine\Orm\Filter\SearchFilter;
use ApiPlatform\Core\Bridge\Doctrine\Orm\Filter\OrderFilter;
use App\Repository\UserRepository;
use App\Trait\TimestampableEntity;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
use Doctrine\ORM\Mapping as ORM;
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\Serializer\Annotation\MaxDepth;
#[ORM\Entity(repositoryClass: UserRepository::class)]
#[ApiResource(
iri: 'User',
itemOperations: [
'get' => [
// 'security' => "is_granted('ROLE_USER')",
'normalization_context' => [
'groups' => 'user:item:get',
'enable_max_depth' => true
]
],
'put' => [
// 'security' => "is_granted('ROLE_USER')",
'normalization_context' => [
'groups' => 'user:item:put',
'enable_max_depth' => true
],
'denormalization_context' => [
'groups' => 'user:item:put',
'enable_max_depth' => true
]
],
'delete' => [
// 'security' => "is_granted('ROLE_USER')",
]
],
collectionOperations: [
'get' => [
// 'security' => "is_granted('ROLE_USER')",
'normalization_context' => [
'groups' => ['user:collection:get', 'createdAt'],
'enable_max_depth' => true
]
],
'post' => [
// 'security' => "is_granted('ROLE_USER')",
'normalization_context' => [
'groups' => 'user:collection:post',
'enable_max_depth' => true
],
'denormalization_context' => [
'groups' => 'user:collection:post',
'enable_max_depth' => true
]
],
]
)]
#[ApiFilter(SearchFilter::class, properties: [
'brokers.broker' => 'exact',
'insureds.insured' => 'exact',
'insurers.insurer' => 'exact',
'channels.channel' => 'exact',
'roles' => 'partial',
'email' => 'partial',
'name' => 'partial',
'createdAt' => 'start',
])]
#[ApiFilter(OrderFilter::class, properties: [
'name',
'email',
'createdAt',
])]
class User implements UserInterface, PasswordAuthenticatedUserInterface
{
use TimestampableEntity;
#[ORM\Id]
#[ORM\GeneratedValue]
#[ORM\Column(type: 'integer')]
#[Groups([
'user:collection:get',
'user:item:get',
])]
private $id;
/**
* The name of the item.
*
* @see https://schema.org/name
*/
#[ORM\Column(type: 'string', nullable: false)]
#[ApiProperty(iri: 'https://schema.org/name')]
#[Assert\Type('string')]
#[Groups([
'user:collection:get',
'user:collection:post',
'user:item:get',
'user:item:put',
])]
private ?string $name = null;
#[ORM\Column(type: 'string', length: 180, unique: true)]
#[ApiProperty(iri: 'https://schema.org/email')]
#[Groups([
'user:collection:get',
'user:collection:post',
'user:item:get',
'user:item:put',
])]
private $email;
#[ORM\Column(type: 'json')]
#[ApiProperty()]
#[Groups([
'user:collection:get',
'user:collection:post',
'user:item:get',
'user:item:put',
])]
private array $roles = [];
#[ORM\Column(type: 'string')]
private $password;
#[Groups([
'user:collection:post',
'user:item:put',
])]
protected $plainPassword;
#[ORM\Column(type: 'string', length: 255, nullable: true)]
private $confirmationToken;
#[ORM\OneToMany(mappedBy: 'user', targetEntity: BrokerUser::class, cascade: ['persist', 'remove'], orphanRemoval: true)]
#[Groups([
'user:collection:post',
'user:item:get',
'user:item:put',
])]
#[MaxDepth(1)]
private $brokers;
#[ORM\OneToMany(mappedBy: 'user', targetEntity: InsuredUser::class, cascade: ['persist', 'remove'], orphanRemoval: true)]
#[Groups([
'user:collection:post',
'user:item:get',
'user:item:put',
])]
#[MaxDepth(1)]
private $insureds;
#[ORM\OneToMany(mappedBy: 'user', targetEntity: InsurerUser::class, cascade: ['persist', 'remove'], orphanRemoval: true)]
#[Groups([
'user:collection:post',
'user:item:get',
'user:item:put',
])]
#[MaxDepth(1)]
private $insurers;
#[ORM\OneToMany(mappedBy: 'user', targetEntity: ChannelUser::class, cascade: ['persist', 'remove'], orphanRemoval: true)]
#[Groups([
'user:collection:post',
'user:item:get',
'user:item:put',
])]
#[MaxDepth(1)]
private $channels;
#[ApiProperty(iri: 'https://schema.org/image')]
#[ORM\ManyToOne(targetEntity: Media::class, cascade: ["persist", "remove"])]
#[ORM\JoinColumn(nullable: true)]
#[Groups([
'user:collection:get',
'user:collection:post',
'user:item:get',
'user:item:put',
])]
#[MaxDepth(1)]
private $avatar;
public function __construct()
{
$this->brokers = new ArrayCollection();
$this->insureds = new ArrayCollection();
$this->insurers = new ArrayCollection();
$this->channels = new ArrayCollection();
}
public function getId(): ?int
{
return $this->id;
}
public function setName(?string $name): void
{
$this->name = $name;
}
public function getName(): ?string
{
return $this->name;
}
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;
}
/**
* @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 getPlainPassword(): ?string
{
return $this->plainPassword;
}
public function setPlainPassword(?string $plainPassword): self
{
$this->plainPassword = $plainPassword;
return $this;
}
/**
* @see UserInterface
*/
public function eraseCredentials(): void
{
// If you store any temporary, sensitive data on the user, clear it here
// $this->plainPassword = null;
}
/**
* @return Collection|BrokerUser[]
*/
public function getBrokers(): Collection
{
return $this->brokers;
}
public function addBroker(BrokerUser $broker): self
{
if (!$this->brokers->contains($broker)) {
$this->brokers[] = $broker;
$broker->setUser($this);
}
return $this;
}
public function removeBroker(BrokerUser $broker): self
{
if ($this->brokers->removeElement($broker)) {
// set the owning side to null (unless already changed)
if ($broker->getUser() === $this) {
$broker->setUser(null);
}
}
return $this;
}
/**
* @return Collection|InsuredUser[]
*/
public function getInsureds(): Collection
{
return $this->insureds;
}
public function addInsured(InsuredUser $insured): self
{
if (!$this->insureds->contains($insured)) {
$this->insureds[] = $insured;
$insured->setUser($this);
}
return $this;
}
public function removeInsured(InsuredUser $insured): self
{
if ($this->insureds->removeElement($insured)) {
// set the owning side to null (unless already changed)
if ($insured->getUser() === $this) {
$insured->setUser(null);
}
}
return $this;
}
/**
* @return Collection|InsurerUser[]
*/
public function getInsurers(): Collection
{
return $this->insurers;
}
public function addInsurer(InsurerUser $insurer): self
{
if (!$this->insurers->contains($insurer)) {
$this->insurers[] = $insurer;
$insurer->setUser($this);
}
return $this;
}
public function removeInsurer(InsurerUser $insurer): self
{
if ($this->insurers->removeElement($insurer)) {
// set the owning side to null (unless already changed)
if ($insurer->getUser() === $this) {
$insurer->setUser(null);
}
}
return $this;
}
public function getConfirmationToken(): ?string
{
return $this->confirmationToken;
}
public function setConfirmationToken(?string $confirmationToken): self
{
$this->confirmationToken = $confirmationToken;
return $this;
}
/**
* @return Collection|ChannelUser[]
*/
public function getChannels(): Collection
{
return $this->channels;
}
public function addChannel(ChannelUser $channel): self
{
if (!$this->channels->contains($channel)) {
$this->channels[] = $channel;
$channel->setUser($this);
}
return $this;
}
public function removeChannel(ChannelUser $channel): self
{
if ($this->channels->removeElement($channel)) {
// set the owning side to null (unless already changed)
if ($channel->getUser() === $this) {
$channel->setUser(null);
}
}
return $this;
}
public function setAvatar(?Media $avatar): self
{
$this->avatar = $avatar;
return $this;
}
public function getAvatar(): ?Media
{
return $this->avatar;
}
}