<?php declare(strict_types=1);
/*
* This file is part of the sister_act app.
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace App\Entity;
use App\Annotations\Redaction;
use App\Repository\UserRepository;
use App\Traits\AddressTrait;
use App\Traits\CreatedUpdatedByTrait;
use App\Utils\CommonConstants;
use DateTime;
use DateTimeInterface;
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\UserInterface;
use Symfony\Component\Serializer\Annotation\Groups;
use Symfony\Component\Validator\Constraints as Assert;
use Symfony\Component\Validator\Context\ExecutionContextInterface;
/**
* User.
*
* @ORM\Entity(repositoryClass=UserRepository::class)
* @ORM\Table(name="act_user",
* uniqueConstraints={
* @ORM\UniqueConstraint(columns={"email"})
* }
* )
* @UniqueEntity(fields={"email"}, message="Un compte existe déjà avec ce mail")
* @ORM\InheritanceType("SINGLE_TABLE")
* @ORM\DiscriminatorColumn(name="discr", type="string")
*/
class User implements UserInterface, \Serializable
{
public const DEFAULT_ROLE = [CommonConstants::ROLE_ROLE_DIRECTOR, CommonConstants::ROLE_ROLE_ADVISOR];
public const SERIALIZER_GROUP_LIST = 'user-list';
public const SERIALIZER_GROUP_SHOW = 'user-show';
public const SERIALIZER_GROUP_IGNORE = 'user-ignore';
public const SERIALIZER_GROUP_MANAGER = 'user-manager';
public const SERIALIZER_HQ_LIST = 'hq-list';
public const REDACTION_FIRST_NAME = 'user_firstName';
public const REDACTION_LAST_NAME = 'user_lastName';
use AddressTrait;
use CreatedUpdatedByTrait;
/**
* @var int
*
* @ORM\Id()
* @ORM\GeneratedValue()
* @ORM\Column(name="id", type="integer")
*
* @Groups({
* User::SERIALIZER_GROUP_LIST,
* User::SERIALIZER_GROUP_SHOW,
* User::SERIALIZER_GROUP_MANAGER,
* User::SERIALIZER_HQ_LIST,
* SalesAgreement::SERIALIZER_GROUP_MANAGER,
* SalesAgreement::SERIALIZER_GROUP_LIST,
* CommonConstants::SERIALIZER_GROUP_INVOICE
* })
*/
protected $id;
/**
* @var string
*
* @ORM\Column(name="email", type="string", length=180, unique=true, nullable=false)
*
* @Assert\NotBlank
*
* @Groups({
* User::SERIALIZER_GROUP_LIST,
* User::SERIALIZER_GROUP_SHOW,
* User::SERIALIZER_HQ_LIST,
* CommonConstants::SERIALIZER_GROUP_INVOICE
* })
*/
private $email;
/**
* @ORM\Column(name="roles", type="json", nullable=false)
*
* @Groups({
* User::SERIALIZER_GROUP_SHOW,
* User::SERIALIZER_HQ_LIST
* })
*/
private $roles;
/**
* @var string The hashed password
*
* @ORM\Column(name="password", type="string", length=255, nullable=false)
*
* @Groups({User::SERIALIZER_GROUP_IGNORE})
*/
private $password;
/**
* @var string|null
*
* @ORM\Column(name="civility", type="string", length=5, nullable=true)
*
* @Groups({User::SERIALIZER_GROUP_LIST, User::SERIALIZER_GROUP_SHOW})
*/
private $civility;
/**
* @var string
*
* @ORM\Column(name="last_name", type="string", length=255, nullable=false)
*
* @Assert\NotBlank
*
* @Groups({
* User::SERIALIZER_GROUP_LIST,
* User::SERIALIZER_GROUP_SHOW,
* User::SERIALIZER_GROUP_MANAGER,
* User::SERIALIZER_HQ_LIST,
* SalesAgreement::SERIALIZER_GROUP_LIST,
* SalesAgreement::SERIALIZER_GROUP_MANAGER
* })
*
* @Redaction(title=User::REDACTION_LAST_NAME, increment=false)
*/
private $lastName;
/**
* @var string
*
* @ORM\Column(name="first_name", type="string", length=255, nullable=false)
*
* @Assert\NotBlank
*
* @Groups({
* User::SERIALIZER_GROUP_LIST,
* User::SERIALIZER_GROUP_SHOW,
* User::SERIALIZER_GROUP_MANAGER,
* User::SERIALIZER_HQ_LIST,
* SalesAgreement::SERIALIZER_GROUP_LIST,
* SalesAgreement::SERIALIZER_GROUP_MANAGER
* })
*
* @Redaction(title=User::REDACTION_FIRST_NAME, increment=false)
*/
private $firstName;
/**
* @var string|null
*
* @ORM\Column(name="phone_mobile_pro", type="string", length=20, nullable=true)
*
* @Assert\NotBlank
*
* @Groups({
* User::SERIALIZER_GROUP_SHOW,
* User::SERIALIZER_HQ_LIST,
* })
*/
private $phoneMobilePro;
/**
* @var string|null
*
* @ORM\Column(name="phone_fix_pro", type="string", length=20, nullable=true)
*
* @Groups({User::SERIALIZER_GROUP_SHOW})
*/
private $phoneFixPro;
/**
* @var bool
*
* @ORM\Column(name="is_active", type="boolean", nullable=false)
*
* @Groups({
* User::SERIALIZER_GROUP_LIST,
* User::SERIALIZER_GROUP_SHOW,
* User::SERIALIZER_HQ_LIST,
* })
*/
private $isActive = false;
/**
* @var bool
*
* @ORM\Column(name="is_delete", type="boolean", nullable=false)
*/
private $isDelete = false;
/**
* Connected date in format ISO-8601 YYYY-MM-DDThh:mm:ss±hhmm.
*
* @var DateTime|null
*
* @ORM\Column(name="last_login", type="datetime", nullable=true)
*
* @Groups({
* User::SERIALIZER_GROUP_LIST,
* User::SERIALIZER_GROUP_SHOW,
* User::SERIALIZER_HQ_LIST,})
*/
private $lastLogin;
/**
* @var string
*
* @ORM\Column(name="validation_token", type="string", length=10, unique=true, nullable=true)
*/
private $validationToken;
/**
* @var DateTime|null
*
* @ORM\Column(name="token_created_at", type="datetime", nullable=true)
* @Groups({User::SERIALIZER_GROUP_SHOW})
*/
private $tokenCreatedAt;
/**
* @ORM\OneToMany(targetEntity=Discussion::class, mappedBy="sender")
*
*/
private $discussions;
/**
* @ORM\OneToMany(targetEntity=UserSaleOrder::class, mappedBy="user")
*/
private $userSaleOrders;
/**
* @var bool
*
* @ORM\Column(type="boolean", nullable=true)
*/
private $orderable;
/**
* @ORM\ManyToOne(targetEntity=HeadQuarter::class, inversedBy="agencies")
*
* @Groups({User::SERIALIZER_GROUP_SHOW,User::SERIALIZER_GROUP_LIST})
*/
private $headQuarter;
/**
* @ORM\OneToMany(targetEntity=TemporaryLinkToken::class, mappedBy="agencyUser")
*/
private $temporaryLinkTokens;
/**
* @var boolean
* @ORM\Column(type="boolean")
*/
private $mailSend = false;
public function __construct()
{
$this->tokenCreatedAt = new DateTime();
$this->discussions = new ArrayCollection();
$this->userSaleOrders = new ArrayCollection();
$this->temporaryLinkTokens = new ArrayCollection();
}
/**
* @return int|null
*/
public function getId(): ?int
{
return $this->id;
}
/**
* @return string|null
*/
public function getEmail(): ?string
{
return $this->email;
}
/**
* @param string $email
*
* @return $this
*/
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
{
return $this->roles;
}
/**
* @param string $role
*
* @return $this
*/
public function setRole(string $role): self
{
$this->roles[] = $role;
return $this;
}
/**
* @return $this
*/
public function addRoles(array $roles): self
{
foreach ($roles as $role) {
$this->setRole($role);
}
return $this;
}
/**
* @see UserInterface
*/
public function getPassword(): string
{
return (string) $this->password;
}
/**
* @param string $password
*
* @return $this
*/
public function setPassword(string $password): self
{
$this->password = $password;
return $this;
}
/**
* @see UserInterface
*/
public function getSalt()
{
// not needed when using the "bcrypt" algorithm in security.yaml
}
/**
* @see UserInterface
*/
public function eraseCredentials()
{
// If you store any temporary, sensitive data on the user, clear it here
// $this->plainPassword = null;
}
/**
* @return string|null
*/
public function getCivility(): ?string
{
return $this->civility;
}
/**
* @param string|null $civility
*
* @return $this
*/
public function setCivility(?string $civility): self
{
$this->civility = $civility;
return $this;
}
/**
* @return string|null
*/
public function getLastName(): ?string
{
return null == $this->lastName ? null : strtoupper($this->lastName);
}
/**
* @param string|null $lastName
*
* @return $this
*/
public function setLastName(?string $lastName): self
{
$this->lastName = $lastName;
return $this;
}
/**
* @return string|null
*/
public function getFirstName(): ?string
{
return $this->firstName;
}
/**
* @param string|null $firstName
*
* @return $this
*/
public function setFirstName(?string $firstName): self
{
$this->firstName = $firstName;
return $this;
}
/**
* @return string|null
*/
public function getPhoneMobilePro(): ?string
{
return $this->phoneMobilePro;
}
/**
* @param string|null $phoneMobilePro
*
* @return $this
*/
public function setPhoneMobilePro(?string $phoneMobilePro): self
{
$this->phoneMobilePro = $phoneMobilePro;
return $this;
}
/**
* @return string|null
*/
public function getPhoneFixPro(): ?string
{
return $this->phoneFixPro;
}
/**
* @param string|null $phoneFixPro
*
* @return $this
*/
public function setPhoneFixPro(?string $phoneFixPro): self
{
$this->phoneFixPro = $phoneFixPro;
return $this;
}
/**
* @return bool|null
*/
public function isActive(): ?bool
{
return $this->isActive;
}
/**
* @param bool $isActive
*
* @return $this
*/
public function setIsActive(bool $isActive): self
{
$this->isActive = $isActive;
return $this;
}
/**
* @return DateTimeInterface|null
*/
public function getLastLogin(): ?DateTimeInterface
{
return $this->lastLogin;
}
/**
* @param DateTimeInterface|null $lastLogin
*
* @return $this
*/
public function setLastLogin(?DateTimeInterface $lastLogin): self
{
$this->lastLogin = $lastLogin;
return $this;
}
/**
* @return bool|null
*/
public function getIsDelete(): ?bool
{
return $this->isDelete;
}
/**
* @param bool $isDelete
*
* @return $this
*/
public function setIsDelete(bool $isDelete): self
{
$this->isDelete = $isDelete;
return $this;
}
/**
* @return string|null
*/
public function getValidationToken(): ?string
{
return $this->validationToken;
}
/**
* @param string $validationToken
*
* @return $this
*/
public function setValidationToken(string $validationToken): self
{
$this->validationToken = $validationToken;
return $this;
}
/**
* @return DateTimeInterface|null
*/
public function getTokenCreatedAt(): ?DateTimeInterface
{
return $this->tokenCreatedAt;
}
/**
* @param DateTimeInterface $tokenCreatedAt
*
* @return $this
*/
public function setTokenCreatedAt(DateTimeInterface $tokenCreatedAt): self
{
$this->tokenCreatedAt = $tokenCreatedAt;
return $this;
}
/**
* @return string
*/
public function serialize()
{
return serialize([
$this->id,
$this->password,
$this->email,
$this->roles,
$this->civility,
$this->lastName,
$this->firstName,
$this->phoneMobilePro,
$this->phoneFixPro,
$this->address,
$this->addressComplement,
$this->zipCode,
$this->city,
]);
}
/**
* @param string $serialized
*/
public function unserialize($serialized)
{
list(
$this->id,
$this->password,
$this->email,
$this->roles,
$this->civility,
$this->lastName,
$this->firstName,
$this->phoneMobilePro,
$this->phoneFixPro,
$this->address,
$this->addressComplement,
$this->zipCode,
$this->city
) = unserialize($serialized, ['allowed_classes' => false]);
}
/**
* @return Collection|Discussion[]
*/
public function getDiscussions(): Collection
{
return $this->discussions;
}
/**
* @param Discussion $discussion
*
* @return $this
*/
public function addDiscussion(Discussion $discussion): self
{
if (!$this->discussions->contains($discussion)) {
$this->discussions[] = $discussion;
$discussion->setSender($this);
}
return $this;
}
/**
* @param Discussion $discussion
*
* @return $this
*/
public function removeDiscussion(Discussion $discussion): self
{
if ($this->discussions->contains($discussion)) {
$this->discussions->removeElement($discussion);
// set the owning side to null (unless already changed)
if ($discussion->getSender() === $this) {
$discussion->setSender(null);
}
}
return $this;
}
/**
* @return string
*/
public function __toString()
{
return $this->firstName.' '.$this->lastName;
}
/**
* @return Collection|UserSaleOrder[]
*/
public function getUserSaleOrders(): Collection
{
return $this->userSaleOrders;
}
/**
* @return $this
*/
public function addUserSaleOrder(UserSaleOrder $userSaleOrder): self
{
if (!$this->userSaleOrders->contains($userSaleOrder)) {
$this->userSaleOrders[] = $userSaleOrder;
$userSaleOrder->setUser($this);
}
return $this;
}
/**
* @return $this
*/
public function removeUserSaleOrder(UserSaleOrder $userSaleOrder): self
{
if ($this->userSaleOrders->contains($userSaleOrder)) {
$this->userSaleOrders->removeElement($userSaleOrder);
// set the owning side to null (unless already changed)
if ($userSaleOrder->getUser() === $this) {
$userSaleOrder->setUser(null);
}
}
return $this;
}
/**
* @return bool
*/
public function isOrderable(): ?bool
{
return null !== $this->orderable ? $this->orderable : true;
}
/**
* @return $this
*/
public function setOrderable(bool $orderable): self
{
$this->orderable = $orderable;
return $this;
}
/**
* @return HeadQuarter|null
*/
public function getHeadQuarter(): ?HeadQuarter
{
return $this->headQuarter;
}
/**
* @param HeadQuarter|null $HeadQuarter
*
* @return $this
*/
public function setHeadQuarter(?HeadQuarter $HeadQuarter): self
{
$this->headQuarter = $HeadQuarter;
return $this;
}
/**
* @return Collection|TemporaryLinkToken[]
*/
public function getTemporaryLinkTokens(): Collection
{
return $this->temporaryLinkTokens;
}
public function addTemporaryLinkToken(TemporaryLinkToken $temporaryLinkToken): self
{
if (!$this->temporaryLinkTokens->contains($temporaryLinkToken)) {
$this->temporaryLinkTokens[] = $temporaryLinkToken;
$temporaryLinkToken->setAgencyUser($this);
}
return $this;
}
/**
* @param TemporaryLinkToken $temporaryLinkToken
*
* @return $this
*/
public function removeTemporaryLinkToken(TemporaryLinkToken $temporaryLinkToken): self
{
if ($this->temporaryLinkTokens->removeElement($temporaryLinkToken)) {
// set the owning side to null (unless already changed)
if ($temporaryLinkToken->getAgencyUser() === $this) {
$temporaryLinkToken->setAgencyUser(null);
}
}
return $this;
}
/**
* @Assert\Callback
*
* @param ExecutionContextInterface $context
*/
public function validate(ExecutionContextInterface $context)
{
if (null !== $this->getEmail() and filter_var($this->getEmail(), FILTER_VALIDATE_EMAIL) === false) {
$context->buildViolation('Format email non valide.')
->atPath('email')
->addViolation();
}
if (null != $this->getEmail()) {
$arobase = strpos($this->getEmail(), '@');
$valid = true;
if (!$arobase) {
$valid = false;
} else {
$domain = ltrim(stristr($this->getEmail(), '@'), '@') . '.';
if (!checkdnsrr($domain)) {
$valid = false;
}
}
if (!$valid) {
$context->buildViolation('Domaine de l\'adresse email non valide.')
->atPath('email')
->addViolation();
}
}
if (null === $this->getAddress()) {
$context->buildViolation('Veuillez renseigner l\'adresse')
->atPath('address')
->addViolation();
}
}
/**
* @return bool
*/
public function isMailSend(): bool
{
return $this->mailSend;
}
/**
* @param bool $mailSend
*
* @return $this
*/
public function setMailSend(bool $mailSend): self
{
$this->mailSend = $mailSend;
return $this;
}
/**
* @Groups({CommonConstants::SERIALIZER_GROUP_INVOICE})
*
* @return string
*/
public function getFullName(): string
{
return $this->firstName .' '. $this->lastName;
}
/**
* @Groups({CommonConstants::SERIALIZER_GROUP_INVOICE})
*
* @return string
*/
public function getSociety(): string
{
return '';
}
}