src/Entity/User.php line 22

Open in your IDE?
  1. <?php
  2. namespace App\Entity;
  3. use App\Entity\Report\Report;
  4. use App\Entity\Roles\RoleGroup;
  5. use App\Entity\Task\MonitoringObjective;
  6. use App\Entity\Task\Objective;
  7. use App\Repository\UserRepository;
  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\PasswordAuthenticatedUserInterface;
  13. use Symfony\Component\Security\Core\User\UserInterface;
  14. /**
  15.  * @ORM\Entity(repositoryClass=UserRepository::class)
  16.  * @ORM\Table(name="`user`")
  17.  * @UniqueEntity(fields={"email"}, message="There is already an account with this email")
  18.  */
  19. class User implements UserInterfacePasswordAuthenticatedUserInterface
  20. {
  21.     /**
  22.      * @ORM\Id
  23.      * @ORM\GeneratedValue
  24.      * @ORM\Column(type="integer")
  25.      */
  26.     private $id;
  27.     /**
  28.      * @ORM\Column(type="string", length=180, unique=true)
  29.      */
  30.     private $email;
  31.     /**
  32.      * @ORM\Column(type="json")
  33.      */
  34.     private $roles = [];
  35.     /**
  36.      * @var string The hashed password
  37.      * @ORM\Column(type="string")
  38.      */
  39.     private $password;
  40.     /**
  41.      * @ORM\Column(type="boolean")
  42.      */
  43.     private $isVerified false;
  44.     /**
  45.      * @ORM\Column(type="datetime", nullable=true)
  46.      */
  47.     private $lastConnect;
  48.     /**
  49.      * @ORM\Column(type="string", length=255, nullable=true)
  50.      */
  51.     private $phone;
  52.     /**
  53.      * @ORM\Column(type="string", length=255, nullable=true)
  54.      */
  55.     private $hashTelegram;
  56.     /**
  57.      * @ORM\OneToOne(targetEntity=Employee::class, mappedBy="user", cascade={"persist", "remove"})
  58.      */
  59.     private $employee;
  60.     /**
  61.      * @ORM\ManyToOne(targetEntity=Company::class, inversedBy="user", cascade={"persist", "remove"})
  62.      */
  63.     private $company;
  64.     /**
  65.      * @ORM\Column(type="json", nullable=true)
  66.      */
  67.     private $lastCoordinate = [];
  68.     /**
  69.      * @ORM\Column(type="string", length=255, nullable=true)
  70.      */
  71.     private $hashSync;
  72.     /**
  73.      * @ORM\ManyToOne(targetEntity=RoleGroup::class, inversedBy="users")
  74.      */
  75.     private $roleGroup;
  76.     /**
  77.      * @ORM\ManyToOne(targetEntity=CompanyGroup::class, inversedBy="users")
  78.      */
  79.     private $companyGroup;
  80.     /**
  81.      * @ORM\OneToMany(targetEntity=Objective::class, mappedBy="owner")
  82.      */
  83.     private $ownerObjectives;
  84.     /**
  85.      * @ORM\OneToMany(targetEntity=Objective::class, mappedBy="viewer")
  86.      */
  87.     private $viewerObjectives;
  88.     /**
  89.      * @ORM\OneToMany(targetEntity=MonitoringObjective::class, mappedBy="creator")
  90.      */
  91.     private $monitoringObjectives;
  92.     /**
  93.      * @ORM\OneToMany(targetEntity=Objective::class, mappedBy="creator")
  94.      */
  95.     private $creatorObjectives;
  96.     /**
  97.      * @ORM\OneToMany(targetEntity=Employee::class, mappedBy="creator")
  98.      */
  99.     private $employees;
  100.     /**
  101.      * @ORM\Column(type="boolean", nullable=true)
  102.      */
  103.     private $dashboard;
  104.  
  105.     public function __construct()
  106.     {
  107.         if(is_null($this->id)) {
  108.             $this->roles[] = 'ROLE_USER';
  109.             $this->dashboard false;
  110.         }
  111.         $this->reports = new ArrayCollection();
  112.         $this->ownerObjectives = new ArrayCollection();
  113.         $this->viewerObjectives = new ArrayCollection();
  114.         $this->monitoringObjectives = new ArrayCollection();
  115.         $this->creatorObjectives = new ArrayCollection();
  116.         $this->employees = new ArrayCollection();
  117.     }
  118.     public function getId(): ?int
  119.     {
  120.         return $this->id;
  121.     }
  122.     public function getEmail(): ?string
  123.     {
  124.         return $this->email;
  125.     }
  126.     public function setEmail(string $email): self
  127.     {
  128.         $this->email $email;
  129.         return $this;
  130.     }
  131.     /**
  132.      * A visual identifier that represents this user.
  133.      *
  134.      * @see UserInterface
  135.      */
  136.     public function getUserIdentifier(): string
  137.     {
  138.         return (string) $this->email;
  139.     }
  140.     /**
  141.      * @deprecated since Symfony 5.3, use getUserIdentifier instead
  142.      */
  143.     public function getUsername(): string
  144.     {
  145.         if(!empty($this->getEmployee())){
  146.             $name $this->getEmployee()->getName(). " ".$this->getEmployee()->getLastName();
  147.             return (string)$name;
  148.         }
  149.         return (string) $this->email;
  150.     }
  151.     /**
  152.      * @see UserInterface
  153.      */
  154.     public function getRoles(): array
  155.     {
  156.         $roles $this->roles;
  157.         if($this->getEmployee() && $this->getEmployee()->getRole() ){
  158.             $roles[] = $this->getEmployee()->getRole();
  159.         } else {
  160.             if ($this->getCompany() && $this->getCompany()->getRole()) {
  161.                 $roles[] = $this->getCompany()->getRole();
  162.             }
  163.         }
  164.         if(!empty($this->getRoleGroup())) {
  165.             $roles[] = $this->getRoleGroup()->getRole();
  166.         }
  167.          return array_unique($roles);
  168.     }
  169.     public function setRoles(array $roles): self
  170.     {
  171.         $this->roles $roles;
  172.         return $this;
  173.     }
  174.     /**
  175.      * @see PasswordAuthenticatedUserInterface
  176.      */
  177.     public function getPassword(): string
  178.     {
  179.         return $this->password;
  180.     }
  181.     public function setPassword(string $password): self
  182.     {
  183.         $this->password $password;
  184.         return $this;
  185.     }
  186.     /**
  187.      * Returning a salt is only needed, if you are not using a modern
  188.      * hashing algorithm (e.g. bcrypt or sodium) in your security.yaml.
  189.      *
  190.      * @see UserInterface
  191.      */
  192.     public function getSalt(): ?string
  193.     {
  194.         return null;
  195.     }
  196.     /**
  197.      * @see UserInterface
  198.      */
  199.     public function eraseCredentials()
  200.     {
  201.         // If you store any temporary, sensitive data on the user, clear it here
  202.         // $this->plainPassword = null;
  203.     }
  204.     public function isVerified(): bool
  205.     {
  206.         return $this->isVerified;
  207.     }
  208.     public function setIsVerified(bool $isVerified): self
  209.     {
  210.         $this->isVerified $isVerified;
  211.         return $this;
  212.     }
  213.     public function getLastConnect(): ?\DateTimeInterface
  214.     {
  215.         return $this->lastConnect;
  216.     }
  217.     public function setLastConnect()
  218.     {
  219.         $this->lastConnect = new \DateTime();
  220.         return $this;
  221.     }
  222.     public function getPhone(): ?string
  223.     {
  224.         return $this->phone;
  225.     }
  226.     public function setPhone(?string $phone): self
  227.     {
  228.         $this->phone $phone;
  229.         return $this;
  230.     }
  231.     public function isSuperAdmin()
  232.     {
  233.         return in_array('ROLE_SUPER_ADMIN',$this->getRoles());
  234.     }
  235.     public function isAdmin()
  236.     {
  237.         return in_array('ROLE_ADMIN',$this->getRoles());
  238.     }
  239.     public function getCompanyId(): ?int
  240.     {
  241.         if($this->isSuperAdmin()) {
  242.             return 0;
  243.         }
  244.         if(!empty($this->getCompany())) {
  245.             return $this->getCompany()->getId();
  246.         }
  247.         return null;
  248.     }
  249.     public function getEmployee(): ?Employee
  250.     {
  251.         return $this->employee;
  252.     }
  253.     public function setEmployee(?Employee $employee): self
  254.     {
  255.         // unset the owning side of the relation if necessary
  256.         if ($employee === null && $this->employee !== null) {
  257.             $this->employee->setUser(null);
  258.         }
  259.         // set the owning side of the relation if necessary
  260.         if ($employee !== null && $employee->getUser() !== $this) {
  261.             $employee->setUser($this);
  262.         }
  263.         $this->employee $employee;
  264.         return $this;
  265.     }
  266.     public function getCompany(): ?Company
  267.     {
  268.         return $this->company;
  269.     }
  270.     public function setCompany(?Company $company): self
  271.     {
  272.         $this->company $company;
  273.         return $this;
  274.     }
  275.     public function getLastCoordinate(): ?array
  276.     {
  277.         return $this->lastCoordinate;
  278.     }
  279.     public function setLastCoordinate(?array $lastCoordinate): self
  280.     {
  281.         $this->lastCoordinate $lastCoordinate;
  282.         return $this;
  283.     }
  284.     public function getHashSync(): ?string
  285.     {
  286.         return $this->hashSync;
  287.     }
  288.     public function setHashSync(?string $hashSync): self
  289.     {
  290.         $this->hashSync $hashSync;
  291.         return $this;
  292.     }
  293.     public function getRoleGroup(): ?RoleGroup
  294.     {
  295.         return $this->roleGroup;
  296.     }
  297.     public function setRoleGroup(?RoleGroup $roleGroup): self
  298.     {
  299.         $this->roleGroup $roleGroup;
  300.         return $this;
  301.     }
  302.     public function getCompanyGroup(): ?CompanyGroup
  303.     {
  304.         if(!empty($this->getCompany())){
  305.             return $this->getCompany()->getCompanyGroup();
  306.         }
  307.         return $this->companyGroup;
  308.     }
  309.     public function setCompanyGroup(?CompanyGroup $companyGroup): self
  310.     {
  311.         $this->companyGroup $companyGroup;
  312.         return $this;
  313.     }
  314.     /**
  315.      * @return Collection<int, Objective>
  316.      */
  317.     public function getOwnerObjectives(): Collection
  318.     {
  319.         return $this->ownerObjectives;
  320.     }
  321.     public function addOwnerObjective(Objective $ownerObjective): self
  322.     {
  323.         if (!$this->ownerObjectives->contains($ownerObjective)) {
  324.             $this->ownerObjectives[] = $ownerObjective;
  325.             $ownerObjective->setOwner($this);
  326.         }
  327.         return $this;
  328.     }
  329.     public function removeOwnerObjective(Objective $ownerObjective): self
  330.     {
  331.         if ($this->ownerObjectives->removeElement($ownerObjective)) {
  332.             // set the owning side to null (unless already changed)
  333.             if ($ownerObjective->getOwner() === $this) {
  334.                 $ownerObjective->setOwner(null);
  335.             }
  336.         }
  337.         return $this;
  338.     }
  339.     /**
  340.      * @return Collection<int, Objective>
  341.      */
  342.     public function getViewerObjectives(): Collection
  343.     {
  344.         return $this->viewerObjectives;
  345.     }
  346.     public function addViewerObjective(Objective $viewerObjective): self
  347.     {
  348.         if (!$this->viewerObjectives->contains($viewerObjective)) {
  349.             $this->viewerObjectives[] = $viewerObjective;
  350.             $viewerObjective->setViewer($this);
  351.         }
  352.         return $this;
  353.     }
  354.     public function removeViewerObjective(Objective $viewerObjective): self
  355.     {
  356.         if ($this->viewerObjectives->removeElement($viewerObjective)) {
  357.             // set the owning side to null (unless already changed)
  358.             if ($viewerObjective->getViewer() === $this) {
  359.                 $viewerObjective->setViewer(null);
  360.             }
  361.         }
  362.         return $this;
  363.     }
  364.     /**
  365.      * @return Collection<int, MonitoringObjective>
  366.      */
  367.     public function getMonitoringObjectives(): Collection
  368.     {
  369.         return $this->monitoringObjectives;
  370.     }
  371.     public function addMonitoringObjective(MonitoringObjective $monitoringObjective): self
  372.     {
  373.         if (!$this->monitoringObjectives->contains($monitoringObjective)) {
  374.             $this->monitoringObjectives[] = $monitoringObjective;
  375.             $monitoringObjective->setCreator($this);
  376.         }
  377.         return $this;
  378.     }
  379.     public function removeMonitoringObjective(MonitoringObjective $monitoringObjective): self
  380.     {
  381.         if ($this->monitoringObjectives->removeElement($monitoringObjective)) {
  382.             // set the owning side to null (unless already changed)
  383.             if ($monitoringObjective->getCreator() === $this) {
  384.                 $monitoringObjective->setCreator(null);
  385.             }
  386.         }
  387.         return $this;
  388.     }
  389.     /**
  390.      * @return Collection<int, Objective>
  391.      */
  392.     public function getCreatorObjectives(): Collection
  393.     {
  394.         return $this->creatorObjectives;
  395.     }
  396.     public function addCreatorObjective(Objective $creatorObjective): self
  397.     {
  398.         if (!$this->creatorObjectives->contains($creatorObjective)) {
  399.             $this->creatorObjectives[] = $creatorObjective;
  400.             $creatorObjective->setCreator($this);
  401.         }
  402.         return $this;
  403.     }
  404.     public function removeCreatorObjective(Objective $creatorObjective): self
  405.     {
  406.         if ($this->creatorObjectives->removeElement($creatorObjective)) {
  407.             // set the owning side to null (unless already changed)
  408.             if ($creatorObjective->getCreator() === $this) {
  409.                 $creatorObjective->setCreator(null);
  410.             }
  411.         }
  412.         return $this;
  413.     }
  414.     public function getHashTelegram(): ?string
  415.     {
  416.         return $this->hashTelegram;
  417.     }
  418.     public function setHashTelegram(?string $hashTelegram): self
  419.     {
  420.         $this->hashTelegram $hashTelegram;
  421.         return $this;
  422.     }
  423.     /**
  424.      * @return Collection<int, Employee>
  425.      */
  426.     public function getEmployees(): Collection
  427.     {
  428.         return $this->employees;
  429.     }
  430.     public function addEmployee(Employee $employee): self
  431.     {
  432.         if (!$this->employees->contains($employee)) {
  433.             $this->employees[] = $employee;
  434.             $employee->setCreator($this);
  435.         }
  436.         return $this;
  437.     }
  438.     public function removeEmployee(Employee $employee): self
  439.     {
  440.         if ($this->employees->removeElement($employee)) {
  441.             // set the owning side to null (unless already changed)
  442.             if ($employee->getCreator() === $this) {
  443.                 $employee->setCreator(null);
  444.             }
  445.         }
  446.         return $this;
  447.     }
  448.     public function isDashboard(): ?bool
  449.     {
  450.         return $this->dashboard;
  451.     }
  452.     public function setDashboard(?bool $dashboard): self
  453.     {
  454.         $this->dashboard $dashboard;
  455.         return $this;
  456.     }
  457. }