<?php
namespace App\Entity;
use ApiPlatform\Metadata\Post;
use ApiPlatform\Metadata\GetCollection;
use ApiPlatform\Metadata\Delete;
use ApiPlatform\Metadata\Patch;
use ApiPlatform\Metadata\Put;
use ApiPlatform\Metadata\Get;
use ApiPlatform\Metadata\ApiResource;
use App\Repository\UserRepository;
use DateTimeInterface;
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\Bridge\Doctrine\Validator\Constraints\UniqueEntity;
use Symfony\Component\Validator\Constraints as Assert;
#[ApiResource(operations: [new Get(), new Put(security: 'is_granted(\'ROLE_SUPER_ADMIN\')', securityMessage: 'Accès Interdit'), new Patch(security: 'is_granted(\'ROLE_SUPER_ADMIN\')', securityMessage: 'Accès Interdit'), new Delete(security: 'is_granted(\'ROLE_SUPER_ADMIN\')', securityMessage: 'Accès Interdit'), new GetCollection(), new Post(security: 'is_granted(\'ROLE_SUPER_ADMIN\')', securityMessage: 'Accès Interdit')])]
#[ORM\Entity(repositoryClass: UserRepository::class)]
#[UniqueEntity(fields: ['email'], errorPath: 'email', message: 'Cet email est déjà utilisé. Veuillez vous connecter.')]
class User implements UserInterface, PasswordAuthenticatedUserInterface
{
#[ORM\Id]
#[ORM\GeneratedValue]
#[ORM\Column(type: 'integer')]
private $id;
#[ORM\Column(type: 'string', length: 180, unique: true, nullable: true)]
#[Assert\Regex(pattern: '/^\S+@\S+\.\S+$/', message: 'Le champ "Email" doit être une adresse email valide.')]
private ?string $email = null;
#[ORM\Column(type: 'json')]
private array $roles = ['ROLE_USER'];
/**
* @var string The hashed password
*/
#[ORM\Column(type: 'string', nullable: true)]
#[Assert\Regex(pattern: '/^(?=.{6,}$)(?=.*[a-z])(?=.*[A-Z])(?=.*[0-9])(?=.*?\W).*$/', message: 'Votre mot de passe doit contenir au moins 8 caractères dont une majuscule, un chiffre et un caractère spécial')]
private ?string $password = null;
#[ORM\Column(type: 'string', length: 50, nullable: true)]
private ?string $name = null;
#[ORM\Column(type: 'string', length: 100, nullable: true)]
private ?string $lastName = null;
#[Assert\Regex(pattern: '/^(\+\d{2}|\d{2})?\s?\d{2}(\s?\d{2}){3}$/', message: 'Le numéro de téléphone n\'est pas valide.')]
#[ORM\Column(type: 'string', length: 20, nullable: true)]
private ?string $phone = null;
#[ORM\Column(type: 'date', nullable: true)]
#[Assert\LessThanOrEqual(value: '-15 years', message: "Vous devez être agé d'au moins 15 ans.")]
private ?DateTimeInterface $birthDate = null;
#[ORM\Column(type: 'text', nullable: true)]
private ?string $address = null;
#[ORM\Column(type: 'string', length: 255, nullable: true)]
private ?string $additionalAddress = null;
#[ORM\Column(type: 'string', length: 100, nullable: true)]
private ?string $city = null;
#[ORM\Column(type: 'string', length: 5, nullable: true)]
#[Assert\Regex(pattern: '/^(?:[0-8]\d|9[0-8])\d{3}$/', message: 'Le code postal n\'est pas valide.')]
private ?string $postalCode = null;
#[ORM\Column(type: 'boolean', nullable: true)]
private ?bool $isAccept = null;
#[ORM\Column(type: 'boolean', nullable: true)]
private ?bool $isActivated = null;
#[ORM\Column(type: 'string', length: 255, nullable: true)]
private ?string $smsToken = null;
#[ORM\JoinTable(name: 'entity_admin')]
#[ORM\ManyToMany(targetEntity: Entity::class, mappedBy: 'admins')]
private Collection $entitiesAdmins;
#[ORM\ManyToMany(targetEntity: Profile::class, inversedBy: 'users')]
private Collection $profiles;
#[ORM\Column(type: 'string', length: 255, nullable: true)]
private ?string $picture = null;
#[ORM\ManyToOne(targetEntity: Entity::class, cascade: ['persist'])]
#[ORM\JoinColumn(name: 'selected_entity_id', referencedColumnName: 'id', onDelete: 'SET NULL')]
private ?Entity $selectedEntity = null;
#[ORM\OneToMany(targetEntity: UserEntity::class, mappedBy: 'user', orphanRemoval: true, cascade: ['persist'])]
private Collection $userEntities;
#[ORM\OneToMany(targetEntity: Answer::class, mappedBy: 'author')]
private Collection $answers;
#[ORM\OneToMany(targetEntity: SavedNotification::class, mappedBy: 'user', orphanRemoval: true)]
private Collection $notifications;
#[ORM\OneToMany(targetEntity: Comment::class, mappedBy: 'author', orphanRemoval: true)]
private array|Collection $comments;
#[ORM\OneToOne(targetEntity: Device::class, cascade: ['persist', 'remove'])]
private ?Device $device = null;
#[ORM\Column(type: 'date')]
private \DateTime $createdDate;
#[ORM\OneToMany(targetEntity: Like::class, mappedBy: 'author', orphanRemoval: true)]
private Collection $likes;
#[ORM\OneToOne(targetEntity: NotifParams::class, mappedBy: 'user', cascade: ['persist', 'remove'])]
private NotifParams $notifParams;
#[ORM\Column(type: 'string', length: 255, nullable: true)]
private ?string $resetToken = null;
/**
* @var ArrayCollection
*/
private Collection $entities;
/**
* @var string
*/
private string $emailVerif;
#[ORM\Column(type: 'integer', nullable: true)]
#[Assert\Length(min: 14, max: 14, minMessage: 'Numéro invalide', maxMessage: 'Numéro invalide', exactMessage: 'Numéro invalide')]
private ?int $numberGU = null;
#[ORM\OneToMany(targetEntity: UserConnexion::class, mappedBy: 'user', cascade: ['remove'])]
private Collection $userConnexions;
#[ORM\OneToMany(targetEntity: Log::class, mappedBy: 'user', cascade: ['remove'])]
private Collection $logs;
#[ORM\ManyToMany(targetEntity: Territory::class, mappedBy: 'admins')]
private Collection $adminTerritories;
#[ORM\ManyToMany(targetEntity: Territory::class, mappedBy: 'members')]
private Collection $territories;
public function __construct()
{
$this->profiles = new ArrayCollection();
$this->answers = new ArrayCollection();
$this->notifications = new ArrayCollection();
$this->userEntities = new ArrayCollection();
$this->entitiesAdmins = new ArrayCollection();
$this->createdDate = new \DateTime();
$this->likes = new ArrayCollection();
$this->entities = new ArrayCollection();
$this->notifParams = new NotifParams();
$this->notifParams->setUser($this);
$this->userConnexions = new ArrayCollection();
$this->logs = new ArrayCollection();
$this->adminTerritories = new ArrayCollection();
$this->territories = new ArrayCollection();
}
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;
}
/**
* A visual identifier that represents this user.
*
* @see UserInterface
*/
public function getUsername() : string
{
return (string) $this->email;
}
/**
* @see UserInterface
*/
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) : self
{
$this->roles = $roles;
return $this;
}
public function addRole(string $role) : self
{
if (!array_search($role, $this->roles)){
$this->roles[] = $role;
}
return $this;
}
public function removeRole(string $role) : self
{
$index = array_search($role, $this->roles);
if ($index){
array_splice($this->roles, $index, 1);
}
return $this;
}
public function toggleRole(string $role) : self
{
$index = array_search($role, $this->roles);
if ($index){
array_splice($this->roles, $index, 1);
} else {
$this->roles[] = $role;
}
return $this;
}
/**
* @see UserInterface
*/
public function getPassword() : ?string
{
return (string) $this->password;
}
public function setPassword(?string $password) : self
{
$this->password = $password;
return $this;
}
/**
* Returning a salt is only needed, if you are not using a modern
* hashing algorithm (e.g. bcrypt or sodium) in your security.yaml.
*
* @see UserInterface
*/
public function getSalt() : ?string
{
return null;
}
/**
* @see UserInterface
*/
public function eraseCredentials()
{
// If you store any temporary, sensitive data on the user, clear it here
// $this->plainPassword = null;
}
public function getName() : ?string
{
return $this->name;
}
public function setName(?string $name) : self
{
$this->name = $name;
return $this;
}
public function getLastName() : ?string
{
return $this->lastName;
}
public function setLastName(?string $lastName) : self
{
$this->lastName = $lastName;
return $this;
}
public function getPhone() : ?string
{
return $this->phone;
}
public function setPhone(?string $phone) : self
{
$this->phone = $phone;
return $this;
}
public function getBirthDate() : ?DateTimeInterface
{
return $this->birthDate;
}
public function setBirthDate(?DateTimeInterface $birthDate) : self
{
$this->birthDate = $birthDate;
return $this;
}
public function getAddress() : ?string
{
return $this->address;
}
public function setAddress(?string $address) : self
{
$this->address = $address;
return $this;
}
public function getAdditionalAddress() : ?string
{
return $this->additionalAddress;
}
public function setAdditionalAddress(?string $additionalAddress) : self
{
$this->additionalAddress = $additionalAddress;
return $this;
}
public function getCity() : ?string
{
return $this->city;
}
public function setCity(?string $city) : self
{
$this->city = $city;
return $this;
}
public function getPostalCode() : ?string
{
return $this->postalCode;
}
public function setPostalCode(?string $postalCode) : self
{
$this->postalCode = $postalCode;
return $this;
}
public function getIsAccept() : ?bool
{
return $this->isAccept;
}
public function setIsAccept(bool $isAccept) : self
{
$this->isAccept = $isAccept;
return $this;
}
public function getIsActivated() : ?bool
{
return $this->isActivated;
}
public function setIsActivated(bool $isActivated) : self
{
$this->isActivated = $isActivated;
return $this;
}
public function getSmsToken() : ?string
{
return $this->smsToken;
}
public function setSmsToken(?string $smsToken) : self
{
$this->smsToken = $smsToken;
return $this;
}
/**
* @return Collection|Entity[]
*/
public function getEntitiesAdmins() : ?Collection
{
return $this->entitiesAdmins;
}
public function addEntityAdmins(Entity $entity) : self
{
if (!$this->entitiesAdmins->contains($entity)) {
$this->entitiesAdmins[] = $entity;
$entity->addAdmin($this);
}
return $this;
}
public function removeEntityAdmins(Entity $entity) : self
{
if ($this->entitiesAdmins->removeElement($entity)) {
$entity->removeAdmin($this);
}
return $this;
}
/**
* @return Collection|Profile[]
*/
public function getProfiles() : Collection
{
return $this->profiles;
}
public function addProfile(Profile $profile) : self
{
if (!$this->profiles->contains($profile)) {
$this->profiles[] = $profile;
}
return $this;
}
public function setProfiles(array $profiles) : self
{
$this->profiles[] = $profiles;
return $this;
}
public function removeProfile(Profile $profile) : self
{
$this->profiles->removeElement($profile);
return $this;
}
public function getPicture() : ?string
{
return $this->picture;
}
public function setPicture(?string $picture) : self
{
$this->picture = $picture;
return $this;
}
public function getSelectedEntity() : ?Entity
{
return $this->selectedEntity;
}
public function setSelectedEntity(?Entity $selectedEntity) : self
{
$this->selectedEntity = $selectedEntity;
return $this;
}
/**
* @return Collection|UserEntity[]
*/
public function getUserEntities() : Collection
{
return $this->userEntities;
}
public function addUserEntity(UserEntity $userEntity) : self
{
if (!$this->userEntities->contains($userEntity)) {
$this->userEntities[] = $userEntity;
$userEntity->setUser($this);
}
return $this;
}
public function removeUserEntity(UserEntity $userEntity) : self
{
if ($this->userEntities->removeElement($userEntity)) {
// set the owning side to null (unless already changed)
if ($userEntity->getUser() === $this) {
$userEntity->setUser(null);
}
}
return $this;
}
public function removeAllUserEntity() : self
{
foreach ($this->userEntities as $userEntity) {
if ($this->userEntities->removeElement($userEntity)) {
// set the owning side to null (unless already changed)
if ($userEntity->getUser() === $this) {
$userEntity->setUser(null);
}
}
}
return $this;
}
public function getAnswers() : Collection
{
return $this->answers;
}
public function addAnswer(Answer $answer) : self
{
if (!$this->answers->contains($answer)) {
$this->answers[] = $answer;
$answer->setAuthor($this);
}
return $this;
}
public function removeAnswer(Answer $answer) : self
{
if ($this->answers->removeElement($answer)) {
// set the owning side to null (unless already changed)
if ($answer->getAuthor() === $this) {
$answer->setAuthor(null);
}
}
return $this;
}
public function getNotifications() : Collection
{
return $this->notifications;
}
public function addNotification(SavedNotification $notification) : self
{
if (!$this->notifications->contains($notification)) {
$this->notifications[] = $notification;
$notification->setUser($this);
}
return $this;
}
public function removeNotification(SavedNotification $notification) : self
{
if ($this->notifications->removeElement($notification)) {
// set the owning side to null (unless already changed)
if ($notification->getUser() === $this) {
$notification->setUser(null);
}
}
return $this;
}
public function getDevice() : ?Device
{
return $this->device;
}
public function setDevice(?Device $device) : self
{
$this->device = $device;
return $this;
}
public function isUSerAcceptedInEntity($idEntity)
{
foreach ($this->getUserEntities() as $userEntity) {
if ($idEntity == $userEntity->getEntity()->getId()) {
return $userEntity->getIsAccepted();
}
}
return false;
}
public function getCreatedDate() : ?\DateTimeInterface
{
return $this->createdDate;
}
public function setCreatedDate(\DateTimeInterface $createdDate) : self
{
$this->createdDate = $createdDate;
return $this;
}
/**
* @return Collection|Like[]
*/
public function getLikes() : Collection
{
return $this->likes;
}
public function addLike(Like $like) : self
{
if (!$this->likes->contains($like)) {
$this->likes[] = $like;
$like->setAuthor($this);
}
return $this;
}
public function removeLike(Like $like) : self
{
if ($this->likes->removeElement($like)) {
// set the owning side to null (unless already changed)
if ($like->getAuthor() === $this) {
$like->setAuthor(null);
}
}
return $this;
}
public function getNotifParams() : ?NotifParams
{
return $this->notifParams;
}
public function setNotifParams(NotifParams $notifParams) : self
{
// set the owning side of the relation if necessary
if ($notifParams->getUser() !== $this) {
$notifParams->setUser($this);
}
$this->notifParams = $notifParams;
return $this;
}
public function getResetToken() : ?string
{
return $this->resetToken;
}
public function setResetToken(?string $resetToken) : self
{
$this->resetToken = $resetToken;
return $this;
}
/**
* @return Collection|Comment[]
*/
public function getComments() : Collection
{
return $this->comments;
}
public function addComments(Comment $comment) : self
{
if (!$this->comments->contains($comment)) {
$this->comments[] = $comment;
$comment->setAuthor($this);
}
return $this;
}
public function removeComments(Comment $comment) : self
{
if ($this->comments->removeElement($comment)) {
// set the owning side to null (unless already changed)
if ($comment->getAuthor() === $this) {
$comment->setAuthor(null);
}
}
return $this;
}
public function setEntities(ArrayCollection $arrayCollection) : User
{
$this->entities = $arrayCollection;
return $this;
}
/**
* @return Collection|Entity[]
*/
public function getEntities() : ?Collection
{
return $this->entities;
}
public function addEntities(Entity $entities) : User
{
$this->entities[] = $entities;
return $this;
}
public function getEmailVerif() : ?string
{
return $this->emailVerif;
}
public function setEmailVerif(?string $emailVerif) : self
{
$this->emailVerif = $emailVerif;
return $this;
}
public function getNumberGU() : ?int
{
return $this->numberGU;
}
public function setNumberGU(?int $numberGU) : self
{
$this->numberGU = $numberGU;
return $this;
}
public function getFormUserAnswers() : ?FormUserAnswers
{
return $this->formUserAnswers;
}
public function setFormUserAnswers(?FormUserAnswers $formUserAnswers) : self
{
$this->formUserAnswers = $formUserAnswers;
return $this;
}
/**
* @return Collection|Log[]
*/
public function getLogs() : Collection
{
return $this->logs;
}
public function addLog(Log $log) : self
{
if (!$this->logs->contains($log)) {
$this->logs[] = $log;
$log->setUser($this);
}
return $this;
}
public function removeLog(Log $log) : self
{
if ($this->logs->removeElement($log)) {
// set the owning side to null (unless already changed)
if ($log->getUser() === $this) {
$log->setUser(null);
}
}
return $this;
}
public function getUserConnexions() : Collection
{
return $this->userConnexions;
}
public function addUserConnexion(UserConnexion $userConnexion) : self
{
if (!$this->userConnexions->contains($userConnexion)) {
$this->userConnexions[] = $userConnexion;
$userConnexion->setUser($this);
}
return $this;
}
public function removeUserConnexion(UserConnexion $userConnexion) : self
{
if ($this->userConnexions->removeElement($userConnexion)) {
// set the owning side to null (unless already changed)
if ($userConnexion->getUser() === $this) {
$userConnexion->setUser(null);
}
}
return $this;
}
public function getUserIdentifier() : string
{
return (string) $this->email;
}
/**
* @return Collection<int, Territory>
*/
public function getAdminTerritories(): Collection
{
return $this->adminTerritories;
}
public function addAdminTerritory(Territory $adminTerritory): self
{
if (!$this->adminTerritories->contains($adminTerritory)) {
$this->adminTerritories->add($adminTerritory);
$adminTerritory->addAdmin($this);
}
return $this;
}
public function removeAdminTerritory(Territory $adminTerritory): self
{
if ($this->adminTerritories->removeElement($adminTerritory)) {
$adminTerritory->removeAdmin($this);
}
return $this;
}
/**
* @return Collection<int, Territory>
*/
public function getTerritories(): Collection
{
return $this->territories;
}
public function addTerritory(Territory $territory): self
{
if (!$this->territories->contains($territory)) {
$this->territories->add($territory);
$territory->addMember($this);
}
return $this;
}
public function removeTerritory(Territory $territory): self
{
if ($this->territories->removeElement($territory)) {
$territory->removeMember($this);
}
return $this;
}
}