<?php
namespace App\Entity;
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\PasswordHasher\Hasher\UserPasswordHasherInterface;
use Symfony\Component\Security\Core\User\UserInterface;
use ApiPlatform\Core\Annotation\ApiResource;
use ApiPlatform\Core\Annotation\ApiProperty;
use ApiPlatform\Core\Annotation\ApiFilter;
use ApiPlatform\Core\Bridge\Doctrine\Orm\Filter\RangeFilter;
use ApiPlatform\Core\Bridge\Doctrine\Orm\Filter\SearchFilter;
use ApiPlatform\Core\Bridge\Doctrine\Orm\Filter\OrderFilter;
use Symfony\Component\Serializer\Annotation\Groups;
/**
* @ApiResource(
* iri="http://schema.org/User",
* attributes={"security"="is_granted('ROLE_USER')"},
* collectionOperations={
* "get",
* "post"={"security"="is_granted('ROLE_USER')"}
* },
* itemOperations={
* "get",
* "put"={"security"="is_granted('ROLE_USER')" },
* "delete"={"security"="is_granted('ROLE_ADMIN')" },
* })
* @ORM\Entity(repositoryClass=UserRepository::class)
* @ApiFilter(SearchFilter::class,
* properties={
* "username": "exact",
* })
* @UniqueEntity(fields={"email"}, message="There is already an account with this email")
*/
class User implements UserInterface, \Symfony\Component\Security\Core\User\PasswordAuthenticatedUserInterface
{
const ROLE_DEFAULT = "ROLE_USER";
const ROLE_SUPER_ADMIN = "ROLE_SUPER_ADMIN";
/**
* @ORM\Id
* @ORM\Column(type="integer")
* @ORM\GeneratedValue(strategy="AUTO")
*/
public $id;
/**
* @var MediaObject|null
*
* @ORM\ManyToOne(targetEntity=MediaObject::class)
* @ORM\JoinColumn(nullable=true)
* @Groups({"media_object_read"})
* @ApiProperty(iri="http://schema.org/image")
*/
public $userPicture;
/**
* @ORM\Column(type="string", length=255, nullable=true)
*/
public $lastName;
/**
* @ORM\Column(type="string", length=255, nullable=true)
*/
public $firstName;
/**
* @ORM\Column(type="string", length=255, nullable=true)
*/
public $phoneNumber;
/**
* @ORM\OneToMany(targetEntity=Contact::class, mappedBy="threatBy")
*/
private $contacts;
/**
* @ORM\OneToMany(targetEntity=Logs::class, mappedBy="user")
*/
private $logs;
/**
* @ORM\OneToMany(targetEntity=Subscription::class, mappedBy="createdBy", orphanRemoval=true)
*/
private $subscriptions;
/**
* @ORM\ManyToOne(targetEntity=Company::class, inversedBy="users")
*/
private $company;
/**
* @ORM\ManyToOne(targetEntity=Address::class)
*/
private $address;
/**
* @ORM\Column(type="string", length=500, unique=true)
*/
private $email;
/**
* @ORM\Column(type="string", length=500)
*/
private $password;
/**
* @ORM\Column(type="array")
*/
private $roles = [];
/**
* @ORM\Column(type="boolean", nullable=true)
*/
private $active;
/**
* @ORM\Column(type="string", length=500, nullable=true)
*/
private $salt;
/**
* @ORM\Column(type="string", length=500, nullable=true)
*/
private $emailCanonical;
/**
* @ORM\Column(type="string", length=255, nullable=true)
*/
private $plainPassword;
/**
* @ORM\Column(type="string", length=500, nullable=true)
*/
private $confirmationToken;
/**
* @ORM\Column(type="datetime_immutable", nullable=true)
*/
private $passwordRequestedAt;
/**
* @ORM\Column(type="string", length=255, unique=true)
*/
private $username;
/**
* @ORM\Column(type="datetime_immutable", nullable=true)
*/
private $lastLogin;
/**
* @ORM\Column(type="boolean", nullable=true)
*/
private $enabled;
/**
* @ORM\Column(type="string", length=255, nullable=true)
*/
private $usernameCanonical;
/**
* @ORM\Column(type="boolean")
*/
private $isVerified = false;
/**
* @ORM\Column(type="string", length=255)
*/
private $local;
/**
* @ORM\Column(type="integer", nullable=true)
*/
private $loginInCount;
/**
* @ORM\OneToMany(targetEntity=BookingRoom::class, mappedBy="user")
*/
private $bookingRooms;
/**
* @ORM\OneToMany(targetEntity=Payment::class, mappedBy="user")
*/
private $payments;
/**
* @ORM\Column(type="string", length=1000, nullable=true)
*/
private $resendMail;
/**
* @ORM\Column(type="datetime_immutable", nullable=true)
*/
private $createdAt;
/**
* @ORM\Column(type="datetime_immutable", nullable=true)
*/
private $updateAt;
/**
* @ORM\OneToMany(targetEntity=AnnounceChat::class, mappedBy="sendBy", orphanRemoval=true)
*/
private $announceChats;
/**
* @ORM\OneToMany(targetEntity=AnnounceChannel::class, mappedBy="createdBy")
*/
private $announceChannels;
public function __construct()
{
$this->createdAt= new \DateTimeImmutable();
$this->customers = new ArrayCollection();
$this->contacts = new ArrayCollection();
$this->logs = new ArrayCollection();
$this->subscriptions = new ArrayCollection();
$this->enabled = false;
$this->roles = [];
$this->bookingRooms = new ArrayCollection();
$this->payments = new ArrayCollection();
$this->local="fr";
$this->announceChats = new ArrayCollection();
$this->announceChannels = new ArrayCollection();
}
public function getLastName(): ?string
{
return $this->lastName;
}
public function setId(?int $id){
$this->id=$id;
}
public function setLastName(?string $lastName): self
{
$this->lastName = $lastName;
return $this;
}
public function getFirstName(): ?string
{
return $this->firstName;
}
public function setFirstName(?string $firstName): self
{
$this->firstName = $firstName;
return $this;
}
public function getPhoneNumber(): ?string
{
return $this->phoneNumber;
}
public function setPhoneNumber(?string $phoneNumber): self
{
$this->phoneNumber = $phoneNumber;
return $this;
}
/**
* @return Collection|Contact[]
*/
public function getContacts(): Collection
{
return $this->contacts;
}
/**
* @return Collection|Logs[]
*/
public function getLogs(): Collection
{
return $this->logs;
}
public function addContact(Contact $contact): self
{
if (!$this->contacts->contains($contact)) {
$this->contacts[] = $contact;
$contact->setThreatBy($this);
}
return $this;
}
public function addLogs(Logs $logs): self
{
if (!$this->logs->contains($logs)) {
$this->logs[] = $logs;
$logs->setUser($this);
}
return $this;
}
public function removeContact(Contact $contact): self
{
if ($this->contacts->contains($contact)) {
$this->contacts->removeElement($contact);
// set the owning side to null (unless already changed)
if ($contact->getThreatBy() === $this) {
$contact->setThreatBy(null);
}
}
return $this;
}
public function removeLogs(Logs $logs): self
{
if ($this->logs->contains($logs)) {
$this->logs->removeElement($logs);
// set the owning side to null (unless already changed)
if ($logs->getUser() === $this) {
$logs->setUser(null);
}
}
return $this;
}
/**
* @return Collection|Subscription[]
*/
public function getSubscriptions(): Collection
{
return $this->subscriptions;
}
public function addSubscription(Subscription $subscription): self
{
if (!$this->subscriptions->contains($subscription)) {
$this->subscriptions[] = $subscription;
$subscription->setCreatedBy($this);
}
return $this;
}
public function removeSubscription(Subscription $subscription): self
{
if ($this->subscriptions->contains($subscription)) {
$this->subscriptions->removeElement($subscription);
// set the owning side to null (unless already changed)
if ($subscription->getCreatedBy() === $this) {
$subscription->setCreatedBy(null);
}
}
return $this;
}
public function getCompany(): ?Company
{
return $this->company;
}
public function setCompany(?Company $company): self
{
$this->company = $company;
return $this;
}
public function getAddress(): ?Address
{
return $this->address;
}
public function setAddress(?Address $address): self
{
$this->address = $address;
return $this;
}
public function getEmail(): ?string
{
return $this->email;
}
public function setEmail(string $email): self
{
$this->email = $email;
return $this;
}
public function getPassword(): ?string
{
return $this->password;
}
public function setPassword(string $password): self
{
$this->password = $password;
return $this;
}
public function getRoles(): array
{
$roles = $this->roles;
// we need to make sure to have at least one role
$roles[] = static::ROLE_DEFAULT;
return array_values(array_unique($roles));
}
public function setRoles(array $roles): self
{
$this->roles = $roles;
return $this;
}
public function isActive(): ?bool
{
return $this->active;
}
public function setActive(bool $active): self
{
$this->active = $active;
return $this;
}
public function getSalt(): ?string
{
return $this->salt;
}
public function setSalt(?string $salt): self
{
$this->salt = $salt;
return $this;
}
public function getEmailCanonical(): ?string
{
return $this->emailCanonical;
}
public function setEmailCanonical(string $emailCanonical): self
{
$this->emailCanonical = $emailCanonical;
return $this;
}
public function getPlainPassword(): ?string
{
return $this->plainPassword;
}
public function setPlainPassword(string $plainPassword): self
{
$this->plainPassword = $plainPassword;
return $this;
}
public function getConfirmationToken(): ?string
{
return $this->confirmationToken;
}
public function setConfirmationToken(string $confirmationToken): self
{
$this->confirmationToken = $confirmationToken;
return $this;
}
public function getPasswordRequestedAt(): ?\DateTimeImmutable
{
return $this->passwordRequestedAt;
}
public function setPasswordRequestedAt(\DateTimeImmutable $passwordRequestedAt): self
{
$this->passwordRequestedAt = $passwordRequestedAt;
return $this;
}
public function getUsername(): ?string
{
return $this->username;
}
public function setUsername(string $username): self
{
$this->username = $username;
return $this;
}
public function eraseCredentials()
{
// TODO: Implement eraseCredentials() method.
$this->plainPassword = null;
}
public function getUserIdentifier(): string
{
return $this->username;
}
public function addRole($role)
{
$role = strtoupper($role);
if ($role === static::ROLE_DEFAULT) {
return $this;
}
if (!in_array($role, $this->roles, true)) {
$this->roles[] = $role;
}
return $this;
}
public function isEnabled()
{
return $this->enabled;
}
/**
* {@inheritdoc}
*/
public function isSuperAdmin()
{
return $this->hasRole(static::ROLE_SUPER_ADMIN);
}
public function hasRole($role)
{
return in_array(strtoupper($role), $this->getRoles(), true);
}
public function removeRole($role)
{
if (false !== $key = array_search(strtoupper($role), $this->roles, true)) {
unset($this->roles[$key]);
$this->roles = array_values($this->roles);
}
return $this;
}
public function setSuperAdmin($boolean)
{
if (true === $boolean) {
$this->addRole(static::ROLE_SUPER_ADMIN);
} else {
$this->removeRole(static::ROLE_SUPER_ADMIN);
}
return $this;
}
public function setLastLogin(\DateTimeImmutable $time = null)
{
$this->lastLogin = $time;
return $this;
}
public function getLastLogin(): ?\DateTimeImmutable
{
return $this->lastLogin;
}
public function isPasswordRequestNonExpired($ttl)
{
return $this->getPasswordRequestedAt() instanceof \DateTime &&
$this->getPasswordRequestedAt()->getTimestamp() + $ttl > time();
}
public function setEnabled(bool $enabled): self
{
$this->enabled = $enabled;
return $this;
}
public function getId(){
return $this->id;
}
public function getUsernameCanonical(): ?string
{
return $this->usernameCanonical;
}
public function setUsernameCanonical(string $usernameCanonical): self
{
$this->usernameCanonical = $usernameCanonical;
return $this;
}
public function isVerified(): bool
{
return $this->isVerified;
}
public function setIsVerified(bool $isVerified): self
{
$this->isVerified = $isVerified;
return $this;
}
public function __toString()
{
// TODO: Implement __toString() method.
return $this->lastName." ".$this->firstName;
}
public function getLocal(): ?string
{
return $this->local;
}
public function setLocal(string $local): self
{
$this->local = $local;
return $this;
}
public function getLoginInCount(): ?int
{
return $this->loginInCount;
}
public function setLoginInCount(?int $loginInCount): self
{
$this->loginInCount = $loginInCount;
return $this;
}
/**
* @return Collection<int, BookingRoom>
*/
public function getBookingRooms(): Collection
{
return $this->bookingRooms;
}
public function addBookingRoom(BookingRoom $bookingRoom): self
{
if (!$this->bookingRooms->contains($bookingRoom)) {
$this->bookingRooms[] = $bookingRoom;
$bookingRoom->setUser($this);
}
return $this;
}
public function removeBookingRoom(BookingRoom $bookingRoom): self
{
if ($this->bookingRooms->removeElement($bookingRoom)) {
// set the owning side to null (unless already changed)
if ($bookingRoom->getUser() === $this) {
$bookingRoom->setUser(null);
}
}
return $this;
}
/**
* @return Collection<int, Payment>
*/
public function getPayments(): Collection
{
return $this->payments;
}
public function addPayment(Payment $payment): self
{
if (!$this->payments->contains($payment)) {
$this->payments[] = $payment;
$payment->setUser($this);
}
return $this;
}
public function removePayment(Payment $payment): self
{
if ($this->payments->removeElement($payment)) {
// set the owning side to null (unless already changed)
if ($payment->getUser() === $this) {
$payment->setUser(null);
}
}
return $this;
}
public function getResendMail(): ?string
{
return $this->resendMail;
}
public function setResendMail(?string $resendMail): self
{
$this->resendMail = $resendMail;
return $this;
}
public function getCreatedAt(): ?\DateTimeImmutable
{
return $this->createdAt;
}
public function setCreatedAt(\DateTimeImmutable $createdAt): self
{
$this->createdAt = $createdAt;
return $this;
}
public function getUpdateAt(): ?\DateTimeImmutable
{
return $this->updateAt;
}
public function setUpdateAt(\DateTimeImmutable $updateAt): self
{
$this->updateAt = $updateAt;
return $this;
}
/**
* @return Collection<int, AnnounceChat>
*/
public function getAnnounceChats(): Collection
{
return $this->announceChats;
}
public function addAnnounceChat(AnnounceChat $announceChat): self
{
if (!$this->announceChats->contains($announceChat)) {
$this->announceChats[] = $announceChat;
$announceChat->setSendBy($this);
}
return $this;
}
public function removeAnnounceChat(AnnounceChat $announceChat): self
{
if ($this->announceChats->removeElement($announceChat)) {
// set the owning side to null (unless already changed)
if ($announceChat->getSendBy() === $this) {
$announceChat->setSendBy(null);
}
}
return $this;
}
/**
* @return Collection<int, AnnounceChannel>
*/
public function getAnnounceChannels(): Collection
{
return $this->announceChannels;
}
public function addAnnounceChannel(AnnounceChannel $announceChannel): self
{
if (!$this->announceChannels->contains($announceChannel)) {
$this->announceChannels[] = $announceChannel;
$announceChannel->setCreatedBy($this);
}
return $this;
}
public function removeAnnounceChannel(AnnounceChannel $announceChannel): self
{
if ($this->announceChannels->removeElement($announceChannel)) {
// set the owning side to null (unless already changed)
if ($announceChannel->getCreatedBy() === $this) {
$announceChannel->setCreatedBy(null);
}
}
return $this;
}
}