306 lines
6.6 KiB
PHP
306 lines
6.6 KiB
PHP
<?php
|
|
|
|
namespace App\Entity;
|
|
|
|
use ApiPlatform\Core\Annotation\ApiFilter;
|
|
use ApiPlatform\Core\Annotation\ApiResource;
|
|
use ApiPlatform\Core\Bridge\Doctrine\Orm\Filter\SearchFilter;
|
|
use App\Repository\UserRepository;
|
|
use DateTimeImmutable;
|
|
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;
|
|
|
|
/**
|
|
* => ["security" => "is_granted('ROLE_ADMIN') or object.owner == user"]
|
|
*/
|
|
#[ORM\Entity(repositoryClass: UserRepository::class), ORM\HasLifecycleCallbacks]
|
|
#[ApiResource(
|
|
collectionOperations: ['get', 'post'],
|
|
itemOperations : ['get'],
|
|
attributes : ['security' => 'is_granted("ROLE_USER")']
|
|
)]
|
|
class User implements UserInterface, PasswordAuthenticatedUserInterface
|
|
{
|
|
#[ORM\Id]
|
|
#[ORM\GeneratedValue]
|
|
#[ORM\Column(type: 'integer')]
|
|
private int $id;
|
|
|
|
#[ORM\Column(type: 'string', length: 180, unique: true)]
|
|
private string $username;
|
|
|
|
#[ORM\Column(type: 'json')]
|
|
private array $roles = [];
|
|
|
|
#[ORM\Column(type: 'string')]
|
|
private string $password;
|
|
|
|
private string $plainPassword;
|
|
|
|
#[ORM\Column(type: 'string', length: 255)]
|
|
private string $email;
|
|
|
|
#[ORM\Column(type: 'string', length: 255, nullable: true)]
|
|
private ?string $firstName = '';
|
|
|
|
#[ORM\Column(type: 'string', length: 255, nullable: true)]
|
|
private ?string $lastName = '';
|
|
|
|
#[ORM\ManyToMany(targetEntity: Projects::class, mappedBy: 'developer')]
|
|
private $projects;
|
|
|
|
#[ORM\Column(type: 'string', length: 255, nullable: true)]
|
|
private $avatar;
|
|
|
|
#[ORM\OneToMany(mappedBy: 'owner', targetEntity: Pages::class)]
|
|
private $pages;
|
|
|
|
#[ORM\Column(type: 'datetime_immutable')]
|
|
private $createdAt;
|
|
|
|
#[ORM\Column(type: 'datetime_immutable', nullable: true)]
|
|
private $modifiedAt;
|
|
|
|
public function __construct()
|
|
{
|
|
$this->projects = new ArrayCollection();
|
|
$this->pages = new ArrayCollection();
|
|
}
|
|
|
|
/**
|
|
* @return string|null
|
|
*/
|
|
public function __toString()
|
|
{
|
|
return $this->username;
|
|
}
|
|
|
|
public function getId(): ?int
|
|
{
|
|
return $this->id;
|
|
}
|
|
|
|
public function getPlainPassword(): string
|
|
{
|
|
return $this->plainPassword;
|
|
}
|
|
|
|
public function getUsername(): ?string
|
|
{
|
|
return $this->username;
|
|
}
|
|
|
|
public function setUsername(string $username): self
|
|
{
|
|
$this->username = $username;
|
|
|
|
return $this;
|
|
}
|
|
|
|
/**
|
|
* A visual identifier that represents this user.
|
|
*
|
|
* @see UserInterface
|
|
*/
|
|
public function getUserIdentifier(): string
|
|
{
|
|
return $this->username;
|
|
}
|
|
|
|
/**
|
|
* @see UserInterface
|
|
*/
|
|
public function getRoles(): array
|
|
{
|
|
$roles = $this->roles;
|
|
// guarantee every user at least has ROLE_USER
|
|
$roles[] = 'ROLE_USER';
|
|
|
|
return array_unique(array: $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;
|
|
}
|
|
|
|
/**
|
|
* @see UserInterface
|
|
*/
|
|
public function eraseCredentials()
|
|
{
|
|
$this->plainPassword = '';
|
|
}
|
|
|
|
public function getEmail(): ?string
|
|
{
|
|
return $this->email;
|
|
}
|
|
|
|
public function setEmail(string $email): self
|
|
{
|
|
$this->email = $email;
|
|
|
|
return $this;
|
|
}
|
|
|
|
public function setPlainPassword(string $plainPassword): void
|
|
{
|
|
$this->plainPassword = $plainPassword;
|
|
}
|
|
|
|
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;
|
|
}
|
|
|
|
/**
|
|
* @return Collection<int, Projects>
|
|
*/
|
|
public function getProjects(): Collection
|
|
{
|
|
return $this->projects;
|
|
}
|
|
|
|
public function addProject(Projects $project): self
|
|
{
|
|
if (!$this->projects->contains(element: $project)) {
|
|
$this->projects[] = $project;
|
|
$project->addDeveloper(developer: $this);
|
|
}
|
|
|
|
return $this;
|
|
}
|
|
|
|
public function removeProject(Projects $project): self
|
|
{
|
|
if ($this->projects->removeElement(element: $project)) {
|
|
$project->removeDeveloper(developer: $this);
|
|
}
|
|
|
|
return $this;
|
|
}
|
|
|
|
public function getAvatar(): string
|
|
{
|
|
return $this->avatar ?? '';
|
|
}
|
|
|
|
public function setAvatar(?string $avatar): self
|
|
{
|
|
$this->avatar = $avatar;
|
|
|
|
return $this;
|
|
}
|
|
|
|
/**
|
|
* @return Collection<int, Pages>
|
|
*/
|
|
public function getPages(): Collection
|
|
{
|
|
return $this->pages;
|
|
}
|
|
|
|
public function addPage(Pages $page): self
|
|
{
|
|
if (!$this->pages->contains($page)) {
|
|
$this->pages[] = $page;
|
|
$page->setOwner($this);
|
|
}
|
|
|
|
return $this;
|
|
}
|
|
|
|
public function removePage(Pages $page): self
|
|
{
|
|
if ($this->pages->removeElement($page)) {
|
|
// set the owning side to null (unless already changed)
|
|
if ($page->getOwner() === $this) {
|
|
$page->setOwner(null);
|
|
}
|
|
}
|
|
|
|
return $this;
|
|
}
|
|
|
|
public function getCreatedAt(): ?DateTimeImmutable
|
|
{
|
|
return $this->createdAt;
|
|
}
|
|
|
|
public function setCreatedAt(DateTimeImmutable $createdAt): self
|
|
{
|
|
$this->createdAt = $createdAt;
|
|
|
|
return $this;
|
|
}
|
|
|
|
public function getModifiedAt(): ?DateTimeImmutable
|
|
{
|
|
return $this->modifiedAt;
|
|
}
|
|
|
|
public function setModifiedAt(?DateTimeImmutable $modifiedAt): self
|
|
{
|
|
$this->modifiedAt = $modifiedAt;
|
|
|
|
return $this;
|
|
}
|
|
|
|
public function getAvatarUri()
|
|
{
|
|
return 'avatar';
|
|
}
|
|
|
|
#[ORM\PrePersist]
|
|
public function onPrePersist(): void
|
|
{
|
|
$this->createdAt = new DateTimeImmutable(datetime: 'now');
|
|
}
|
|
|
|
#[ORM\PreUpdate]
|
|
public function onPreUpdate(): void
|
|
{
|
|
$this->modifiedAt = new DateTimeImmutable(datetime: 'now');
|
|
}
|
|
}
|