<?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 ApiPlatform\Core\Serializer\Filter\PropertyFilter;
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;
use Ramsey\Uuid\Uuid;
use Ramsey\Uuid\UuidInterface;
#[ORM\Entity(repositoryClass: UserRepository::class)]
#[ApiResource(
iri: 'User',
itemOperations: [
'get' => [
'security' => "
is_granted('ROLE_ADMIN') or
is_granted('ROLE_OPERATIONS_COORDINATOR') or
is_granted('ROLE_RESEARCH_ANALYST')
",
'normalization_context' => [
'groups' => 'user:item:get',
'enable_max_depth' => true
]
],
'put' => [
'security' => "
is_granted('ROLE_ADMIN') or
is_granted('ROLE_OPERATIONS_COORDINATOR') or
is_granted('ROLE_RESEARCH_ANALYST')
",
'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_ADMIN') or
is_granted('ROLE_OPERATIONS_COORDINATOR') or
is_granted('ROLE_RESEARCH_ANALYST')
",
]
],
collectionOperations: [
'get' => [
'security' => "
is_granted('ROLE_ADMIN') or
is_granted('ROLE_OPERATIONS_COORDINATOR') or
is_granted('ROLE_RESEARCH_ANALYST')
",
'normalization_context' => [
'groups' => [
'user:collection:get',
'createdAt'
],
'enable_max_depth' => true
]
],
'post' => [
'security' => "
is_granted('ROLE_ADMIN') or
is_granted('ROLE_OPERATIONS_COORDINATOR') or
is_granted('ROLE_RESEARCH_ANALYST')
",
'normalization_context' => [
'groups' => 'user:collection:post',
'enable_max_depth' => true
],
'denormalization_context' => [
'groups' => 'user:collection:post',
'enable_max_depth' => true
]
],
]
)]
#[ApiFilter(SearchFilter::class, properties: [
'roles' => 'partial',
'email' => 'partial',
'name' => 'partial',
'roles' => 'partial',
'desk' => 'exact',
'createdAt' => 'start',
])]
#[ApiFilter(OrderFilter::class, properties: [
'name',
'email',
'desk',
'createdAt',
])]
#[ApiFilter(PropertyFilter::class)]
class User implements UserInterface, PasswordAuthenticatedUserInterface
{
use TimestampableEntity;
public const ROLE_USER = 'ROLE_USER';
public const ROLE_OPERATOR = 'ROLE_OPERATOR';
public const ROLE_OPERATIONS_COORDINATOR = 'ROLE_OPERATIONS_COORDINATOR';
public const ROLE_BROKER = 'ROLE_BROKER';
public const ROLE_FINANCIAL_MANAGER = 'ROLE_FINANCIAL_MANAGER';
public const ROLE_RESEARCH_ANALYST = 'ROLE_RESEARCH_ANALYST';
public const ROLE_INTERN = 'ROLE_INTERN';
public const ROLE_ADMIN = 'ROLE_ADMIN';
public const ROLE_SUPERADMIN = 'ROLE_SUPERADMIN';
public const ROLES = [
self::ROLE_USER,
self::ROLE_OPERATOR,
self::ROLE_OPERATIONS_COORDINATOR,
self::ROLE_BROKER,
self::ROLE_FINANCIAL_MANAGER,
self::ROLE_RESEARCH_ANALYST,
self::ROLE_INTERN,
self::ROLE_ADMIN,
self::ROLE_SUPERADMIN,
];
public const DESK_OPERATIONS = 'Operations';
public const DESK_DRY = 'Dry';
public const DESK_TANKERS = 'Tankers';
public const DESK_ACCOUNTING = 'Accounting';
public const DESK_RESEARCH = 'Research';
public const DESKS = [
self::DESK_OPERATIONS,
self::DESK_DRY,
self::DESK_TANKERS,
self::DESK_ACCOUNTING,
self::DESK_RESEARCH,
];
#[ORM\Id]
#[ORM\GeneratedValue(strategy: 'NONE')]
#[ORM\Column(type: 'uuid', unique: true)]
private ?UuidInterface $id = null;
#[ORM\Column(type: 'string', nullable: false)]
#[ApiProperty()]
#[Assert\Type('string')]
#[Groups([
'user:collection:get',
'user:collection:post',
'user:item:get',
'user:item:put',
'contract:item:get',
'contract:item:put',
'contract:collection:get',
'contract:collection:post',
'market_rumor:item:get',
'market_rumor:item:put',
'market_rumor:collection:get',
'market_rumor:collection:post'
])]
private ?string $name = null;
#[ORM\Column(type: 'string', length: 180, unique: true)]
#[ApiProperty()]
#[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;
#[ApiProperty()]
#[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;
#[ORM\Column(type: 'string', nullable: true)]
#[ApiProperty()]
#[Assert\Type('string')]
#[Assert\Choice(
choices: self::DESKS,
message: 'The desk is not valid.'
)]
#[Groups([
'user:collection:get',
'user:collection:post',
'user:item:get',
'user:item:put',
'contract:collection:get',
])]
private ?string $desk = null;
#[ORM\OneToMany(
targetEntity: Contract::class,
mappedBy: 'broker'
)]
private ?Collection $contracts = null;
#[ORM\OneToMany(
targetEntity: Contract::class,
mappedBy: 'operator'
)]
private ?Collection $operations = null;
// #[ORM\OneToMany(
// targetEntity: MarketRumor::class,
// mappedBy: 'broker'
// )]
// private ?Collection $marketRumors = null;
public function __construct()
{
$this->id = Uuid::uuid4();
$this->contracts = new ArrayCollection();
$this->operations = new ArrayCollection();
// $this->marketRumors = new ArrayCollection();
}
public function getId(): ?UuidInterface
{
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;
}
public function getUserIdentifier(): string
{
return (string) $this->email;
}
public function getRoles(): array
{
$roles = $this->roles;
// guarantee every user at least has ROLE_USER
$roles[] = self::ROLE_USER;
return array_unique($roles);
}
public function setRoles(array $roles): self
{
$this->roles = $roles;
return $this;
}
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;
}
public function eraseCredentials(): void
{
// If you store any temporary, sensitive data on the user, clear it here
// $this->plainPassword = null;
}
public function getConfirmationToken(): ?string
{
return $this->confirmationToken;
}
public function setConfirmationToken(?string $confirmationToken): self
{
$this->confirmationToken = $confirmationToken;
return $this;
}
public function setAvatar(?Media $avatar): self
{
$this->avatar = $avatar;
return $this;
}
public function getAvatar(): ?Media
{
return $this->avatar;
}
public function setDesk(?string $desk): void
{
$this->desk = $desk;
}
public function getDesk(): ?string
{
return $this->desk;
}
public function getContracts(): Collection
{
return $this->contracts;
}
public function addContract(Contract $contract): self
{
if (!$this->contracts->contains($contract)) {
$this->contracts[] = $contract;
$contract->setBroker($this);
}
return $this;
}
public function removeContract(Contract $contract): self
{
if ($this->contracts->removeElement($contract)) {
if ($contract->getBroker() === $this) {
$contract->setBroker(null);
}
}
return $this;
}
public function getOperations(): Collection
{
return $this->operations;
}
public function addOperation(Contract $operation): self
{
if (!$this->operations->contains($operation)) {
$this->operations[] = $operation;
$operation->setOperator($this);
}
return $this;
}
public function removeOperation(Contract $operation): self
{
if ($this->operations->removeElement($operation)) {
if ($operation->getOperator() === $this) {
$operation->setOperator(null);
}
}
return $this;
}
// public function getMarketRumors(): Collection
// {
// return $this->marketRumors;
// }
// public function addMarketRumor(MarketRumor $marketRumor): self
// {
// if (!$this->marketRumors->contains($marketRumor)) {
// $this->marketRumors[] = $marketRumor;
// $marketRumor->setBroker($this);
// }
// return $this;
// }
// public function removeMarketRumor(MarketRumor $marketRumor): self
// {
// if ($this->marketRumors->removeElement($marketRumor)) {
// if ($marketRumor->getBroker() === $this) {
// $marketRumor->setBroker(null);
// }
// }
// return $this;
// }
}