<?php
namespace App\Entity;
use ApiPlatform\Core\Annotation\ApiResource;
use ApiPlatform\Core\Annotation\ApiSubresource;
use App\Controller\Api\User\AuthenticationGoogleController;
use App\Controller\Api\User\DeleteUserController;
use App\Controller\Api\User\GetItemController;
use App\Controller\Api\User\MeController;
use App\Controller\Api\User\RefreshTokenController;
use App\Controller\Api\User\RegisterController;
use App\Controller\Api\User\RemoveDeleteRequestController;
use App\Controller\Api\User\ResetPasswordController;
use App\Controller\Api\User\UserCompaniesManagementController;
use App\Repository\UserRepository;
use DateTimeInterface;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
use Doctrine\DBAL\Types\Types;
use Doctrine\ORM\Mapping as ORM;
use Gedmo\Timestampable\Traits\TimestampableEntity;
use Lexik\Bundle\JWTAuthenticationBundle\Security\User\JWTUserInterface;
use Symfony\Component\Security\Core\User\PasswordAuthenticatedUserInterface;
use Symfony\Component\Security\Core\User\UserInterface;
use Symfony\Component\Serializer\Annotation\Groups;
use Symfony\Component\Validator\Constraints as Assert;
#[ORM\Entity(repositoryClass: UserRepository::class)]
#[ApiResource(
collectionOperations: [
'register' => [
'normalization_context' => [
'groups' => 'read:user:collection:register',
],
'denormalization_context' => [
'groups' => 'write:user:collection:register',
],
'method' => 'POST',
'path' => '/register',
'controller' => RegisterController::class,
'write' => false,
],
'resetPassword' => [
'method' => 'POST',
'path' => '/reset-password',
'controller' => ResetPasswordController::class,
],
"authentication_google" => [
'method' => 'POST',
'path' => '/authentication-google',
'controller' => AuthenticationGoogleController::class,
],
'deleteUser' => [
'normalization_context' => [
'groups' => 'read:user:item:me',
],
'method' => 'delete',
'path' => '/delete-user',
'controller' => DeleteUserController::class,
'write' => false,
],
'removeDeleteRequest' => [
'normalization_context' => [
'groups' => 'read:user:item:me',
],
'method' => 'put',
'path' => '/remove-delete-request',
'controller' => RemoveDeleteRequestController::class,
'write' => false,
],
],
itemOperations: [
'delete',
'put',
'get' => [
'normalization_context' => [
'groups' => 'read:user:item:get',
],
'method' => 'GET',
'path' => '/users/{id}',
'controller' => GetItemController::class,
],
'me' => [
'normalization_context' => [
'groups' => 'read:user:item:me',
],
'method' => 'GET',
'path' => '/me',
'controller' => MeController::class,
'read' => false,
],
'refreshToken' => [
'normalization_context' => [
'groups' => 'read:user:item:me',
],
'method' => 'POST',
'path' => '/refresh-token',
'controller' => RefreshTokenController::class,
'read' => false,
],
"getUserCompaniesManagement" => [
'method' => 'GET',
'path' => '/user-companies-management',
'controller' => UserCompaniesManagementController::class,
'normalization_context' => [
'groups' => 'read:user:item:user:companies:management',
],
'read' => false,
],
]
)]
class User implements UserInterface, PasswordAuthenticatedUserInterface, JWTUserInterface
{
const GENDER_MALE = 'man';
const GENDER_FEMALE = 'woman';
const GENDER_OTHER = 'other';
#[ORM\Id]
#[ORM\GeneratedValue]
#[ORM\Column(type: 'integer')]
#[Groups([
'read:user:collection:get',
'read:user:item:get',
'read:user:item:me',
'read:club:collection:getUsers',
'read:club:collection:getAds',
'read:club:collection:getPosts',
'read:club:collection:getCompanies',
'read:company:collection:getCollaborators',
'read:company:collection:get',
])]
private ?int $id;
use TimestampableEntity;
#[ORM\Column(type: 'string', length: 180, unique: true)]
#[Groups([
'read:user:collection:get',
'read:user:collection:register',
'read:user:item:me',
'read:user:item:get',
'write:user:collection:register',
'read:club:collection:getUsers',
])]
#[Assert\Email(
message: 'The email {{ value }} is not a valid email.',
)]
private string $email;
#[ORM\Column(type: Types::JSON)]
#[Groups([
'read:user:item:me',
'read:user:item:get',
'read:club:collection:getUsers',
])]
private array $roles = [];
#[ORM\Column(type: 'string', nullable: true)]
#[Groups([
'write:user:collection:register',
])]
private string $password;
#[ORM\OneToMany(mappedBy: 'user', targetEntity: Photo::class, cascade: ["persist", "remove"])]
#[ApiSubresource]
#[Groups([
'read:user:item:get',
'read:user:item:book',
])]
private Collection $photos;
#[ORM\Column(length: 255, nullable: true)]
#[Groups([
'write:user:collection:put',
'read:user:item:me',
'read:user:item:get',
'read:club:collection:getUsers',
'read:club:collection:getCompanies',
'read:company:collection:getCollaborators',
])]
private ?string $firstname = null;
#[ORM\Column(length: 255, nullable: true)]
#[Groups([
'write:user:collection:put',
'read:user:item:me',
'read:user:item:get',
'read:club:collection:getUsers',
'read:club:collection:getCompanies',
'read:company:collection:getCollaborators',
])]
private ?string $lastname = null;
#[ORM\Column(type: Types::TEXT, nullable: true)]
#[Groups([
'write:user:collection:put',
'read:user:item:me',
'read:user:item:get',
'read:club:collection:getUsers',
])]
private ?string $description = null;
#[ORM\Column(length: 255, nullable: true)]
#[Groups([
'write:user:collection:put',
'read:user:item:me',
'read:user:item:get',
'read:club:collection:getUsers',
])]
private ?string $city = null;
#[ORM\Column(length: 255, nullable: true)]
#[Groups([
'write:user:collection:put',
'read:user:item:me',
'read:user:item:get',
])]
private ?string $country = null;
#[ORM\Column(length: 255, nullable: true)]
#[Groups([
'write:user:collection:put',
'read:user:item:me',
'read:user:item:get',
])]
private ?string $postalCode = null;
#[ORM\Column(type: Types::DATETIME_MUTABLE, nullable: true)]
#[Groups([
'write:user:collection:put',
'read:user:item:me',
'read:user:item:get',
])]
private ?DateTimeInterface $birthDate = null;
#[ORM\Column(length: 255, nullable: true)]
#[Groups([
'read:user:item:me',
'read:club:collection:getUsers',
'read:user:item:get',
])]
private ?string $phone = null;
#[ORM\Column(length: 50, nullable: true)]
#[Groups([
'write:user:collection:put',
'read:user:item:me',
'read:user:item:get',
'read:club:collection:getUsers',
])]
private ?string $gender = null;
#[ORM\Column(nullable: true)]
private ?bool $adminPublicProfile = null;
#[Groups([
'read:user:item:me',
])]
#[ORM\Column(length: 255, nullable: true)]
private ?string $stripeId = null;
#[ORM\OneToMany(mappedBy: 'author', targetEntity: Post::class)]
private Collection $posts;
#[ORM\OneToMany(mappedBy: 'user', targetEntity: Collaborator::class, cascade: ["persist"])]
#[Groups([
'read:user:item:get',
'read:user:collection:get',
'read:club:collection:getUsers',
'read:user:item:me',
])]
private Collection $collaborators;
private ?array $clubs;
#[ORM\OneToMany(mappedBy: 'author', targetEntity: Ad::class)]
private Collection $ads;
#[ORM\Column(type: Types::TEXT, nullable: true)]
private ?string $fti = null;
#[Groups([
'read:user:item:get',
'read:user:collection:get',
'read:club:collection:getUsers',
'read:user:item:me',
])]
#[ORM\Column(length: 255, nullable: true)]
private ?string $title = null;
#[ORM\OneToMany(mappedBy: 'user', targetEntity: UserClub::class)]
private Collection $userClubs;
#[ORM\OneToMany(mappedBy: 'creator', targetEntity: Company::class)]
#[Groups('read:user:item:user:companies:management')]
private Collection $companies;
#[ORM\OneToMany(mappedBy: 'claimer', targetEntity: ClaimingRequest::class)]
private Collection $claimingRequests;
#[ORM\OneToMany(mappedBy: 'user', targetEntity: ClubManager::class, cascade: ["persist"])]
private Collection $clubManagers;
#[ORM\Column(length: 255, nullable: true)]
#[Groups([
'write:user:collection:put',
'read:user:item:me',
])]
private ?string $notificationToken = null;
#[ORM\Column]
#[Groups([
'read:user:item:me',
'write:user:collection:put',
])]
private ?bool $acceptNotification = null;
#[ORM\Column(nullable: true)]
private ?string $googleId = null;
#[ORM\Column(length: 255, nullable: true)]
private ?string $googleName = null;
#[ORM\Column(type: Types::DATE_MUTABLE, nullable: true)]
#[Groups('read:user:item:me')]
private ?DateTimeInterface $deleteRequestDate = null;
public function __construct()
{
$this->id = null;
$this->photos = new ArrayCollection();
$this->adminPublicProfile = false;
$this->posts = new ArrayCollection();
$this->collaborators = new ArrayCollection();
$this->ads = new ArrayCollection();
$this->userClubs = new ArrayCollection();
$this->companies = new ArrayCollection();
$this->claimingRequests = new ArrayCollection();
$this->clubManagers = new ArrayCollection();
$this->acceptNotification = true;
}
public static function createFromPayload($username, array $payload): User
{
$user = new User();
$user->setEmail($username);
return $user;
}
/** @see UserInterface */
public function getUserIdentifier(): string
{
return $this->email;
}
public function getUsername(): ?string
{
return $this->email;
}
/** @see UserInterface */
public function getRoles(): array
{
$roles = $this->roles;
// guarantee every user at least has ROLE_USER
$roles[] = 'ROLE_USER';
if ($this->clubManagers->count() > 0) {
$roles[] = 'ROLE_CLUB_MANAGER';
}
return array_unique($roles);
}
public function setRoles(array $roles): self
{
$this->roles = $roles;
return $this;
}
/** @see UserInterface */
public function eraseCredentials()
{
// If you store any temporary, sensitive data on the user, clear it here
// $this->plainPassword = null;
}
/**
* @see PasswordAuthenticatedUserInterface
*/
public function getPassword(): string
{
return $this->password;
}
public function setPassword(string $password): self
{
$this->password = $password;
return $this;
}
#[Groups([
'read:user:collection:get',
'write:user:collection:register',
'read:user:item:get',
'read:user:item:me',
'read:club:collection:getUsers',
'read:club:collection:getCompanies',
])]
public function getAvatarUrl(): string
{
$mainPhoto = $this->getMainPhoto();
if ($mainPhoto) {
return $mainPhoto->getImageLarge()->getUrl();
}
return 'https://localbeez.ri7.fr/assets/img/image_placeholder.jpg';
}
public function getMainPhoto()
{
foreach ($this->photos as $p) {
if ($p->isMain()) {
return $p;
}
}
if (sizeof($this->photos) > 0) {
return $this->photos[0];
}
return false;
}
#[Groups([
'read:user:collection:get',
'write:user:collection:register',
'read:user:item:get',
'read:club:collection:getPosts',
'read:club:collection:getAds',
'read:user:item:me',
'read:club:collection:getUsers',
'read:club:collection:getPosts',
'read:club:collection:getAds',
'read:company:collection:getCollaborators',
])]
public function getSmallAvatarUrl(): string
{
$mainPhoto = $this->getMainPhoto();
if ($mainPhoto) {
return $mainPhoto->getImageSmall()->getUrl();
}
return 'https://localbeez.ri7.fr/assets/img/image_placeholder.jpg';
}
#[Groups([
'read:user:collection:get',
'write:user:collection:register',
'read:user:item:get',
'read:user:item:me',
'read:club:collection:getUsers',
])]
public function getMediumAvatarUrl(): string
{
$mainPhoto = $this->getMainPhoto();
if ($mainPhoto) {
return $mainPhoto->getImageMedium()->getUrl();
}
return 'https://localbeez.ri7.fr/assets/img/image_placeholder.jpg';
}
public function __toString()
{
return $this->getShortname();
}
#[Groups([
'read:club:collection:getPosts',
'read:club:collection:getAds',
'read:user:item:get',
])]
public function getShortname()
{
if (($this->firstname != null) && ($this->lastname != null)) {
return $this->firstname.'.'.mb_substr($this->lastname, 0, 1);
} elseif ($this->firstname != null) {
return $this->firstname;
}
return $this->email;
}
public function getFullName()
{
if (($this->firstname == null) && ($this->lastname == null) && ($this->googleName != null)) {
return $this->googleName;
}
return $this->firstname.' '.$this->lastname;
}
public function getGenderLabel(): ?string
{
$genders = self::getGenderList();
return ($this->getGender() != null) ? $genders[$this->getGender()] : null;
}
public static function getGenderList()
{
return array(
self::GENDER_FEMALE => 'Femme',
self::GENDER_MALE => 'Homme',
self::GENDER_OTHER => 'Autre',
);
}
#[Groups([
'read:user:item:me',
])]
public function getClubs(): ?array
{
return $this->clubs;
}
public function setClubs($clubs): self
{
$this->clubs = $clubs;
return $this;
}
public function getId(): ?int
{
return $this->id;
}
public function getEmail(): ?string
{
return $this->email;
}
public function setEmail(string $email): self
{
$this->email = $email;
return $this;
}
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;
}
public function getDescription(): ?string
{
return $this->description;
}
public function setDescription(?string $description): self
{
$this->description = $description;
return $this;
}
public function getCity(): ?string
{
return $this->city;
}
public function setCity(?string $city): self
{
$this->city = $city;
return $this;
}
public function getCountry(): ?string
{
return $this->country;
}
public function setCountry(?string $country): self
{
$this->country = $country;
return $this;
}
public function getPostalCode(): ?string
{
return $this->postalCode;
}
public function setPostalCode(?string $postalCode): self
{
$this->postalCode = $postalCode;
return $this;
}
public function getBirthDate(): ?DateTimeInterface
{
return $this->birthDate;
}
public function setBirthDate(?DateTimeInterface $birthDate): self
{
$this->birthDate = $birthDate;
return $this;
}
public function getPhone(): ?string
{
return $this->phone;
}
public function setPhone(?string $phone): self
{
$this->phone = $phone;
return $this;
}
public function getGender(): ?string
{
return $this->gender;
}
public function setGender(?string $gender): self
{
$this->gender = $gender;
return $this;
}
public function isAdminPublicProfile(): ?bool
{
return $this->adminPublicProfile;
}
public function setAdminPublicProfile(?bool $adminPublicProfile): self
{
$this->adminPublicProfile = $adminPublicProfile;
return $this;
}
public function getStripeId(): ?string
{
return $this->stripeId;
}
public function setStripeId(?string $stripeId): self
{
$this->stripeId = $stripeId;
return $this;
}
/**
* @return Collection<int, Photo>
*/
public function getPhotos(): Collection
{
return $this->photos;
}
public function addPhoto(Photo $photo): self
{
if (!$this->photos->contains($photo)) {
$this->photos->add($photo);
$photo->setUser($this);
}
return $this;
}
public function removePhoto(Photo $photo): self
{
if ($this->photos->removeElement($photo)) {
// set the owning side to null (unless already changed)
if ($photo->getUser() === $this) {
$photo->setUser(null);
}
}
return $this;
}
/**
* @return Collection<int, Post>
*/
public function getPosts(): Collection
{
return $this->posts;
}
public function addPost(Post $post): self
{
if (!$this->posts->contains($post)) {
$this->posts->add($post);
$post->setAuthor($this);
}
return $this;
}
public function removePost(Post $post): self
{
if ($this->posts->removeElement($post)) {
// set the owning side to null (unless already changed)
if ($post->getAuthor() === $this) {
$post->setAuthor(null);
}
}
return $this;
}
/**
* @return Collection<int, Collaborator>
*/
public function getCollaborators(): Collection
{
return $this->collaborators;
}
public function addCollaborator(Collaborator $collaborator): self
{
if (!$this->collaborators->contains($collaborator)) {
$this->collaborators->add($collaborator);
$collaborator->setUser($this);
}
return $this;
}
public function removeCollaborator(Collaborator $collaborator): self
{
if ($this->collaborators->removeElement($collaborator)) {
// set the owning side to null (unless already changed)
if ($collaborator->getUser() === $this) {
$collaborator->setUser(null);
}
}
return $this;
}
/**
* @return Collection<int, Ad>
*/
public function getAds(): Collection
{
return $this->ads;
}
public function addAd(Ad $ad): self
{
if (!$this->ads->contains($ad)) {
$this->ads->add($ad);
$ad->setAuthor($this);
}
return $this;
}
public function removeAd(Ad $ad): self
{
if ($this->ads->removeElement($ad)) {
// set the owning side to null (unless already changed)
if ($ad->getAuthor() === $this) {
$ad->setAuthor(null);
}
}
return $this;
}
public function getFti(): ?string
{
return $this->fti;
}
public function setFti(?string $fti): self
{
$this->fti = $fti;
return $this;
}
public function getTitle(): ?string
{
return $this->title;
}
public function setTitle(?string $title): self
{
$this->title = $title;
return $this;
}
/**
* @return Collection<int, UserClub>
*/
public function getUserClubs(): Collection
{
return $this->userClubs;
}
public function addUserClub(UserClub $userClub): self
{
if (!$this->userClubs->contains($userClub)) {
$this->userClubs->add($userClub);
$userClub->setUser($this);
}
return $this;
}
public function removeUserClub(UserClub $userClub): self
{
if ($this->userClubs->removeElement($userClub)) {
// set the owning side to null (unless already changed)
if ($userClub->getUser() === $this) {
$userClub->setUser(null);
}
}
return $this;
}
/**
* @return Collection<int, Company>
*/
public function getCompanies(): Collection
{
return $this->companies;
}
public function addCompany(Company $company): self
{
if (!$this->companies->contains($company)) {
$this->companies->add($company);
$company->setCreator($this);
}
return $this;
}
public function removeCompany(Company $company): self
{
if ($this->companies->removeElement($company)) {
// set the owning side to null (unless already changed)
if ($company->getCreator() === $this) {
$company->setCreator(null);
}
}
return $this;
}
/**
* @return Collection<int, ClaimingRequest>
*/
public function getClaimingRequests(): Collection
{
return $this->claimingRequests;
}
public function addClaimingRequest(ClaimingRequest $claimingRequest): self
{
if (!$this->claimingRequests->contains($claimingRequest)) {
$this->claimingRequests->add($claimingRequest);
$claimingRequest->setClaimer($this);
}
return $this;
}
public function removeClaimingRequest(ClaimingRequest $claimingRequest): self
{
if ($this->claimingRequests->removeElement($claimingRequest)) {
// set the owning side to null (unless already changed)
if ($claimingRequest->getClaimer() === $this) {
$claimingRequest->setClaimer(null);
}
}
return $this;
}
/**
* @return Collection<int, ClubManager>
*/
public function getClubManagers(): Collection
{
return $this->clubManagers;
}
public function addClubManager(ClubManager $clubManager): static
{
if (!$this->clubManagers->contains($clubManager)) {
$this->clubManagers->add($clubManager);
$clubManager->setUser($this);
}
return $this;
}
public function removeClubManager(ClubManager $clubManager): static
{
if ($this->clubManagers->removeElement($clubManager)) {
// set the owning side to null (unless already changed)
if ($clubManager->getUser() === $this) {
$clubManager->setUser(null);
}
}
return $this;
}
public function getNotificationToken(): ?string
{
return $this->notificationToken;
}
public function setNotificationToken(?string $notificationToken): static
{
$this->notificationToken = $notificationToken;
return $this;
}
public function isAcceptNotification(): ?bool
{
return $this->acceptNotification;
}
public function setAcceptNotification(bool $acceptNotification): static
{
$this->acceptNotification = $acceptNotification;
return $this;
}
public function getGoogleId(): ?string
{
return $this->googleId;
}
public function setGoogleId(?string $googleId): static
{
$this->googleId = $googleId;
return $this;
}
public function getGoogleName(): ?string
{
return $this->googleName;
}
public function setGoogleName(?string $googleName): static
{
$this->googleName = $googleName;
return $this;
}
public function getDeleteRequestDate(): ?DateTimeInterface
{
return $this->deleteRequestDate;
}
public function setDeleteRequestDate(?DateTimeInterface $deleteRequestDate): static
{
$this->deleteRequestDate = $deleteRequestDate;
return $this;
}
}