This commit is contained in:
Jérémy Guillot
2024-06-03 17:36:22 +02:00
commit bddcdd6823
73 changed files with 13150 additions and 0 deletions

364
src/Entity/User.php Normal file
View File

@@ -0,0 +1,364 @@
<?php
namespace App\Entity;
use ApiPlatform\Metadata\ApiResource;
use ApiPlatform\Metadata\Delete;
use ApiPlatform\Metadata\Get;
use ApiPlatform\Metadata\GetCollection;
use ApiPlatform\Metadata\Patch;
use ApiPlatform\Metadata\Post;
use ApiPlatform\Metadata\Put;
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\Serializer\Annotation\Groups;
use Symfony\Component\Serializer\Attribute\SerializedName;
use Symfony\Component\Validator\Constraints as Assert;
#[ORM\Entity(repositoryClass: UserRepository::class)]
#[ORM\Table(name: '`user`')]
#[ApiResource(
operations: [
new Get(),
new GetCollection(),
new Post(
validationContext: ['groups' => ['Default', 'postValidation']]
),
new Put(),
new Patch(),
new Delete()
],
normalizationContext: ['groups' => ['user:read']],
denormalizationContext: ['groups' => ['user:write']],
security: "is_granted('ROLE_USER')"
)]
#[UniqueEntity(fields: ['email'], message: 'There is already an account with this email')]
class User implements UserInterface, PasswordAuthenticatedUserInterface
{
#[ORM\Id]
#[ORM\GeneratedValue]
#[ORM\Column]
private ?int $id = null;
#[ORM\Column(length: 180, unique: true)]
#[Groups(['user:read', 'user:write'])]
#[Assert\NotBlank]
#[Assert\Email]
private ?string $email = null;
#[ORM\Column]
private array $roles = [];
/**
* @var ?string The hashed password
*/
#[ORM\Column]
private ?string $password = null;
#[ORM\Column(length: 255)]
#[Groups(['user:read', 'user:write'])]
#[Assert\NotBlank]
private ?string $firstName = null;
#[ORM\Column(length: 255)]
#[Groups(['user:read', 'user:write'])]
#[Assert\NotBlank]
private ?string $lastName = null;
#[ORM\OneToMany(mappedBy: 'ownedBy', targetEntity: ApiToken::class)]
private Collection $apiTokens;
private ?array $accessTokenScopes = null;
#[ORM\ManyToOne(inversedBy: 'employees')]
#[ORM\JoinColumn(nullable: true, onDelete: 'SET NULL')]
#[Groups(['user:read'])]
private ?Company $company = null;
#[Groups(['user:write'])]
#[SerializedName('password')]
#[Assert\NotBlank(groups: ['postValidation'])]
private ?string $plainPassword = null;
#[ORM\OneToMany(mappedBy: 'ownedBy', targetEntity: Project::class)]
#[Groups(['user:read'])]
private Collection $projects;
#[ORM\OneToMany(mappedBy: 'postedBy', targetEntity: Comment::class)]
private Collection $comments;
#[ORM\OneToMany(mappedBy: 'createdBy', targetEntity: Folder::class)]
private Collection $folders;
public function __construct()
{
$this->apiTokens = new ArrayCollection();
$this->projects = new ArrayCollection();
$this->comments = new ArrayCollection();
$this->folders = new ArrayCollection();
}
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
{
return (string) $this->email;
}
/**
* @see UserInterface
*/
public function getRoles(): array
{
if($this->accessTokenScopes !== null){
$roles = $this->roles;
$roles[] = 'ROLE_FRONT_USER';
}else{
$roles = $this->accessTokenScopes;
}
// 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
*/
public function getPassword(): string
{
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;
}
public function getFirstName(): ?string
{
return $this->firstName;
}
public function setFirstName(string $firstName): static
{
$this->firstName = $firstName;
return $this;
}
public function getLastName(): ?string
{
return $this->lastName;
}
public function setLastName(string $lastName): static
{
$this->lastName = $lastName;
return $this;
}
/**
* @return Collection<int, ApiToken>
*/
public function getApiTokens(): Collection
{
return $this->apiTokens;
}
public function addApiToken(ApiToken $apiToken): static
{
if (!$this->apiTokens->contains($apiToken)) {
$this->apiTokens->add($apiToken);
$apiToken->setOwnedBy($this);
}
return $this;
}
public function removeApiToken(ApiToken $apiToken): static
{
if ($this->apiTokens->removeElement($apiToken)) {
// set the owning side to null (unless already changed)
if ($apiToken->getOwnedBy() === $this) {
$apiToken->setOwnedBy(null);
}
}
return $this;
}
/**
* @return string[]
*/
#[Groups(['user:read'])]
public function getValidTokenStrings(): array
{
return $this->getApiTokens()
->filter(fn (ApiToken $token) => $token->isValid())
->map(fn (ApiToken $token) => $token->getToken())
->toArray();
}
public function markAsTokenAuthenticated(array $scopes): void
{
$this->accessTokenScopes = $scopes;
}
public function getCompany(): ?Company
{
return $this->company;
}
public function setCompany(?Company $company): static
{
$this->company = $company;
return $this;
}
public function getPlainPassword(): ?string
{
return $this->plainPassword;
}
public function setPlainPassword(string $plainPassword): void
{
$this->plainPassword = $plainPassword;
}
/**
* @return Collection<int, Project>
*/
public function getProjects(): Collection
{
return $this->projects;
}
public function addProject(Project $project): static
{
if (!$this->projects->contains($project)) {
$this->projects->add($project);
$project->setOwnedBy($this);
}
return $this;
}
public function removeProject(Project $project): static
{
if ($this->projects->removeElement($project)) {
// set the owning side to null (unless already changed)
if ($project->getOwnedBy() === $this) {
$project->setOwnedBy(null);
}
}
return $this;
}
/**
* @return Collection<int, Comment>
*/
public function getComments(): Collection
{
return $this->comments;
}
public function addComment(Comment $comment): static
{
if (!$this->comments->contains($comment)) {
$this->comments->add($comment);
$comment->setPostedBy($this);
}
return $this;
}
public function removeComment(Comment $comment): static
{
if ($this->comments->removeElement($comment)) {
// set the owning side to null (unless already changed)
if ($comment->getPostedBy() === $this) {
$comment->setPostedBy(null);
}
}
return $this;
}
/**
* @return Collection<int, Folder>
*/
public function getFolders(): Collection
{
return $this->folders;
}
public function addFolder(Folder $folder): static
{
if (!$this->folders->contains($folder)) {
$this->folders->add($folder);
$folder->setCreatedBy($this);
}
return $this;
}
public function removeFolder(Folder $folder): static
{
if ($this->folders->removeElement($folder)) {
// set the owning side to null (unless already changed)
if ($folder->getCreatedBy() === $this) {
$folder->setCreatedBy(null);
}
}
return $this;
}
}