src/Entity/User.php line 41

Open in your IDE?
  1. <?php declare(strict_types=1);
  2. /*
  3.  * This file is part of the sister_act app.
  4.  *
  5.  * For the full copyright and license information, please view the LICENSE
  6.  * file that was distributed with this source code.
  7.  */
  8. namespace App\Entity;
  9. use App\Annotations\Redaction;
  10. use App\Repository\UserRepository;
  11. use App\Traits\AddressTrait;
  12. use App\Traits\CreatedUpdatedByTrait;
  13. use App\Utils\CommonConstants;
  14. use DateTime;
  15. use DateTimeInterface;
  16. use Doctrine\Common\Collections\ArrayCollection;
  17. use Doctrine\Common\Collections\Collection;
  18. use Doctrine\ORM\Mapping as ORM;
  19. use Symfony\Bridge\Doctrine\Validator\Constraints\UniqueEntity;
  20. use Symfony\Component\Security\Core\User\UserInterface;
  21. use Symfony\Component\Serializer\Annotation\Groups;
  22. use Symfony\Component\Validator\Constraints as Assert;
  23. use Symfony\Component\Validator\Context\ExecutionContextInterface;
  24. /**
  25.  * User.
  26.  *
  27.  * @ORM\Entity(repositoryClass=UserRepository::class)
  28.  * @ORM\Table(name="act_user",
  29.  *      uniqueConstraints={
  30.  *          @ORM\UniqueConstraint(columns={"email"})
  31.  *      }
  32.  * )
  33.  * @UniqueEntity(fields={"email"}, message="Un compte existe déjà avec ce mail")
  34.  * @ORM\InheritanceType("SINGLE_TABLE")
  35.  * @ORM\DiscriminatorColumn(name="discr", type="string")
  36.  */
  37. class User implements UserInterface, \Serializable
  38. {
  39.     public const DEFAULT_ROLE = [CommonConstants::ROLE_ROLE_DIRECTORCommonConstants::ROLE_ROLE_ADVISOR];
  40.     public const SERIALIZER_GROUP_LIST 'user-list';
  41.     public const SERIALIZER_GROUP_SHOW 'user-show';
  42.     public const SERIALIZER_GROUP_IGNORE 'user-ignore';
  43.     public const SERIALIZER_GROUP_MANAGER 'user-manager';
  44.     public const SERIALIZER_HQ_LIST 'hq-list';
  45.     public const REDACTION_FIRST_NAME 'user_firstName';
  46.     public const REDACTION_LAST_NAME 'user_lastName';
  47.     use AddressTrait;
  48.     use CreatedUpdatedByTrait;
  49.     /**
  50.      * @var int
  51.      *
  52.      * @ORM\Id()
  53.      * @ORM\GeneratedValue()
  54.      * @ORM\Column(name="id", type="integer")
  55.      *
  56.      * @Groups({
  57.      *     User::SERIALIZER_GROUP_LIST,
  58.      *     User::SERIALIZER_GROUP_SHOW,
  59.      *     User::SERIALIZER_GROUP_MANAGER,
  60.      *     User::SERIALIZER_HQ_LIST,
  61.      *     SalesAgreement::SERIALIZER_GROUP_MANAGER,
  62.      *     SalesAgreement::SERIALIZER_GROUP_LIST,
  63.      *     CommonConstants::SERIALIZER_GROUP_INVOICE
  64.      * })
  65.      */
  66.     protected $id;
  67.     /**
  68.      * @var string
  69.      *
  70.      * @ORM\Column(name="email", type="string", length=180, unique=true, nullable=false)
  71.      *
  72.      * @Assert\NotBlank
  73.      *
  74.      * @Groups({
  75.      *     User::SERIALIZER_GROUP_LIST,
  76.      *     User::SERIALIZER_GROUP_SHOW,
  77.      *     User::SERIALIZER_HQ_LIST,
  78.      *     CommonConstants::SERIALIZER_GROUP_INVOICE
  79.      * })
  80.      */
  81.     private $email;
  82.     /**
  83.      * @ORM\Column(name="roles", type="json", nullable=false)
  84.      *
  85.      * @Groups({
  86.      *     User::SERIALIZER_GROUP_SHOW,
  87.      *     User::SERIALIZER_HQ_LIST
  88.      *     })
  89.      */
  90.     private $roles;
  91.     /**
  92.      * @var string The hashed password
  93.      *
  94.      * @ORM\Column(name="password", type="string", length=255, nullable=false)
  95.      *
  96.      * @Groups({User::SERIALIZER_GROUP_IGNORE})
  97.      */
  98.     private $password;
  99.     /**
  100.      * @var string|null
  101.      *
  102.      * @ORM\Column(name="civility", type="string", length=5, nullable=true)
  103.      *
  104.      * @Groups({User::SERIALIZER_GROUP_LIST, User::SERIALIZER_GROUP_SHOW})
  105.      */
  106.     private $civility;
  107.     /**
  108.      * @var string
  109.      *
  110.      * @ORM\Column(name="last_name", type="string", length=255, nullable=false)
  111.      *
  112.      * @Assert\NotBlank
  113.      *
  114.      * @Groups({
  115.      *     User::SERIALIZER_GROUP_LIST,
  116.      *     User::SERIALIZER_GROUP_SHOW,
  117.      *     User::SERIALIZER_GROUP_MANAGER,
  118.      *     User::SERIALIZER_HQ_LIST,
  119.      *     SalesAgreement::SERIALIZER_GROUP_LIST,
  120.      *     SalesAgreement::SERIALIZER_GROUP_MANAGER
  121.      *     })
  122.      *
  123.      * @Redaction(title=User::REDACTION_LAST_NAME, increment=false)
  124.      */
  125.     private $lastName;
  126.     /**
  127.      * @var string
  128.      *
  129.      * @ORM\Column(name="first_name", type="string", length=255, nullable=false)
  130.      *
  131.      * @Assert\NotBlank
  132.      *
  133.      * @Groups({
  134.      *     User::SERIALIZER_GROUP_LIST,
  135.      *     User::SERIALIZER_GROUP_SHOW,
  136.      *     User::SERIALIZER_GROUP_MANAGER,
  137.      *     User::SERIALIZER_HQ_LIST,
  138.      *     SalesAgreement::SERIALIZER_GROUP_LIST,
  139.      *     SalesAgreement::SERIALIZER_GROUP_MANAGER
  140.      *     })
  141.      *
  142.      * @Redaction(title=User::REDACTION_FIRST_NAME, increment=false)
  143.      */
  144.     private $firstName;
  145.     /**
  146.      * @var string|null
  147.      *
  148.      * @ORM\Column(name="phone_mobile_pro", type="string", length=20, nullable=true)
  149.      *
  150.      * @Assert\NotBlank
  151.      *
  152.      * @Groups({
  153.      *     User::SERIALIZER_GROUP_SHOW,
  154.      *     User::SERIALIZER_HQ_LIST,
  155.      *     })
  156.      */
  157.     private $phoneMobilePro;
  158.     /**
  159.      * @var string|null
  160.      *
  161.      * @ORM\Column(name="phone_fix_pro", type="string", length=20, nullable=true)
  162.      *
  163.      * @Groups({User::SERIALIZER_GROUP_SHOW})
  164.      */
  165.     private $phoneFixPro;
  166.     /**
  167.      * @var bool
  168.      *
  169.      * @ORM\Column(name="is_active", type="boolean", nullable=false)
  170.      *
  171.      * @Groups({
  172.      *     User::SERIALIZER_GROUP_LIST,
  173.      *     User::SERIALIZER_GROUP_SHOW,
  174.      *     User::SERIALIZER_HQ_LIST,
  175.      *     })
  176.      */
  177.     private $isActive false;
  178.     /**
  179.      * @var bool
  180.      *
  181.      * @ORM\Column(name="is_delete", type="boolean", nullable=false)
  182.      */
  183.     private $isDelete false;
  184.     /**
  185.      * Connected date in format ISO-8601 YYYY-MM-DDThh:mm:ss±hhmm.
  186.      *
  187.      * @var DateTime|null
  188.      *
  189.      * @ORM\Column(name="last_login", type="datetime", nullable=true)
  190.      *
  191.      * @Groups({
  192.      *     User::SERIALIZER_GROUP_LIST,
  193.      *     User::SERIALIZER_GROUP_SHOW,
  194.      *     User::SERIALIZER_HQ_LIST,})
  195.      */
  196.     private $lastLogin;
  197.     /**
  198.      * @var string
  199.      *
  200.      * @ORM\Column(name="validation_token", type="string", length=10, unique=true, nullable=true)
  201.      */
  202.     private $validationToken;
  203.     /**
  204.      * @var DateTime|null
  205.      *
  206.      * @ORM\Column(name="token_created_at", type="datetime", nullable=true)
  207.      * @Groups({User::SERIALIZER_GROUP_SHOW})
  208.      */
  209.     private $tokenCreatedAt;
  210.     /**
  211.      * @ORM\OneToMany(targetEntity=Discussion::class, mappedBy="sender")
  212.      *
  213.      */
  214.     private $discussions;
  215.     /**
  216.      * @ORM\OneToMany(targetEntity=UserSaleOrder::class, mappedBy="user")
  217.      */
  218.     private $userSaleOrders;
  219.     /**
  220.      * @var bool
  221.      *
  222.      * @ORM\Column(type="boolean", nullable=true)
  223.      */
  224.     private $orderable;
  225.     /**
  226.      * @ORM\ManyToOne(targetEntity=HeadQuarter::class, inversedBy="agencies")
  227.      *
  228.      * @Groups({User::SERIALIZER_GROUP_SHOW,User::SERIALIZER_GROUP_LIST})
  229.      */
  230.     private $headQuarter;
  231.     /**
  232.      * @ORM\OneToMany(targetEntity=TemporaryLinkToken::class, mappedBy="agencyUser")
  233.      */
  234.     private $temporaryLinkTokens;
  235.     /**
  236.      * @var boolean
  237.      * @ORM\Column(type="boolean")
  238.      */
  239.     private $mailSend false;
  240.     public function __construct()
  241.     {
  242.         $this->tokenCreatedAt = new DateTime();
  243.         $this->discussions = new ArrayCollection();
  244.         $this->userSaleOrders = new ArrayCollection();
  245.         $this->temporaryLinkTokens = new ArrayCollection();
  246.     }
  247.     /**
  248.      * @return int|null
  249.      */
  250.     public function getId(): ?int
  251.     {
  252.         return $this->id;
  253.     }
  254.     /**
  255.      * @return string|null
  256.      */
  257.     public function getEmail(): ?string
  258.     {
  259.         return $this->email;
  260.     }
  261.     /**
  262.      * @param string $email
  263.      *
  264.      * @return $this
  265.      */
  266.     public function setEmail(string $email): self
  267.     {
  268.         $this->email $email;
  269.         return $this;
  270.     }
  271.     /**
  272.      * A visual identifier that represents this user.
  273.      *
  274.      * @see UserInterface
  275.      */
  276.     public function getUsername(): string
  277.     {
  278.         return (string) $this->email;
  279.     }
  280.     /**
  281.      * @see UserInterface
  282.      */
  283.     public function getRoles(): ?array
  284.     {
  285.         return $this->roles;
  286.     }
  287.     /**
  288.      * @param string $role
  289.      *
  290.      * @return $this
  291.      */
  292.     public function setRole(string $role): self
  293.     {
  294.         $this->roles[] = $role;
  295.         return $this;
  296.     }
  297.     /**
  298.      * @return $this
  299.      */
  300.     public function addRoles(array $roles): self
  301.     {
  302.         foreach ($roles as $role) {
  303.             $this->setRole($role);
  304.         }
  305.         return $this;
  306.     }
  307.     /**
  308.      * @see UserInterface
  309.      */
  310.     public function getPassword(): string
  311.     {
  312.         return (string) $this->password;
  313.     }
  314.     /**
  315.      * @param string $password
  316.      *
  317.      * @return $this
  318.      */
  319.     public function setPassword(string $password): self
  320.     {
  321.         $this->password $password;
  322.         return $this;
  323.     }
  324.     /**
  325.      * @see UserInterface
  326.      */
  327.     public function getSalt()
  328.     {
  329.         // not needed when using the "bcrypt" algorithm in security.yaml
  330.     }
  331.     /**
  332.      * @see UserInterface
  333.      */
  334.     public function eraseCredentials()
  335.     {
  336.         // If you store any temporary, sensitive data on the user, clear it here
  337.         // $this->plainPassword = null;
  338.     }
  339.     /**
  340.      * @return string|null
  341.      */
  342.     public function getCivility(): ?string
  343.     {
  344.         return $this->civility;
  345.     }
  346.     /**
  347.      * @param string|null $civility
  348.      *
  349.      * @return $this
  350.      */
  351.     public function setCivility(?string $civility): self
  352.     {
  353.         $this->civility $civility;
  354.         return $this;
  355.     }
  356.     /**
  357.      * @return string|null
  358.      */
  359.     public function getLastName(): ?string
  360.     {
  361.         return null == $this->lastName null strtoupper($this->lastName);
  362.     }
  363.     /**
  364.      * @param string|null $lastName
  365.      *
  366.      * @return $this
  367.      */
  368.     public function setLastName(?string $lastName): self
  369.     {
  370.         $this->lastName $lastName;
  371.         return $this;
  372.     }
  373.     /**
  374.      * @return string|null
  375.      */
  376.     public function getFirstName(): ?string
  377.     {
  378.         return $this->firstName;
  379.     }
  380.     /**
  381.      * @param string|null $firstName
  382.      *
  383.      * @return $this
  384.      */
  385.     public function setFirstName(?string $firstName): self
  386.     {
  387.         $this->firstName $firstName;
  388.         return $this;
  389.     }
  390.     /**
  391.      * @return string|null
  392.      */
  393.     public function getPhoneMobilePro(): ?string
  394.     {
  395.         return $this->phoneMobilePro;
  396.     }
  397.     /**
  398.      * @param string|null $phoneMobilePro
  399.      *
  400.      * @return $this
  401.      */
  402.     public function setPhoneMobilePro(?string $phoneMobilePro): self
  403.     {
  404.         $this->phoneMobilePro $phoneMobilePro;
  405.         return $this;
  406.     }
  407.     /**
  408.      * @return string|null
  409.      */
  410.     public function getPhoneFixPro(): ?string
  411.     {
  412.         return $this->phoneFixPro;
  413.     }
  414.     /**
  415.      * @param string|null $phoneFixPro
  416.      *
  417.      * @return $this
  418.      */
  419.     public function setPhoneFixPro(?string $phoneFixPro): self
  420.     {
  421.         $this->phoneFixPro $phoneFixPro;
  422.         return $this;
  423.     }
  424.     /**
  425.      * @return bool|null
  426.      */
  427.     public function isActive(): ?bool
  428.     {
  429.         return $this->isActive;
  430.     }
  431.     /**
  432.      * @param bool $isActive
  433.      *
  434.      * @return $this
  435.      */
  436.     public function setIsActive(bool $isActive): self
  437.     {
  438.         $this->isActive $isActive;
  439.         return $this;
  440.     }
  441.     /**
  442.      * @return DateTimeInterface|null
  443.      */
  444.     public function getLastLogin(): ?DateTimeInterface
  445.     {
  446.         return $this->lastLogin;
  447.     }
  448.     /**
  449.      * @param DateTimeInterface|null $lastLogin
  450.      *
  451.      * @return $this
  452.      */
  453.     public function setLastLogin(?DateTimeInterface $lastLogin): self
  454.     {
  455.         $this->lastLogin $lastLogin;
  456.         return $this;
  457.     }
  458.     /**
  459.      * @return bool|null
  460.      */
  461.     public function getIsDelete(): ?bool
  462.     {
  463.         return $this->isDelete;
  464.     }
  465.     /**
  466.      * @param bool $isDelete
  467.      *
  468.      * @return $this
  469.      */
  470.     public function setIsDelete(bool $isDelete): self
  471.     {
  472.         $this->isDelete $isDelete;
  473.         return $this;
  474.     }
  475.     /**
  476.      * @return string|null
  477.      */
  478.     public function getValidationToken(): ?string
  479.     {
  480.         return $this->validationToken;
  481.     }
  482.     /**
  483.      * @param string $validationToken
  484.      *
  485.      * @return $this
  486.      */
  487.     public function setValidationToken(string $validationToken): self
  488.     {
  489.         $this->validationToken $validationToken;
  490.         return $this;
  491.     }
  492.     /**
  493.      * @return DateTimeInterface|null
  494.      */
  495.     public function getTokenCreatedAt(): ?DateTimeInterface
  496.     {
  497.         return $this->tokenCreatedAt;
  498.     }
  499.     /**
  500.      * @param DateTimeInterface $tokenCreatedAt
  501.      *
  502.      * @return $this
  503.      */
  504.     public function setTokenCreatedAt(DateTimeInterface $tokenCreatedAt): self
  505.     {
  506.         $this->tokenCreatedAt $tokenCreatedAt;
  507.         return $this;
  508.     }
  509.     /**
  510.      * @return string
  511.      */
  512.     public function serialize()
  513.     {
  514.         return serialize([
  515.             $this->id,
  516.             $this->password,
  517.             $this->email,
  518.             $this->roles,
  519.             $this->civility,
  520.             $this->lastName,
  521.             $this->firstName,
  522.             $this->phoneMobilePro,
  523.             $this->phoneFixPro,
  524.             $this->address,
  525.             $this->addressComplement,
  526.             $this->zipCode,
  527.             $this->city,
  528.         ]);
  529.     }
  530.     /**
  531.      * @param string $serialized
  532.      */
  533.     public function unserialize($serialized)
  534.     {
  535.         list(
  536.             $this->id,
  537.             $this->password,
  538.             $this->email,
  539.             $this->roles,
  540.             $this->civility,
  541.             $this->lastName,
  542.             $this->firstName,
  543.             $this->phoneMobilePro,
  544.             $this->phoneFixPro,
  545.             $this->address,
  546.             $this->addressComplement,
  547.             $this->zipCode,
  548.             $this->city
  549.             ) = unserialize($serialized, ['allowed_classes' => false]);
  550.     }
  551.     /**
  552.      * @return Collection|Discussion[]
  553.      */
  554.     public function getDiscussions(): Collection
  555.     {
  556.         return $this->discussions;
  557.     }
  558.     /**
  559.      * @param Discussion $discussion
  560.      *
  561.      * @return $this
  562.      */
  563.     public function addDiscussion(Discussion $discussion): self
  564.     {
  565.         if (!$this->discussions->contains($discussion)) {
  566.             $this->discussions[] = $discussion;
  567.             $discussion->setSender($this);
  568.         }
  569.         return $this;
  570.     }
  571.     /**
  572.      * @param Discussion $discussion
  573.      *
  574.      * @return $this
  575.      */
  576.     public function removeDiscussion(Discussion $discussion): self
  577.     {
  578.         if ($this->discussions->contains($discussion)) {
  579.             $this->discussions->removeElement($discussion);
  580.             // set the owning side to null (unless already changed)
  581.             if ($discussion->getSender() === $this) {
  582.                 $discussion->setSender(null);
  583.             }
  584.         }
  585.         return $this;
  586.     }
  587.     /**
  588.      * @return string
  589.      */
  590.     public function __toString()
  591.     {
  592.         return $this->firstName.' '.$this->lastName;
  593.     }
  594.     /**
  595.      * @return Collection|UserSaleOrder[]
  596.      */
  597.     public function getUserSaleOrders(): Collection
  598.     {
  599.         return $this->userSaleOrders;
  600.     }
  601.     /**
  602.      * @return $this
  603.      */
  604.     public function addUserSaleOrder(UserSaleOrder $userSaleOrder): self
  605.     {
  606.         if (!$this->userSaleOrders->contains($userSaleOrder)) {
  607.             $this->userSaleOrders[] = $userSaleOrder;
  608.             $userSaleOrder->setUser($this);
  609.         }
  610.         return $this;
  611.     }
  612.     /**
  613.      * @return $this
  614.      */
  615.     public function removeUserSaleOrder(UserSaleOrder $userSaleOrder): self
  616.     {
  617.         if ($this->userSaleOrders->contains($userSaleOrder)) {
  618.             $this->userSaleOrders->removeElement($userSaleOrder);
  619.             // set the owning side to null (unless already changed)
  620.             if ($userSaleOrder->getUser() === $this) {
  621.                 $userSaleOrder->setUser(null);
  622.             }
  623.         }
  624.         return $this;
  625.     }
  626.     /**
  627.      * @return bool
  628.      */
  629.     public function isOrderable(): ?bool
  630.     {
  631.         return null !== $this->orderable $this->orderable true;
  632.     }
  633.     /**
  634.      * @return $this
  635.      */
  636.     public function setOrderable(bool $orderable): self
  637.     {
  638.         $this->orderable $orderable;
  639.         return $this;
  640.     }
  641.     /**
  642.      * @return HeadQuarter|null
  643.      */
  644.     public function getHeadQuarter(): ?HeadQuarter
  645.     {
  646.         return $this->headQuarter;
  647.     }
  648.     /**
  649.      * @param HeadQuarter|null $HeadQuarter
  650.      *
  651.      * @return $this
  652.      */
  653.     public function setHeadQuarter(?HeadQuarter $HeadQuarter): self
  654.     {
  655.         $this->headQuarter $HeadQuarter;
  656.         return $this;
  657.     }
  658.     /**
  659.      * @return Collection|TemporaryLinkToken[]
  660.      */
  661.     public function getTemporaryLinkTokens(): Collection
  662.     {
  663.         return $this->temporaryLinkTokens;
  664.     }
  665.     public function addTemporaryLinkToken(TemporaryLinkToken $temporaryLinkToken): self
  666.     {
  667.         if (!$this->temporaryLinkTokens->contains($temporaryLinkToken)) {
  668.             $this->temporaryLinkTokens[] = $temporaryLinkToken;
  669.             $temporaryLinkToken->setAgencyUser($this);
  670.         }
  671.         return $this;
  672.     }
  673.     /**
  674.      * @param TemporaryLinkToken $temporaryLinkToken
  675.      *
  676.      * @return $this
  677.      */
  678.     public function removeTemporaryLinkToken(TemporaryLinkToken $temporaryLinkToken): self
  679.     {
  680.         if ($this->temporaryLinkTokens->removeElement($temporaryLinkToken)) {
  681.             // set the owning side to null (unless already changed)
  682.             if ($temporaryLinkToken->getAgencyUser() === $this) {
  683.                 $temporaryLinkToken->setAgencyUser(null);
  684.             }
  685.         }
  686.         return $this;
  687.     }
  688.     /**
  689.      * @Assert\Callback
  690.      *
  691.      * @param ExecutionContextInterface $context
  692.      */
  693.     public function validate(ExecutionContextInterface $context)
  694.     {
  695.         if (null !== $this->getEmail() and filter_var($this->getEmail(), FILTER_VALIDATE_EMAIL) === false) {
  696.             $context->buildViolation('Format email non valide.')
  697.                 ->atPath('email')
  698.                 ->addViolation();
  699.         }
  700.         if (null != $this->getEmail()) {
  701.             $arobase strpos($this->getEmail(), '@');
  702.             $valid true;
  703.             if (!$arobase) {
  704.                 $valid false;
  705.             } else {
  706.                 $domain ltrim(stristr($this->getEmail(), '@'), '@') . '.';
  707.                 if (!checkdnsrr($domain)) {
  708.                     $valid false;
  709.                 }
  710.             }
  711.             if (!$valid) {
  712.                 $context->buildViolation('Domaine de l\'adresse email non valide.')
  713.                     ->atPath('email')
  714.                     ->addViolation();
  715.             }
  716.         }
  717.         if (null === $this->getAddress()) {
  718.             $context->buildViolation('Veuillez renseigner l\'adresse')
  719.                 ->atPath('address')
  720.                 ->addViolation();
  721.         }
  722.     }
  723.     /**
  724.      * @return bool
  725.      */
  726.     public function isMailSend(): bool
  727.     {
  728.         return $this->mailSend;
  729.     }
  730.     /**
  731.      * @param bool $mailSend
  732.      *
  733.      * @return $this
  734.      */
  735.     public function setMailSend(bool $mailSend): self
  736.     {
  737.         $this->mailSend $mailSend;
  738.         return $this;
  739.     }
  740.     /**
  741.      * @Groups({CommonConstants::SERIALIZER_GROUP_INVOICE})
  742.      *
  743.      * @return string
  744.      */
  745.     public function getFullName(): string
  746.     {
  747.         return $this->firstName .' '$this->lastName;
  748.     }
  749.     /**
  750.      * @Groups({CommonConstants::SERIALIZER_GROUP_INVOICE})
  751.      *
  752.      * @return string
  753.      */
  754.     public function getSociety(): string
  755.     {
  756.         return '';
  757.     }
  758. }