<?php
namespace App\Entity;
use App\Repository\UserRepository;
use App\Service\ToolsService;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
use Doctrine\ORM\Mapping as ORM;
use Hslavich\OneloginSamlBundle\Security\User\SamlUserInterface;
use Symfony\Bridge\Doctrine\Validator\Constraints\UniqueEntity;
use Symfony\Component\Security\Core\User\PasswordAuthenticatedUserInterface;
use Symfony\Component\Security\Core\User\UserInterface;
use Symfony\Component\Validator\Constraints as Assert;
use Symfony\Component\Serializer\Annotation\Groups;
/**
* @ORM\Entity(repositoryClass=UserRepository::class)
* @ORM\Table(name="`user`")
* @UniqueEntity(
* fields={"email", "category"},
* errorPath="email",
* message="The email is already in use",
* ignoreNull=false
* )
* @ORM\HasLifecycleCallbacks()
*/
class User implements UserInterface, PasswordAuthenticatedUserInterface, SamlUserInterface
{
const GROUP_ADMIN = 'admin';
const GROUP_COMPANY = 'company';
const GROUP_CLIENT = 'client';
const GROUP_SPECIALIST = 'specialist';
const ROLE_USER = 'ROLE_USER';
const ROLE_CLIENT = 'ROLE_CLIENT';
const ROLE_COMPANY = 'ROLE_COMPANY';
const ROLE_EXTERNAL_COMPANY = 'ROLE_EXTERNAL_COMPANY';
const ROLE_ADMIN = 'ROLE_ADMIN';
const ROLE_SUPER_ADMIN = 'ROLE_SUPER_ADMIN';
const ROLE_SPECIALIST = 'ROLE_SPECIALIST';
const GROUPS = [
self::GROUP_ADMIN,
self::GROUP_COMPANY,
self::GROUP_CLIENT,
self::GROUP_SPECIALIST,
];
/**
* @ORM\Id
* @ORM\GeneratedValue
* @ORM\Column(type="integer")
* @Groups("user_favorite:read")
*/
protected $id;
/**
* @ORM\Column(type="string", length=180)
* @Assert\Email
* @Assert\NotBlank
* @Groups("user_favorite:read")
*/
protected $email;
/**
* @ORM\Column(type="json")
* @Groups("user_favorite:read")
*/
protected $roles = [];
/**
* @var string|null The hashed password
* @ORM\Column(type="string")
* @Assert\Length(min="8", max="255")
* @Assert\NotBlank
*/
protected $password = null;
/**
* @ORM\Column(type="datetime")
*/
protected $createdAt;
/**
* @ORM\Column(type="datetime", nullable=true)
*/
protected $lastLogin;
/**
* @ORM\Column(type="string", length=255)
* @Assert\NotBlank()
* @Assert\Length(max="255", min="0")
* @Groups("user_favorite:read")
* @var string
*/
protected $name = '';
/**
* @ORM\Column(type="string", length=50)
* @Assert\Choice(choices=self::GROUPS)
* @Groups("user_favorite:read")
*/
protected $category;
/**
* @ORM\Column(type="boolean")
* @Groups("user_favorite:read")
*/
protected $active = true;
/**
* @ORM\Column(type="datetime", nullable=true)
*/
protected $banStart;
/**
* @ORM\Column(type="datetime", nullable=true)
*/
protected $banEnd;
/**
* @ORM\Column(type="string", length=255, nullable=true, unique=true)
*/
protected $resetToken;
/**
* @ORM\OneToOne(targetEntity=Specialist::class, mappedBy="user", cascade={"persist", "remove"})
*/
protected $specialist;
/**
* @ORM\OneToOne(targetEntity=Company::class, mappedBy="user", cascade={"persist", "remove"})
*/
protected $company;
/**
* @ORM\OneToOne(targetEntity=Client::class, mappedBy="user", cascade={"persist", "remove"})
*/
protected $client;
/**
* @var bool
*/
protected $_hashPwd = false;
/**
* @var null|string
*/
private $token = null;
/**
* @ORM\OneToMany(targetEntity=Document::class, mappedBy="owner")
*/
private $documents;
/**
* @ORM\OneToMany(targetEntity=MarketplaceReservation::class, mappedBy="user")
*/
private $marketplaceReservations;
/**
* @ORM\Column(type="string", length=255, nullable=true)
* @Groups("user_favorite:read")
*/
private $firstName;
/**
* @ORM\Column(type="string", length=255, nullable=true)
* @Groups("user_favorite:read")
*/
private $lastName;
/**
* @ORM\OneToMany(targetEntity=UserFavorite::class, mappedBy="user", cascade={"persist", "remove"})
*/
private $favorites;
/**
* @ORM\ManyToOne(targetEntity=Segmentation::class, inversedBy="users", cascade={"persist"})
*/
private $segmentation;
/**
* @ORM\Column(type="string", length=255, nullable=true, options={"default": "FR"})
*/
private $country;
public function __construct()
{
$this->createdAt = new \DateTime();
$this->roles[] = 'ROLE_USER';
$this->documents = new ArrayCollection();
$this->marketplaceReservations = new ArrayCollection();
$this->favorites = 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 getUserIdentifier(): string
{
return (string)$this->email;
}
/**
* @deprecated since Symfony 5.3, use getUserIdentifier instead
*/
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);
}
/**
* @param string $role
* @return bool
*/
public function hasRole(string $role): bool
{
return in_array($role, $this->roles);
}
/**
* @see UserInterface
*/
public function addRole(string $role): self
{
if (!in_array($role, $this->roles)) {
$this->roles[] = $role;
}
return $this;
}
/**
* @see UserInterface
*/
public function removeRole(string $role): self
{
if (in_array($role, $this->roles)) {
foreach ($this->roles as $key => $item) {
if ($item === $role) {
unset($this->roles[$key]);
}
}
$this->roles = array_values($this->roles);
}
return $this;
}
public function setRoles(array $roles): self
{
$this->roles = $roles;
return $this;
}
/**
* @see PasswordAuthenticatedUserInterface
*/
public function getPassword(): ?string
{
return $this->password;
}
/**
* This setter also checks if the password has changed it will rehash it
* @param string|null $password New password
* @param bool $hash Forces rehash of password
* @return $this
*/
public function setPassword(?string $password, bool $hash = false): self
{
if (!empty($password) && ($this->password !== $password || $hash)) {
$this->_hashPwd = true;
}
if (!empty($password)){
$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 getCreatedAt(): ?\DateTime
{
return $this->createdAt;
}
public function setCreatedAt(\DateTime $createdAt): self
{
$this->createdAt = $createdAt;
return $this;
}
public function getLastLogin(): ?\DateTimeInterface
{
return $this->lastLogin;
}
public function setLastLogin(?\DateTimeInterface $lastLogin): self
{
$this->lastLogin = $lastLogin;
return $this;
}
public function getName(): ?string
{
return $this->name;
}
public function setName(?string $name): self
{
$this->name = $name;
return $this;
}
public function getCategory(): ?string
{
return $this->category;
}
public function setCategory(string $category): self
{
$this->category = $category;
return $this;
}
public function getActive(): ?bool
{
return $this->active;
}
public function setActive(bool $active): self
{
$this->active = $active;
return $this;
}
public function getBanStart(): ?\DateTimeInterface
{
return $this->banStart;
}
public function setBanStart(?\DateTimeInterface $banStart): self
{
$this->banStart = $banStart;
return $this;
}
public function getBanEnd(): ?\DateTimeInterface
{
return $this->banEnd;
}
public function setBanEnd(?\DateTimeInterface $banEnd): self
{
$this->banEnd = $banEnd;
return $this;
}
public function getResetToken(): ?string
{
return $this->resetToken;
}
public function setResetToken(?string $resetToken): self
{
$this->resetToken = $resetToken;
return $this;
}
public function getSpecialist(): ?Specialist
{
return $this->specialist;
}
public function setSpecialist(Specialist $specialist): self
{
// set the owning side of the relation if necessary
if ($specialist->getUser() !== $this) {
$specialist->setUser($this);
}
$this->specialist = $specialist;
return $this;
}
public function getCompany(): ?Company
{
return $this->company;
}
public function setCompany(Company $company): self
{
$this->company = $company;
return $this;
}
public function getClient(): ?Client
{
return $this->client;
}
public function setClient(Client $client): self
{
// set the owning side of the relation if necessary
if ($client->getUser() !== $this) {
$client->setUser($this);
}
$this->client = $client;
return $this;
}
/**
* @return bool
*/
public function isHashPwd(): bool
{
return $this->_hashPwd;
}
/**
* @return string
*/
public function __toString()
{
return $this->name;
}
/**
* @ORM\PrePersist()
*/
public function updateName()
{
if ($this->company instanceof Company) {
$this->name = $this->company->getName();
} elseif ($this->specialist instanceof Specialist) {
$this->name = (string)$this->specialist;
} elseif ($this->client instanceof Client) {
$this->name = (string)$this->client;
}
}
/**
* @param string|null $token
* @return User
*/
public function setToken(?string $token): User
{
$this->token = $token;
return $this;
}
/**
* @return string|null
*/
public function getToken(): ?string
{
return $this->token;
}
public function setSamlAttributes(array $attributes)
{
$this->token = $attributes['sessionIndex'];
$data = [];
foreach ($attributes as $key => $value) {
$data[ToolsService::toCamelCase($key)] = $value;
}
if (!empty($data['email'])) {
if(is_array($data['email'])) $this->email = $data['email'][0];
elseif (is_string($data['email'])) $this->email = $data['email'];
}
if (!empty($data['firstName'])) {
if(is_array($data['firstName'])) $this->firstName = $data['firstName'][0];
elseif (is_string($data['firstName'])) $this->firstName = $data['firstName'];
}
if (!empty($data['lastName'])) {
if(is_array($data['lastName'])) $this->lastName = $data['lastName'][0];
elseif (is_string($data['lastName'])) $this->lastName = $data['lastName'];
}
if (!empty($data['country'])) {
if(is_array($data['country'])) $this->country = $data['country'][0];
elseif (is_string($data['country'])) $this->country = $data['country'];
}
}
/**
* @return Collection<int, Document>
*/
public function getDocuments(): Collection
{
return $this->documents;
}
public function addDocument(Document $document): self
{
if (!$this->documents->contains($document)) {
$this->documents[] = $document;
$document->setOwner($this);
}
return $this;
}
public function removeDocument(Document $document): self
{
if ($this->documents->removeElement($document)) {
// set the owning side to null (unless already changed)
if ($document->getOwner() === $this) {
$document->setOwner(null);
}
}
return $this;
}
/**
* @return Collection<int, MarketplaceReservation>
*/
public function getMarketplaceReservations(): Collection
{
return $this->marketplaceReservations;
}
public function addMarketplaceReservation(MarketplaceReservation $marketplaceReservation): self
{
if (!$this->marketplaceReservations->contains($marketplaceReservation)) {
$this->marketplaceReservations[] = $marketplaceReservation;
$marketplaceReservation->setUser($this);
}
return $this;
}
public function removeMarketplaceReservation(MarketplaceReservation $marketplaceReservation): self
{
if ($this->marketplaceReservations->removeElement($marketplaceReservation)) {
// set the owning side to null (unless already changed)
if ($marketplaceReservation->getUser() === $this) {
$marketplaceReservation->setUser(null);
}
}
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;
}
/**
* @return Collection<int, UserFavorite>
*/
public function getFavorites(): Collection
{
return $this->favorites;
}
public function addFavorite(UserFavorite $favorite): self
{
if (!$this->favorites->contains($favorite)) {
$this->favorites[] = $favorite;
$favorite->setUser($this);
}
return $this;
}
public function removeFavorite(UserFavorite $favorite): self
{
if ($this->favorites->removeElement($favorite)) {
// set the owning side to null (unless already changed)
if ($favorite->getUser() === $this) {
$favorite->setUser(null);
}
}
return $this;
}
public function getSegmentation(): ?Segmentation
{
return $this->segmentation;
}
public function setSegmentation(?Segmentation $segmentation): self
{
$this->segmentation = $segmentation;
return $this;
}
public function getCountry(): ?string
{
return $this->country;
}
public function setCountry(?string $country): self
{
$this->country = $country;
return $this;
}
}