230 lines
5.5 KiB
PHP
Raw Normal View History

2024-07-11 02:29:08 +02:00
<?php
namespace App\Entity;
2024-07-18 12:36:01 +02:00
use ApiPlatform\Metadata\ApiResource;
use ApiPlatform\Metadata\Get;
2024-08-05 01:30:27 +02:00
use ApiPlatform\Metadata\Post;
2024-07-18 12:36:01 +02:00
use App\Controller\MeController;
2024-08-05 01:30:27 +02:00
use App\Controller\RegistrationController;
2024-07-11 02:29:08 +02:00
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;
2024-07-11 02:29:08 +02:00
use Symfony\Component\Security\Core\User\PasswordAuthenticatedUserInterface;
use Symfony\Component\Security\Core\User\UserInterface;
2024-07-18 12:36:01 +02:00
use Symfony\Component\Serializer\Attribute\Groups;
2024-07-11 02:29:08 +02:00
#[ORM\Entity(repositoryClass: UserRepository::class)]
#[ORM\UniqueConstraint(name: 'UNIQ_IDENTIFIER_EMAIL', fields: ['email'])]
2024-08-02 23:24:52 +02:00
#[ORM\Table(name: '`user`')]
#[UniqueEntity(fields: ['email'], message: 'There is already an account with this email')]
2024-07-18 12:36:01 +02:00
#[ApiResource(
operations: [
new Get(
uriTemplate: '/me',
controller: MeController::class,
2024-08-02 23:24:52 +02:00
normalizationContext: ['groups' => 'user:list'],
2024-07-18 12:36:01 +02:00
read: false
2024-08-02 23:24:52 +02:00
),
2024-08-05 01:30:27 +02:00
new Post(
uriTemplate: '/register',
routeName: 'user_register',
controller: RegistrationController::class,
denormalizationContext: ['groups' => ['user:register']],
read: false,
),
2024-07-18 12:36:01 +02:00
]
)]
2024-07-11 02:29:08 +02:00
class User implements UserInterface, PasswordAuthenticatedUserInterface
{
#[ORM\Id]
#[ORM\GeneratedValue]
#[ORM\Column]
private ?int $id = null;
#[ORM\Column(length: 180)]
2024-08-05 01:30:27 +02:00
#[Groups(['user:list', 'user:register'])]
2024-07-11 02:29:08 +02:00
private ?string $email = null;
/**
2024-07-11 17:01:16 +02:00
* @var array The user roles
2024-07-11 02:29:08 +02:00
*/
#[ORM\Column]
2024-07-18 12:36:01 +02:00
#[Groups(['user:list'])]
2024-07-11 02:29:08 +02:00
private array $roles = [];
/**
2024-07-11 17:01:16 +02:00
* @var string|null The hashed password
2024-07-11 02:29:08 +02:00
*/
2024-07-22 14:52:16 +02:00
#[ORM\Column(nullable: true)]
2024-08-05 01:30:27 +02:00
#[Groups(['user:register'])]
2024-07-11 02:29:08 +02:00
private ?string $password = null;
/**
2024-07-12 15:20:24 +02:00
* @var Collection<int, WatchList>
2024-07-11 02:29:08 +02:00
*/
2024-07-12 15:20:24 +02:00
#[ORM\OneToMany(targetEntity: WatchList::class, mappedBy: 'user', orphanRemoval: true)]
private Collection $watchLists;
2024-07-11 02:29:08 +02:00
2024-07-28 21:14:41 +02:00
/**
* @var Collection<int, Connector>
*/
2024-07-29 15:28:05 +02:00
#[ORM\OneToMany(targetEntity: Connector::class, mappedBy: 'user', orphanRemoval: true)]
2024-07-28 21:14:41 +02:00
private Collection $connectors;
2024-08-05 01:30:27 +02:00
#[ORM\Column]
private bool $isVerified = false;
2024-07-11 02:29:08 +02:00
public function __construct()
{
2024-07-12 15:20:24 +02:00
$this->watchLists = new ArrayCollection();
2024-07-28 21:14:41 +02:00
$this->connectors = new ArrayCollection();
2024-07-11 02:29:08 +02:00
}
public function getId(): ?int
{
return $this->id;
}
public function getEmail(): ?string
{
return $this->email;
}
public function setEmail(string $email): static
{
$this->email = $email;
return $this;
}
/**
* A visual identifier that represents this user.
*
* @see UserInterface
*/
public function getUserIdentifier(): string
{
2024-08-02 23:24:52 +02:00
return (string) $this->email;
2024-07-11 02:29:08 +02:00
}
/**
2024-07-11 22:22:59 +02:00
* @return list<string>
2024-07-11 02:29:08 +02:00
*
2024-08-02 23:24:52 +02:00
* @see UserInterface
2024-07-11 02:29:08 +02:00
*/
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): static
{
$this->roles = $roles;
return $this;
}
/**
* @see PasswordAuthenticatedUserInterface
*/
2024-07-22 14:52:16 +02:00
public function getPassword(): ?string
2024-07-11 02:29:08 +02:00
{
return $this->password;
}
public function setPassword(string $password): static
{
$this->password = $password;
return $this;
}
/**
* @see UserInterface
*/
public function eraseCredentials(): void
{
// If you store any temporary, sensitive data on the user, clear it here
// $this->plainPassword = null;
}
/**
2024-07-12 15:20:24 +02:00
* @return Collection<int, WatchList>
2024-07-11 02:29:08 +02:00
*/
2024-07-12 15:20:24 +02:00
public function getWatchLists(): Collection
2024-07-11 02:29:08 +02:00
{
2024-07-12 15:20:24 +02:00
return $this->watchLists;
2024-07-11 02:29:08 +02:00
}
2024-07-12 15:20:24 +02:00
public function addWatchList(WatchList $watchList): static
2024-07-11 02:29:08 +02:00
{
2024-07-12 15:20:24 +02:00
if (!$this->watchLists->contains($watchList)) {
$this->watchLists->add($watchList);
$watchList->setUser($this);
2024-07-11 02:29:08 +02:00
}
return $this;
}
2024-07-12 15:20:24 +02:00
public function removeWatchList(WatchList $watchList): static
2024-07-11 02:29:08 +02:00
{
2024-07-12 15:20:24 +02:00
if ($this->watchLists->removeElement($watchList)) {
2024-07-11 02:29:08 +02:00
// set the owning side to null (unless already changed)
2024-07-12 15:20:24 +02:00
if ($watchList->getUser() === $this) {
$watchList->setUser(null);
2024-07-11 02:29:08 +02:00
}
}
return $this;
}
2024-07-28 21:14:41 +02:00
/**
* @return Collection<int, Connector>
*/
public function getConnectors(): Collection
{
return $this->connectors;
}
public function addConnector(Connector $connector): static
{
if (!$this->connectors->contains($connector)) {
$this->connectors->add($connector);
2024-07-29 15:28:05 +02:00
$connector->setUser($this);
2024-07-28 21:14:41 +02:00
}
return $this;
}
public function removeConnector(Connector $connector): static
{
if ($this->connectors->removeElement($connector)) {
// set the owning side to null (unless already changed)
2024-07-29 15:28:05 +02:00
if ($connector->getUser() === $this) {
$connector->setUser(null);
2024-07-28 21:14:41 +02:00
}
}
return $this;
}
2024-08-05 01:30:27 +02:00
public function isVerified(): bool
{
return $this->isVerified;
}
public function setVerified(bool $isVerified): static
{
$this->isVerified = $isVerified;
return $this;
}
2024-07-11 02:29:08 +02:00
}