← Back
Editing: User.php
<?php namespace App\Entity; use App\Contract\PersistableUserInterface; use App\Entity\Traits\ActivableTrait; use App\Enum\ReportType; use Doctrine\Common\Collections\ArrayCollection; use Doctrine\Common\Collections\Collection; use Doctrine\Common\Collections\Criteria; use Doctrine\ORM\Mapping as ORM; use Lexik\Bundle\JWTAuthenticationBundle\Security\User\JWTUser; use Lexik\Bundle\JWTAuthenticationBundle\Security\User\JWTUserInterface; use Symfony\Bridge\Doctrine\Validator\Constraints\UniqueEntity; use Symfony\Component\Security\Core\User\PasswordAuthenticatedUserInterface; use Symfony\Component\Serializer\Annotation\Groups; use Symfony\Component\Serializer\Annotation\SerializedName; use Symfony\Component\Validator\Constraints as Assert; #[ORM\Table(name: 'user')] #[ORM\Entity(repositoryClass: 'App\Repository\UserRepository')] #[ORM\HasLifecycleCallbacks] #[UniqueEntity('email')] class User implements PersistableUserInterface, PasswordAuthenticatedUserInterface, JWTUserInterface { use ActivableTrait; #[ORM\Id] #[ORM\GeneratedValue] #[ORM\Column(type: 'integer')] #[Groups(['session', 'show_user', 'supervisor_agent', 'supervisor_base', 'organization_base'])] protected ?int $id = null; #[ORM\Column(type: 'string', length: 180)] #[Groups(['show_user', 'supervisor_agent'])] private ?string $username = null; #[ORM\Column(type: 'string', length: 180)] #[Groups(['show_user'])] private ?string $usernameCanonical = null; #[ORM\Column(type: 'string', length: 180, unique: true)] #[Assert\Email] #[Groups(['show_user', 'supervisor_agent'])] private ?string $email = null; #[ORM\Column(type: 'string', length: 180)] #[Groups(['show_user'])] private ?string $emailCanonical = null; #[ORM\Column(type: 'boolean', length: 180)] #[Groups(['show_user'])] private bool $enabled = true; #[ORM\Column(type: 'string', nullable: true)] #[Groups(['show_user'])] private ?string $salt = null; #[ORM\Column(type: 'string')] private ?string $password = null; /** * @var \DateTime|null */ #[ORM\Column(type: 'datetime', nullable: true)] #[Groups(['show_user'])] private ?\DateTimeInterface $lastLogin = null; #[ORM\Column(type: 'string', length: 180, nullable: true)] #[Groups(['show_user'])] private ?string $confirmationToken = null; /** * @var \DateTime|null */ #[ORM\Column(type: 'datetime', nullable: true)] #[Groups(['show_user'])] private ?\DateTimeInterface $passwordRequestedAt = null; #[ORM\Column(type: 'text', nullable: true)] #[Groups(['show_user'])] private ?string $roles; #[SerializedName('array_roles')] #[Groups(['show_user'])] private array $arrayRoles = []; #[ORM\Column(name: 'nom', type: 'string', length: 255, nullable: true)] #[Groups(['show_user', 'supervisor_agent', 'supervisor_base', 'manager', 'organization_base'])] private ?string $nom = null; #[ORM\Column(name: 'prenom', type: 'string', length: 255, nullable: true)] #[Groups(['show_user', 'supervisor_agent', 'supervisor_base', 'manager', 'organization_base'])] private ?string $prenom = null; #[ORM\Column(type: 'boolean', nullable: true)] #[Groups(['show_user'])] protected ?bool $superAdmin = null; /** * @deprecated remove for agentFrequencies */ #[ORM\Column(type: 'integer', nullable: true)] #[Assert\Type('integer')] private ?int $objectifControle = null; #[ORM\ManyToOne(targetEntity: Agency::class, inversedBy: 'users')] #[ORM\JoinColumn(onDelete: 'CASCADE')] #[Groups(['show_user'])] private ?Agency $agency = null; /** * @deprecated * * @var ?Agency */ #[Groups(['show_user'])] private ?Agency $client = null; #[ORM\ManyToOne(targetEntity: 'App\Entity\Organization', inversedBy: 'users')] #[Groups(['show_user'])] private ?Organization $organization = null; /** * @var Collection<array-key, Controle> */ #[ORM\OneToMany(mappedBy: 'agent', targetEntity: 'App\Entity\Controle')] private Collection $controles; /** * @deprecated remove for agentFrequencies */ #[ORM\JoinColumn(name: 'frequence_controle', referencedColumnName: 'id', nullable: true)] #[ORM\ManyToOne(targetEntity: 'Periode')] private ?Periode $frequenceControle = null; #[ORM\Column(name: 'rappel', type: 'integer', nullable: true)] #[Assert\Type('integer')] #[Groups(['show_user'])] private ?int $rappel = null; /** * @var ?string */ private ?string $plainPassword = null; #[ORM\Column(type: 'datetime', options: ['default' => 'CURRENT_TIMESTAMP'])] private ?\DateTimeInterface $updatedAt; #[ORM\ManyToOne(targetEntity: 'App\Entity\User', inversedBy: 'agents')] #[ORM\JoinColumn(nullable: true, onDelete: 'SET NULL')] private ?User $manager = null; /** * @var Collection<array-key, User> */ #[ORM\OneToMany(mappedBy: 'manager', targetEntity: 'App\Entity\User')] #[ORM\OrderBy(['nom' => 'ASC'])] private Collection $agents; /** * @var Collection<array-key, Rappel> */ #[ORM\OneToMany(mappedBy: 'agent', targetEntity: Rappel::class)] private Collection $rappels; /** * @var Collection<array-key, AgentFrequency> */ #[ORM\OneToMany(mappedBy: 'agent', targetEntity: AgentFrequency::class, cascade: ['persist'])] private Collection $agentFrequencies; /** * @var Collection<array-key, Solution> */ #[ORM\ManyToMany(targetEntity: Solution::class, inversedBy: 'users')] private Collection $solutions; /** * @var Collection<array-key, Picture> */ #[ORM\OneToMany(mappedBy: 'agent', targetEntity: Picture::class)] private Collection $pictures; /** * @var Collection<array-key, Adresse> */ #[ORM\OneToMany(mappedBy: 'manager', targetEntity: Adresse::class, cascade: ['persist', 'remove'])] private Collection $managerAddresses; /** * @var Collection<array-key, Adresse> */ #[ORM\ManyToMany(targetEntity: Adresse::class, inversedBy: 'usersAccess', cascade: ['persist', 'remove'])] private Collection $addressesAccess; /** * @var Collection<array-key, UserAddress> */ #[ORM\OneToMany(mappedBy: 'user', targetEntity: UserAddress::class, cascade: ['persist', 'remove'])] private Collection $userAddresses; #[ORM\Column(type: 'string', nullable: true)] private ?string $color = null; /** * @var Collection<array-key, Report> */ #[ORM\OneToMany(mappedBy: 'agent', targetEntity: Report::class, cascade: ['persist', 'remove'])] private Collection $reports; /** * @var Collection<array-key, Report> */ #[ORM\OneToMany(mappedBy: 'manager', targetEntity: Report::class, cascade: ['persist', 'remove'])] private Collection $managerReports; /** * @var Collection<array-key, ReportPicture> */ #[ORM\OneToMany(mappedBy: 'owner', targetEntity: ReportPicture::class)] private Collection $reportPictures; /** * @var array<int, ?AgentFrequency> */ private array $frequencyBySolution = []; #[ORM\OneToMany(mappedBy: 'user', targetEntity: UserEmailAlias::class, cascade: ['persist'])] private Collection $emailAliases; public const ROLES = [ 'Locataire' => 'ROLE_TENANT', 'Agent' => 'ROLE_AGENT', 'Manager' => 'ROLE_MANAGER', 'Supervisor' => 'ROLE_SUPERVISOR', 'Organisme' => 'ROLE_ORGANIZATION', 'Admin' => 'ROLE_ADMIN', ]; public function __construct() { $this->roles = serialize([]); $this->controles = new ArrayCollection(); $this->updatedAt = new \DateTime(); $this->agents = new ArrayCollection(); $this->rappels = new ArrayCollection(); $this->agentFrequencies = new ArrayCollection(); $this->solutions = new ArrayCollection(); $this->managerAddresses = new ArrayCollection(); $this->pictures = new ArrayCollection(); $this->addressesAccess = new ArrayCollection(); $this->userAddresses = new ArrayCollection(); $this->reports = new ArrayCollection(); $this->managerReports = new ArrayCollection(); $this->reportPictures = new ArrayCollection(); $this->emailAliases = new ArrayCollection(); } public function getId(): ?int { return $this->id; } public function setId($id): self { $this->id = $id; return $this; } public function __toString() { return $this->prenom.' '.$this->nom; } public function getNom(): ?string { return $this->nom; } /** * @return $this */ public function setNom(?string $nom): self { $this->nom = $nom; return $this; } public function getPrenom(): ?string { return $this->prenom; } /** * @return $this */ public function setPrenom(?string $prenom): self { $this->prenom = $prenom; return $this; } public function getSuperAdmin(): ?bool { return $this->superAdmin; } public function setSuperAdmin(bool $superAdmin): self { $this->superAdmin = $superAdmin; return $this; } /** * @deprecated remove for getAgentFrequencies */ public function getFrequenceControle(): ?Periode { return $this->frequenceControle; } /** * @return $this * * @deprecated remove for addAgentFrequencies */ public function setFrequenceControle(Periode $frequenceControle): self { $this->frequenceControle = $frequenceControle; return $this; } public function getAgency(): ?Agency { return $this->agency; } /** * @return $this */ public function setAgency(?Agency $agency): self { $this->agency = $agency; return $this; } /** * @return $this */ public function setClient(?Agency $agency): self { $this->client = $agency; return $this; } public function getClient(): ?Agency { return $this->client; } public function getOrganization(): ?Organization { return $this->organization; } /** * @return $this */ public function setOrganization(?Organization $organization): self { $this->organization = $organization; return $this; } /** * @deprecated remove for agentFrequencies */ public function getObjectifControle(): ?int { return $this->objectifControle; } /** * @return $this * * @deprecated remove for agentFrequencies */ public function setObjectifControle(?int $objectifControle): self { $this->objectifControle = $objectifControle; return $this; } public function getRappel(): ?int { return $this->rappel; } public function setRappel(?int $rappel): self { $this->rappel = $rappel; return $this; } public function getUsername(): ?string { return $this->username; } public function setUsername(string $username): self { $this->username = $username; $this->usernameCanonical = strtolower($username); return $this; } public function getEmail(): ?string { return $this->email; } public function setEmail(string $email): self { $this->email = $email; $this->emailCanonical = strtolower($email); return $this; } public function getEmailCanonical(): ?string { return $this->emailCanonical; } public function setEmailCanonical(string $emailCanonical): self { $this->emailCanonical = $emailCanonical; return $this; } public function getEnabled(): ?bool { return $this->enabled; } public function setEnabled(bool $enabled): self { $this->enabled = $enabled; return $this; } public function getSalt(): ?string { return $this->salt; } public function setSalt(?string $salt): self { $this->salt = $salt; return $this; } public function getPassword(): ?string { return $this->password; } public function setPassword(string $password): self { $this->password = $password; return $this; } public function getLastLogin(): ?\DateTimeInterface { return $this->lastLogin; } public function setLastLogin(?\DateTimeInterface $lastLogin): self { $this->lastLogin = $lastLogin; return $this; } public function getConfirmationToken(): ?string { return $this->confirmationToken; } public function setConfirmationToken(?string $confirmationToken): self { $this->confirmationToken = $confirmationToken; return $this; } public function getPasswordRequestedAt(): ?\DateTimeInterface { return $this->passwordRequestedAt; } public function setPasswordRequestedAt(?\DateTimeInterface $passwordRequestedAt): self { $this->passwordRequestedAt = $passwordRequestedAt; return $this; } public function getRoles(): array { return unserialize($this->roles); } public function setRoles(?array $roles): self { $this->roles = serialize($roles); return $this; } public function hasRole(string $role): bool { return in_array($role, $this->getRoles(), true); } public function getUsernameCanonical(): ?string { return $this->usernameCanonical; } public function setUsernameCanonical(string $usernameCanonical): self { $this->usernameCanonical = $usernameCanonical; return $this; } public function eraseCredentials(): void { // TODO: Implement eraseCredentials() method. } public function getPlainPassword(): ?string { return $this->plainPassword; } public function setPlainPassword(?string $plainPassword): self { $this->plainPassword = $plainPassword; return $this; } public function isEnabled(): bool { return $this->enabled; } public function getControles(): Collection { return $this->controles; } public function addControle(Controle $controle): self { if (!$this->controles->contains($controle)) { $this->controles[] = $controle; $controle->setAgent($this); } return $this; } public function removeControle(Controle $controle): self { if ($this->controles->contains($controle)) { $this->controles->removeElement($controle); // set the owning side to null (unless already changed) if ($controle->getAgent() === $this) { $controle->setAgent(null); } } return $this; } public function getUpdatedAt(): ?\DateTimeInterface { return $this->updatedAt; } public function setUpdatedAt(\DateTimeInterface $updatedAt): self { $this->updatedAt = $updatedAt; return $this; } public function getManager(): ?self { return $this->manager; } public function setManager(?self $manager): self { $this->manager = $manager; return $this; } public function getAgents(): Collection { return $this->agents; } public function addAgent(User $agent): self { if (!$this->agents->contains($agent)) { $this->agents[] = $agent; $agent->setManager($this); } return $this; } public function removeAgent(User $agent): self { if ($this->agents->contains($agent)) { $this->agents->removeElement($agent); // set the owning side to null (unless already changed) if ($agent->getManager() === $this) { $agent->setManager(null); } } return $this; } public function getRappels(): Collection { return $this->rappels; } public function addRappel(Rappel $rappel): self { if (!$this->rappels->contains($rappel)) { $this->rappels[] = $rappel; $rappel->setAgent($this); } return $this; } public function getAgentFrequencies(): Collection { return $this->agentFrequencies; } public function addAgentFrequency(AgentFrequency $agentFrequency): self { if (!$this->agentFrequencies->contains($agentFrequency)) { $this->agentFrequencies[] = $agentFrequency; $agentFrequency->setAgent($this); } return $this; } public function removeAgentFrequency(AgentFrequency $agentFrequency): self { if ($this->agentFrequencies->contains($agentFrequency)) { $this->agentFrequencies->removeElement($agentFrequency); // set the owning side to null (unless already changed) if ($agentFrequency->getAgent() === $this) { $agentFrequency->setAgent(null); } } return $this; } public function getSolutions(): Collection { return $this->solutions; } /** * @return $this */ public function addSolution(Solution $solution): self { if (!$this->solutions->contains($solution)) { $this->solutions[] = $solution; } return $this; } /** * @return $this */ public function removeSolution(Solution $solution): self { if ($this->solutions->contains($solution)) { $this->solutions->removeElement($solution); } return $this; } public function hasSolution(Solution $solution): bool { foreach ($this->getSolutions() as $item) { if ($solution->getId() === $item->getId()) { return true; } } return false; } public static function createFromPayload($username, array $payload): JWTUserInterface { return new JWTUser($username); } #[ORM\PostLoad] public function fillArrayRoles(): void { $this->arrayRoles = unserialize($this->roles); } public function setArrayRoles($roles): void { $this->arrayRoles = $roles; } #[Groups(['show_user'])] public function getArrayRoles(): array { return $this->arrayRoles; } /** * @deprecated Compat with app, use getLastControlDate */ #[SerializedName('last_control_date')] #[Groups(['show_user'])] public function getLastControlDateCompat(): ?\DateTimeInterface { return $this->getLastControlDate(); } #[Groups(['show_user'])] public function getLastControlDate(): ?\DateTimeInterface { /** @var Controle $lastControl */ $criteria = Criteria::create() ->where(Criteria::expr()->eq('valide', true)) ->orderBy(['createdAt' => 'ASC'] ); $controls = $this->controles->matching($criteria); if ($controls->count() > 0) { return $controls->last()->getCreatedAt(); } return null; } public function getPictures(): Collection { return $this->pictures; } public function addPicture(Picture $picture): self { if (!$this->pictures->contains($picture)) { $this->pictures[] = $picture; $picture->setAgent($this); } return $this; } public function removePicture(Picture $picture): self { if ($this->pictures->removeElement($picture)) { // set the owning side to null (unless already changed) if ($picture->getAgent() === $this) { $picture->setAgent(null); } } return $this; } public function getAddressesAccess(): Collection { return $this->addressesAccess; } public function addAddressesAccess(Adresse $addressesAccess): self { if (!$this->addressesAccess->contains($addressesAccess)) { $this->addressesAccess[] = $addressesAccess; } return $this; } public function removeAddressesAccess(Adresse $addressesAccess): self { $this->addressesAccess->removeElement($addressesAccess); return $this; } public function getUserAddresses(): Collection { return $this->userAddresses; } public function addUserAddress(UserAddress $userAddress): self { if (!$this->userAddresses->contains($userAddress)) { $this->userAddresses[] = $userAddress; $userAddress->setUser($this); } return $this; } public function removeUserAddress(UserAddress $userAddress): self { if ($this->userAddresses->removeElement($userAddress)) { // set the owning side to null (unless already changed) if ($userAddress->getUser() === $this) { $userAddress->setUser(null); } } return $this; } /** * @return Collection<array-key, Adresse> */ public function getManagerAddresses(): Collection { return $this->managerAddresses; } public function addManagerAddress(Adresse $managerAddress): self { if (!$this->managerAddresses->contains($managerAddress)) { $this->managerAddresses[] = $managerAddress; $managerAddress->setManager($this); } return $this; } /** * @return $this */ public function removeManagerAddress(Adresse $managerAddress): self { if ($this->managerAddresses->removeElement($managerAddress)) { // set the owning side to null (unless already changed) if ($managerAddress->getManager() === $this) { $managerAddress->setManager(null); } } return $this; } public function getControlsWithDates(?\DateTimeInterface $startDate = null, ?\DateTimeInterface $endDate = null) { $criteria = new Criteria(); if ($startDate) { $criteria->andWhere(Criteria::expr()->gte('dateRealisation', $startDate)); } if ($endDate) { $criteria->andWhere(Criteria::expr()->lte('dateRealisation', $endDate)); } return $this->controles->matching($criteria); } public function getReportsWithDates( ReportType $reportType, ?\DateTime $startDate = null, ?\DateTime $endDate = null ) { $criteria = new Criteria(); if ($startDate) { $criteria->andWhere(Criteria::expr()->gte('createdAt', $startDate)); } if ($endDate) { $criteria->andWhere(Criteria::expr()->lte('createdAt', $endDate)); } $criteria->andWhere(Criteria::expr()->eq('type', $reportType->value)); return $this->reports->matching($criteria); } public function isSuperAdmin(): ?bool { return $this->superAdmin; } public function getUserIdentifier(): string { return (string) $this->email; } public function removeRappel(Rappel $rappel): self { if ($this->rappels->removeElement($rappel)) { // set the owning side to null (unless already changed) if ($rappel->getAgent() === $this) { $rappel->setAgent(null); } } return $this; } public function getColor(): ?string { return $this->color; } /** * @return $this */ public function setColor(?string $color): self { $this->color = $color; return $this; } public function getReports(): Collection { return $this->reports; } public function addReport(Report $report): self { if (!$this->reports->contains($report)) { $this->reports[] = $report; $report->setManager($this); } return $this; } /** * @return $this */ public function removeReport(Report $report): self { if ($this->reports->removeElement($report)) { // set the owning side to null (unless already changed) if ($report->getManager() === $this) { $report->setManager(null); } } return $this; } public function getManagerReports(): Collection { return $this->managerReports; } public function addManagerReport(Report $managerReport): self { if (!$this->managerReports->contains($managerReport)) { $this->managerReports[] = $managerReport; $managerReport->setManager($this); } return $this; } /** * @return $this */ public function removeManagerReport(Report $managerReport): self { if ($this->managerReports->removeElement($managerReport)) { // set the owning side to null (unless already changed) if ($managerReport->getManager() === $this) { $managerReport->setManager(null); } } return $this; } public function getReportPictures(): Collection { return $this->reportPictures; } public function addReportPicture(ReportPicture $reportPicture): self { if (!$this->reportPictures->contains($reportPicture)) { $this->reportPictures[] = $reportPicture; $reportPicture->setOwner($this); } return $this; } public function removeReportPicture(ReportPicture $reportPicture): self { if ($this->reportPictures->removeElement($reportPicture)) { // set the owning side to null (unless already changed) if ($reportPicture->getOwner() === $this) { $reportPicture->setOwner(null); } } return $this; } public function getAgentFrequencyBySolution(Solution $solution): ?AgentFrequency { $solutionId = (int) $solution->getId(); if (!isset($this->frequencyBySolution[$solutionId])) { $criteria = Criteria::create() ->where(Criteria::expr()->eq('solution', $solution)) ->setMaxResults(1); $frequencies = $this->agentFrequencies->matching($criteria); if (0 === $frequencies->count()) { $this->frequencyBySolution[$solutionId] = null; return null; } $this->frequencyBySolution[$solutionId] = $frequencies->first(); } return $this->frequencyBySolution[$solutionId]; } /** * @return Collection<int, UserEmailAlias> */ public function getEmailAliases(): Collection { return $this->emailAliases; } public function addEmailAlias(UserEmailAlias $emailAlias): static { if (!$this->emailAliases->contains($emailAlias)) { $this->emailAliases->add($emailAlias); $emailAlias->setUser($this); } return $this; } public function removeEmailAlias(UserEmailAlias $emailAlias): static { if ($this->emailAliases->removeElement($emailAlias)) { // set the owning side to null (unless already changed) if ($emailAlias->getUser() === $this) { $emailAlias->setUser(null); } } return $this; } #[Groups(['show_user'])] public function getAppPermissions(): Collection { if (in_array('ROLE_ORGANIZATION', $this->arrayRoles)) { $permissions = new ArrayCollection(); foreach ($this->getOrganization()?->getAgencies() as $agency) { foreach ($agency->getPermissions() as $agencyPermission) { if (!$permissions->contains($agencyPermission)) { $permissions->add($agencyPermission); } } } return $permissions; } return new ArrayCollection($this->getAgency()?->getPermissions()); } /** * @return list<?string> */ public function getEmailAliasValues(): array { return array_values($this->emailAliases->map(fn (UserEmailAlias $alias) => $alias->getEmail())->toArray()); } }
Save File
Cancel