src/Evo/Infrastructure/MappingORM/User.php line 41

Open in your IDE?
  1. <?php
  2. namespace Evo\Infrastructure\MappingORM;
  3. use ApiPlatform\Core\Annotation\ApiFilter;
  4. use ApiPlatform\Core\Annotation\ApiProperty;
  5. use ApiPlatform\Core\Annotation\ApiResource;
  6. use ApiPlatform\Core\Bridge\Doctrine\Orm\Filter\SearchFilter;
  7. use ApiPlatform\Core\Serializer\Filter\GroupFilter;
  8. use Doctrine\Common\Collections\ArrayCollection;
  9. use Doctrine\Common\Collections\Collection;
  10. use Doctrine\ORM\Mapping as ORM;
  11. use Symfony\Bridge\Doctrine\Validator\Constraints\UniqueEntity;
  12. use Symfony\Component\Security\Core\User\UserInterface;
  13. use Symfony\Component\Serializer\Annotation\Groups;
  14. use Symfony\Component\Validator\Constraints as Assert;
  15. /**
  16.  * User entity.
  17.  *
  18.  * @ApiResource(attributes={
  19.  *     "normalization_context"={"groups"={"read_user"}},
  20.  *     "denormalization_context"={"groups"={"write_user"}}
  21.  * },
  22.  *     collectionOperations={
  23.  *     "me"= {
  24.  *             "method"="GET",
  25.  *             "path"="/admin/user/me"
  26.  *     },
  27.  *       "get",
  28.  *       "post"
  29.  *     },
  30.  *     itemOperations={"get","put","change_password"={"method"="POST", "route_name"="app_user_change_password"}}
  31.  *)
  32.  * @ApiFilter(SearchFilter::class, properties={"id": "exact", "email": "partial", "phoneNumber": "partial", "resetToken" : "exact", "roles": "partial", "organizations":"exact", "lastname":"partial", "firstname":"partial"})
  33.  * @ApiFilter(GroupFilter::class, arguments={"parameterName": "groups", "overrideDefaultGroups": true})
  34.  * @ORM\Table(name="app_users")
  35.  * @ORM\Entity(repositoryClass="App\Repository\UserRepository")
  36.  * @UniqueEntity("email")
  37.  */
  38. class User implements UserInterface
  39. {
  40.     /**
  41.      * @Groups({"read_user", "read_lead", "admin:user:read", "admin_orga_list", "admin:invoice:read"})
  42.      * @ORM\Column(type="integer")
  43.      * @ORM\Id
  44.      * @ORM\GeneratedValue()
  45.      */
  46.     private $id;
  47.     /**
  48.      * @Groups({"admin_orga_list", "read_user", "write_user", "admin:user:read"})
  49.      * @ORM\ManyToMany(targetEntity="Evo\Infrastructure\MappingORM\Organization", mappedBy="users", cascade={"persist"})
  50.      * @ORM\JoinTable(name="user_organization",
  51.      *      joinColumns={@ORM\JoinColumn(name="user_id", referencedColumnName="id")},
  52.      *      inverseJoinColumns={@ORM\JoinColumn(name="organization_id", referencedColumnName="id")}
  53.      *      )
  54.      */
  55.     private Collection $organizations;
  56.     /**
  57.      * @Groups({"read_user", "write_user"})
  58.      * @ORM\ManyToMany(targetEntity="Evo\Infrastructure\MappingORM\Store")
  59.      * @Assert\Valid
  60.      */
  61.     private Collection $stores;
  62.     /**
  63.      * @Groups({"admin_orga_list", "read_user", "write_user", "admin:user:read"})
  64.      * @ORM\Column(type="string", length=150, unique=true)
  65.      * @Assert\NotBlank
  66.      * @Assert\Email
  67.      */
  68.     private ?string $email null;
  69.     /**
  70.      * @Groups({"admin_orga_list", "read_user", "write_user", "admin:user:read"})
  71.      * @ORM\Column(type="string", length=30, nullable=true)
  72.      */
  73.     private ?string $phoneNumber null;
  74.     /**
  75.      * @Groups({"read_user", "write_user"})
  76.      *
  77.      * @var string The hashed password
  78.      *
  79.      * @ORM\Column(type="text")
  80.      * @Assert\NotBlank
  81.      * @Assert\Regex(pattern="/^(?=^.{8,}$)((?=.*\d)|(?=.*\W+))(?![.\n])(?=.*[A-Z])(?=.*[a-z]).*$/")
  82.      */
  83.     private ?string $password null;
  84.     /**
  85.      * @var array
  86.      *
  87.      * @Groups({"read_user", "write_user", "admin:user:read", "read:activity"})
  88.      * @ORM\Column(type="json")
  89.      * @Assert\NotBlank
  90.      */
  91.     private $roles = [];
  92.     /**
  93.      * @Groups({"admin_orga_list", "read_user", "write_user", "read_lead", "admin:user:read", "read_potential_case"})
  94.      * @ORM\Column(type="string", length=150, nullable=true)
  95.      */
  96.     private ?string $lastname null;
  97.     /**
  98.      * @Groups({"admin_orga_list", "read_user", "write_user", "read_lead", "read:activity", "admin:user:read", "read_potential_case"})
  99.      * @ORM\Column(type="string", length=150, nullable=true)
  100.      */
  101.     private ?string $firstname null;
  102.     /**
  103.      * @Groups({"read_user", "admin:user:read"})
  104.      * @ORM\Column(type="datetime")
  105.      */
  106.     private \DateTimeInterface $createdAt;
  107.     /**
  108.      * @Groups({"read_user"})
  109.      * @ORM\Column(type="string", length=255, nullable=true)
  110.      */
  111.     private ?string $registrationSource null;
  112.     /**
  113.      * @Groups({"read_user", "write_user"})
  114.      * @ORM\Column(type="string", length=255, nullable=true)
  115.      */
  116.     private ?string $honorificPrefix null;
  117.     /**
  118.      * @ApiProperty(writable=false)
  119.      * @Groups({"read_user", "write_user"})
  120.      * @ORM\Column(type="string", length=255, nullable=true)
  121.      */
  122.     private ?string $resetToken null;
  123.     /**
  124.      * @ApiProperty(writable=false)
  125.      * @Groups({"read_user"})
  126.      * @ORM\Column(type="string", length=255, nullable=true)
  127.      */
  128.     private ?string $resetTokenExpirationDate null;
  129.     /**
  130.      * @ORM\OneToMany(targetEntity="Evo\Infrastructure\MappingORM\Invoice", mappedBy="user")
  131.      */
  132.     private ?Collection $invoices;
  133.     /**
  134.      * @Groups({"read_user", "write_user"})
  135.      * @ORM\OneToMany(targetEntity="Evo\Infrastructure\MappingORM\UserUTM", mappedBy="user", cascade={"persist"})
  136.      */
  137.     private ?Collection $UTMs;
  138.     /**
  139.      * @Groups({"read_user", "write_user"})
  140.      * @ORM\Column(type="string", length=255, nullable=true)
  141.      */
  142.     private ?string $uniqId null;
  143.     /**
  144.      * @ORM\Column(type="integer", nullable=true)
  145.      */
  146.     private ?int $codeConfirmation null;
  147.     /**
  148.      * @ORM\OneToMany(targetEntity="Evo\Infrastructure\MappingORM\Lead", mappedBy="manager")
  149.      */
  150.     private ?Collection $leads;
  151.     /**
  152.      * @ORM\OneToMany(targetEntity=ActivityLog::class, mappedBy="user")
  153.      */
  154.     private ?Collection $activityLogs;
  155.     /**
  156.      * @Groups({"write_user", "admin:user:read"})
  157.      * @ORM\Column(type="datetime", nullable=true)
  158.      */
  159.     private ?\DateTimeInterface $updatedAt null;
  160.     /**
  161.      * @Groups({"read_user", "write_user", "admin:user:read"})
  162.      * @ORM\Column(type="boolean", options={"default" : true})
  163.      */
  164.     private bool $isActive true;
  165.     /**
  166.      * @ORM\OneToMany(targetEntity=CommercialGesture::class, mappedBy="user")
  167.      */
  168.     private ?Collection $commercialGestures;
  169.     /**
  170.      * @ORM\OneToMany(targetEntity=PappersActivityLogWrapper::class, mappedBy="user")
  171.      */
  172.     private Collection $pappersActivityLogWrappers;
  173.     public function __construct()
  174.     {
  175.         $this->setRoles(['ROLE_USER']);
  176.         $this->organizations = new ArrayCollection();
  177.         $this->stores = new ArrayCollection();
  178.         $this->invoices = new ArrayCollection();
  179.         $this->UTMs = new ArrayCollection();
  180.         $this->leads = new ArrayCollection();
  181.         $this->activityLogs = new ArrayCollection();
  182.         $this->createdAt = new \DateTime();
  183.         $this->commercialGestures = new ArrayCollection();
  184.         $this->pappersActivityLogWrappers = new ArrayCollection();
  185.     }
  186.     public function __toString(): string
  187.     {
  188.         if (' ' !== $this->getFullName()) {
  189.             return $this->getFullName();
  190.         }
  191.         return $this->getEmail() ?? 'N/A';
  192.     }
  193.     /**
  194.      * @Groups({"admin:invoice:read"})
  195.      */
  196.     public function getFullName(): string
  197.     {
  198.         if (!($this->firstname && $this->lastname)) {
  199.             return $this->email;
  200.         }
  201.         return trim($this->firstname.' '.$this->lastname);
  202.     }
  203.     public function getEmail(): ?string
  204.     {
  205.         return $this->email;
  206.     }
  207.     public function setEmail(?string $email): self
  208.     {
  209.         $this->email $email;
  210.         return $this;
  211.     }
  212.     /**
  213.      * @return string
  214.      */
  215.     public function getId()
  216.     {
  217.         return $this->id;
  218.     }
  219.     /**
  220.      * @return Organization[]
  221.      */
  222.     public function getOrganizations()
  223.     {
  224.         return $this->organizations->getValues();
  225.     }
  226.     /**
  227.      * @return User
  228.      */
  229.     public function addOrganization(Organization $organization)
  230.     {
  231.         if (!$this->organizations->contains($organization)) {
  232.             $this->organizations->add($organization);
  233.         }
  234.         return $this;
  235.     }
  236.     /**
  237.      * @return User
  238.      */
  239.     public function removeOrganization(Organization $organization)
  240.     {
  241.         $this->organizations->removeElement($organization);
  242.         return $this;
  243.     }
  244.     /**
  245.      * @return string
  246.      */
  247.     public function getPhoneNumber()
  248.     {
  249.         return $this->phoneNumber;
  250.     }
  251.     /**
  252.      * @param string $phoneNumber
  253.      *
  254.      * @return User
  255.      */
  256.     public function setPhoneNumber($phoneNumber)
  257.     {
  258.         $this->phoneNumber str_replace(' '''$phoneNumber);
  259.         return $this;
  260.     }
  261.     /**
  262.      * @return string
  263.      */
  264.     public function getLastname()
  265.     {
  266.         return $this->lastname;
  267.     }
  268.     /**
  269.      * @param string $lastname
  270.      *
  271.      * @return User
  272.      */
  273.     public function setLastname($lastname)
  274.     {
  275.         $this->lastname $lastname;
  276.         return $this;
  277.     }
  278.     /**
  279.      * @return string
  280.      */
  281.     public function getFirstname()
  282.     {
  283.         return $this->firstname;
  284.     }
  285.     /**
  286.      * @param string $firstname
  287.      *
  288.      * @return User
  289.      */
  290.     public function setFirstname($firstname)
  291.     {
  292.         $this->firstname $firstname;
  293.         return $this;
  294.     }
  295.     /**
  296.      * A visual identifier that represents this user.
  297.      *
  298.      * @see UserInterface
  299.      */
  300.     public function getUsername()
  301.     {
  302.         return $this->email;
  303.     }
  304.     /**
  305.      * @see UserInterface
  306.      */
  307.     public function getRoles()
  308.     {
  309.         return $this->roles;
  310.     }
  311.     /**
  312.      * @return User
  313.      */
  314.     public function setRoles(array $roles)
  315.     {
  316.         $this->roles $roles;
  317.         return $this;
  318.     }
  319.     /**
  320.      * @return User
  321.      */
  322.     public function addRole(string $role)
  323.     {
  324.         $this->roles[] = $role;
  325.         return $this;
  326.     }
  327.     /**
  328.      * @see UserInterface
  329.      */
  330.     public function getPassword()
  331.     {
  332.         return $this->password;
  333.     }
  334.     /**
  335.      * @return User
  336.      */
  337.     public function setPassword(string $password)
  338.     {
  339.         $this->password $password;
  340.         return $this;
  341.     }
  342.     public function getCreatedAt(): \DateTimeInterface
  343.     {
  344.         return $this->createdAt;
  345.     }
  346.     /**
  347.      * @param \DateTime|\DateTimeImmutable $createdAt
  348.      *
  349.      * @return User
  350.      */
  351.     public function setCreatedAt(?\DateTimeInterface $createdAt)
  352.     {
  353.         $this->createdAt $createdAt;
  354.         return $this;
  355.     }
  356.     /**
  357.      * @see UserInterface
  358.      */
  359.     public function getSalt()
  360.     {
  361.         return null;
  362.     }
  363.     /**
  364.      * @see UserInterface
  365.      */
  366.     public function eraseCredentials()
  367.     {
  368.         // If you store any temporary, sensitive data on the user, clear it here
  369.     }
  370.     public function getRegistrationSource(): ?string
  371.     {
  372.         return $this->registrationSource;
  373.     }
  374.     public function setRegistrationSource(?string $registrationSource): self
  375.     {
  376.         $this->registrationSource $registrationSource;
  377.         return $this;
  378.     }
  379.     public function getHonorificPrefix(): ?string
  380.     {
  381.         return $this->honorificPrefix;
  382.     }
  383.     public function setHonorificPrefix(?string $honorificPrefix): self
  384.     {
  385.         $this->honorificPrefix $honorificPrefix;
  386.         return $this;
  387.     }
  388.     public function getResetToken(): ?string
  389.     {
  390.         return $this->resetToken;
  391.     }
  392.     public function setResetToken(?string $resetToken): self
  393.     {
  394.         $this->resetToken $resetToken;
  395.         return $this;
  396.     }
  397.     public function getResetTokenExpirationDate(): ?string
  398.     {
  399.         return $this->resetTokenExpirationDate;
  400.     }
  401.     public function setResetTokenExpirationDate(?string $resetTokenExpirationDate): self
  402.     {
  403.         $this->resetTokenExpirationDate $resetTokenExpirationDate;
  404.         return $this;
  405.     }
  406.     /**
  407.      * @return Store[]
  408.      */
  409.     public function getStores()
  410.     {
  411.         return $this->stores->getValues();
  412.     }
  413.     public function setStores(?Collection $stores): User
  414.     {
  415.         $this->stores $stores;
  416.         return $this;
  417.     }
  418.     /**
  419.      * @return User
  420.      */
  421.     public function addStore(Store $store)
  422.     {
  423.         if (!$this->stores->contains($store)) {
  424.             $this->stores->add($store);
  425.         }
  426.         return $this;
  427.     }
  428.     /**
  429.      * @return User
  430.      */
  431.     public function removeStore(Store $store)
  432.     {
  433.         $this->stores->removeElement($store);
  434.         return $this;
  435.     }
  436.     /**
  437.      * @return Collection|Invoice[]
  438.      */
  439.     public function getInvoices(): Collection
  440.     {
  441.         return $this->invoices;
  442.     }
  443.     public function addInvoice(Invoice $invoice): self
  444.     {
  445.         if (!$this->invoices->contains($invoice)) {
  446.             $this->invoices[] = $invoice;
  447.             $invoice->setUser($this);
  448.         }
  449.         return $this;
  450.     }
  451.     public function removeInvoice(Invoice $invoice): self
  452.     {
  453.         if ($this->invoices->contains($invoice)) {
  454.             $this->invoices->removeElement($invoice);
  455.             // set the owning side to null (unless already changed)
  456.             if ($invoice->getUser() === $this) {
  457.                 $invoice->setUser(null);
  458.             }
  459.         }
  460.         return $this;
  461.     }
  462.     /**
  463.      * @return Collection|UserUTM[]
  464.      */
  465.     public function getUTMs(): Collection
  466.     {
  467.         return $this->UTMs;
  468.     }
  469.     public function addUTM(UserUTM $uTM): self
  470.     {
  471.         if (!$this->UTMs->contains($uTM)) {
  472.             $this->UTMs[] = $uTM;
  473.             $uTM->setUser($this);
  474.         }
  475.         return $this;
  476.     }
  477.     public function removeUTM(UserUTM $uTM): self
  478.     {
  479.         if ($this->UTMs->contains($uTM)) {
  480.             $this->UTMs->removeElement($uTM);
  481.             // set the owning side to null (unless already changed)
  482.             if ($uTM->getUser() === $this) {
  483.                 $uTM->setUser(null);
  484.             }
  485.         }
  486.         return $this;
  487.     }
  488.     public function getUniqId(): ?string
  489.     {
  490.         if ($this->uniqId) {
  491.             return $this->uniqId;
  492.         }
  493.         return $this->id;
  494.     }
  495.     public function setUniqId(?string $uniqId): self
  496.     {
  497.         $this->uniqId $uniqId;
  498.         return $this;
  499.     }
  500.     public function getCodeConfirmation(): ?int
  501.     {
  502.         return $this->codeConfirmation;
  503.     }
  504.     public function setCodeConfirmation(?int $codeConfirmation): self
  505.     {
  506.         $this->codeConfirmation $codeConfirmation;
  507.         return $this;
  508.     }
  509.     /**
  510.      * @return Collection|Lead[]
  511.      */
  512.     public function getLeads(): Collection
  513.     {
  514.         return $this->leads;
  515.     }
  516.     public function addLead(Lead $lead): self
  517.     {
  518.         if (!$this->leads->contains($lead)) {
  519.             $this->leads[] = $lead;
  520.             $lead->setManager($this);
  521.         }
  522.         return $this;
  523.     }
  524.     public function removeLead(Lead $lead): self
  525.     {
  526.         if ($this->leads->contains($lead)) {
  527.             $this->leads->removeElement($lead);
  528.             // set the owning side to null (unless already changed)
  529.             if ($lead->getManager() === $this) {
  530.                 $lead->setManager(null);
  531.             }
  532.         }
  533.         return $this;
  534.     }
  535.     /**
  536.      * @return Collection|ActivityLog[]
  537.      */
  538.     public function getActivityLogs(): Collection
  539.     {
  540.         return $this->activityLogs;
  541.     }
  542.     public function addActivityLog(ActivityLog $activityLog): self
  543.     {
  544.         if (!$this->activityLogs->contains($activityLog)) {
  545.             $this->activityLogs[] = $activityLog;
  546.             $activityLog->setUser($this);
  547.         }
  548.         return $this;
  549.     }
  550.     public function removeActivityLog(ActivityLog $activityLog): self
  551.     {
  552.         // set the owning side to null (unless already changed)
  553.         if ($this->activityLogs->removeElement($activityLog) && $activityLog->getUser() === $this) {
  554.             $activityLog->setUser(null);
  555.         }
  556.         return $this;
  557.     }
  558.     public function getUpdatedAt(): ?\DateTimeInterface
  559.     {
  560.         return $this->updatedAt;
  561.     }
  562.     public function setUpdatedAt(?\DateTimeInterface $updatedAt): self
  563.     {
  564.         $this->updatedAt $updatedAt;
  565.         return $this;
  566.     }
  567.     public function getIsActive(): ?bool
  568.     {
  569.         return $this->isActive;
  570.     }
  571.     public function setIsActive(bool $isActive): self
  572.     {
  573.         $this->isActive $isActive;
  574.         return $this;
  575.     }
  576.     /**
  577.      * @return Collection|CommercialGesture[]
  578.      */
  579.     public function getCommercialGestures(): Collection
  580.     {
  581.         return $this->commercialGestures;
  582.     }
  583.     public function addCommercialGesture(CommercialGesture $commercialGesture): self
  584.     {
  585.         if (!$this->commercialGestures->contains($commercialGesture)) {
  586.             $this->commercialGestures[] = $commercialGesture;
  587.             $commercialGesture->setUser($this);
  588.         }
  589.         return $this;
  590.     }
  591.     public function removeCommercialGesture(CommercialGesture $commercialGesture): self
  592.     {
  593.         // set the owning side to null (unless already changed)
  594.         if ($this->commercialGestures->removeElement($commercialGesture) && $commercialGesture->getUser() === $this) {
  595.             $commercialGesture->setUser(null);
  596.         }
  597.         return $this;
  598.     }
  599.     public static function createFixture(array $data): self
  600.     {
  601.         $self = (new self());
  602.         $self->email $data['email'];
  603.         $self->firstname $data['firstname'] ?: null;
  604.         $self->lastname $data['lastname'] ?: null;
  605.         $self->phoneNumber $data['phoneNumber'];
  606.         $self->password $data['password'] ?? '123soleil';
  607.         $self->roles $data['roles'] ?? ['ROLE_USER'];
  608.         $self->uniqId $data['uniqId'] ?? uniqid('test'true);
  609.         return $self;
  610.     }
  611.     public function getPappersActivityLogWrappers(): Collection
  612.     {
  613.         return $this->pappersActivityLogWrappers;
  614.     }
  615.     public function addPappersActivityLogWrapper(PappersActivityLogWrapper $pappersActivityLogWrapper): self
  616.     {
  617.         if (!$this->pappersActivityLogWrappers->contains($pappersActivityLogWrapper)) {
  618.             $this->pappersActivityLogWrappers[] = $pappersActivityLogWrapper;
  619.             $pappersActivityLogWrapper->setUser($this);
  620.         }
  621.         return $this;
  622.     }
  623.     public function removePappersActivityLogWrapper(PappersActivityLogWrapper $pappersActivityLogWrapper): self
  624.     {
  625.         if ($this->pappersActivityLogWrappers->contains($pappersActivityLogWrapper)) {
  626.             $this->pappersActivityLogWrappers->removeElement($pappersActivityLogWrapper);
  627.             // set the owning side to null (unless already changed)
  628.             if ($pappersActivityLogWrapper->getUser() === $this) {
  629.                 $pappersActivityLogWrapper->setUser(null);
  630.             }
  631.         }
  632.         return $this;
  633.     }
  634. }