<?php
namespace App\Entity;
use App\Entity\Report\Report;
use App\Entity\Roles\RoleGroup;
use App\Entity\Task\MonitoringObjective;
use App\Entity\Task\Objective;
use App\Repository\UserRepository;
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\PasswordAuthenticatedUserInterface;
use Symfony\Component\Security\Core\User\UserInterface;
/**
* @ORM\Entity(repositoryClass=UserRepository::class)
* @ORM\Table(name="`user`")
* @UniqueEntity(fields={"email"}, message="There is already an account with this email")
*/
class User implements UserInterface, PasswordAuthenticatedUserInterface
{
/**
* @ORM\Id
* @ORM\GeneratedValue
* @ORM\Column(type="integer")
*/
private $id;
/**
* @ORM\Column(type="string", length=180, unique=true)
*/
private $email;
/**
* @ORM\Column(type="json")
*/
private $roles = [];
/**
* @var string The hashed password
* @ORM\Column(type="string")
*/
private $password;
/**
* @ORM\Column(type="boolean")
*/
private $isVerified = false;
/**
* @ORM\Column(type="datetime", nullable=true)
*/
private $lastConnect;
/**
* @ORM\Column(type="string", length=255, nullable=true)
*/
private $phone;
/**
* @ORM\Column(type="string", length=255, nullable=true)
*/
private $hashTelegram;
/**
* @ORM\OneToOne(targetEntity=Employee::class, mappedBy="user", cascade={"persist", "remove"})
*/
private $employee;
/**
* @ORM\ManyToOne(targetEntity=Company::class, inversedBy="user", cascade={"persist", "remove"})
*/
private $company;
/**
* @ORM\Column(type="json", nullable=true)
*/
private $lastCoordinate = [];
/**
* @ORM\Column(type="string", length=255, nullable=true)
*/
private $hashSync;
/**
* @ORM\ManyToOne(targetEntity=RoleGroup::class, inversedBy="users")
*/
private $roleGroup;
/**
* @ORM\ManyToOne(targetEntity=CompanyGroup::class, inversedBy="users")
*/
private $companyGroup;
/**
* @ORM\OneToMany(targetEntity=Objective::class, mappedBy="owner")
*/
private $ownerObjectives;
/**
* @ORM\OneToMany(targetEntity=Objective::class, mappedBy="viewer")
*/
private $viewerObjectives;
/**
* @ORM\OneToMany(targetEntity=MonitoringObjective::class, mappedBy="creator")
*/
private $monitoringObjectives;
/**
* @ORM\OneToMany(targetEntity=Objective::class, mappedBy="creator")
*/
private $creatorObjectives;
/**
* @ORM\OneToMany(targetEntity=Employee::class, mappedBy="creator")
*/
private $employees;
/**
* @ORM\Column(type="boolean", nullable=true)
*/
private $dashboard;
public function __construct()
{
if(is_null($this->id)) {
$this->roles[] = 'ROLE_USER';
$this->dashboard = false;
}
$this->reports = new ArrayCollection();
$this->ownerObjectives = new ArrayCollection();
$this->viewerObjectives = new ArrayCollection();
$this->monitoringObjectives = new ArrayCollection();
$this->creatorObjectives = new ArrayCollection();
$this->employees = new ArrayCollection();
}
public function getId(): ?int
{
return $this->id;
}
public function getEmail(): ?string
{
return $this->email;
}
public function setEmail(string $email): self
{
$this->email = $email;
return $this;
}
/**
* A visual identifier that represents this user.
*
* @see UserInterface
*/
public function getUserIdentifier(): string
{
return (string) $this->email;
}
/**
* @deprecated since Symfony 5.3, use getUserIdentifier instead
*/
public function getUsername(): string
{
if(!empty($this->getEmployee())){
$name = $this->getEmployee()->getName(). " ".$this->getEmployee()->getLastName();
return (string)$name;
}
return (string) $this->email;
}
/**
* @see UserInterface
*/
public function getRoles(): array
{
$roles = $this->roles;
if($this->getEmployee() && $this->getEmployee()->getRole() ){
$roles[] = $this->getEmployee()->getRole();
} else {
if ($this->getCompany() && $this->getCompany()->getRole()) {
$roles[] = $this->getCompany()->getRole();
}
}
if(!empty($this->getRoleGroup())) {
$roles[] = $this->getRoleGroup()->getRole();
}
return array_unique($roles);
}
public function setRoles(array $roles): self
{
$this->roles = $roles;
return $this;
}
/**
* @see PasswordAuthenticatedUserInterface
*/
public function getPassword(): string
{
return $this->password;
}
public function setPassword(string $password): self
{
$this->password = $password;
return $this;
}
/**
* Returning a salt is only needed, if you are not using a modern
* hashing algorithm (e.g. bcrypt or sodium) in your security.yaml.
*
* @see UserInterface
*/
public function getSalt(): ?string
{
return null;
}
/**
* @see UserInterface
*/
public function eraseCredentials()
{
// If you store any temporary, sensitive data on the user, clear it here
// $this->plainPassword = null;
}
public function isVerified(): bool
{
return $this->isVerified;
}
public function setIsVerified(bool $isVerified): self
{
$this->isVerified = $isVerified;
return $this;
}
public function getLastConnect(): ?\DateTimeInterface
{
return $this->lastConnect;
}
public function setLastConnect()
{
$this->lastConnect = new \DateTime();
return $this;
}
public function getPhone(): ?string
{
return $this->phone;
}
public function setPhone(?string $phone): self
{
$this->phone = $phone;
return $this;
}
public function isSuperAdmin()
{
return in_array('ROLE_SUPER_ADMIN',$this->getRoles());
}
public function isAdmin()
{
return in_array('ROLE_ADMIN',$this->getRoles());
}
public function getCompanyId(): ?int
{
if($this->isSuperAdmin()) {
return 0;
}
if(!empty($this->getCompany())) {
return $this->getCompany()->getId();
}
return null;
}
public function getEmployee(): ?Employee
{
return $this->employee;
}
public function setEmployee(?Employee $employee): self
{
// unset the owning side of the relation if necessary
if ($employee === null && $this->employee !== null) {
$this->employee->setUser(null);
}
// set the owning side of the relation if necessary
if ($employee !== null && $employee->getUser() !== $this) {
$employee->setUser($this);
}
$this->employee = $employee;
return $this;
}
public function getCompany(): ?Company
{
return $this->company;
}
public function setCompany(?Company $company): self
{
$this->company = $company;
return $this;
}
public function getLastCoordinate(): ?array
{
return $this->lastCoordinate;
}
public function setLastCoordinate(?array $lastCoordinate): self
{
$this->lastCoordinate = $lastCoordinate;
return $this;
}
public function getHashSync(): ?string
{
return $this->hashSync;
}
public function setHashSync(?string $hashSync): self
{
$this->hashSync = $hashSync;
return $this;
}
public function getRoleGroup(): ?RoleGroup
{
return $this->roleGroup;
}
public function setRoleGroup(?RoleGroup $roleGroup): self
{
$this->roleGroup = $roleGroup;
return $this;
}
public function getCompanyGroup(): ?CompanyGroup
{
if(!empty($this->getCompany())){
return $this->getCompany()->getCompanyGroup();
}
return $this->companyGroup;
}
public function setCompanyGroup(?CompanyGroup $companyGroup): self
{
$this->companyGroup = $companyGroup;
return $this;
}
/**
* @return Collection<int, Objective>
*/
public function getOwnerObjectives(): Collection
{
return $this->ownerObjectives;
}
public function addOwnerObjective(Objective $ownerObjective): self
{
if (!$this->ownerObjectives->contains($ownerObjective)) {
$this->ownerObjectives[] = $ownerObjective;
$ownerObjective->setOwner($this);
}
return $this;
}
public function removeOwnerObjective(Objective $ownerObjective): self
{
if ($this->ownerObjectives->removeElement($ownerObjective)) {
// set the owning side to null (unless already changed)
if ($ownerObjective->getOwner() === $this) {
$ownerObjective->setOwner(null);
}
}
return $this;
}
/**
* @return Collection<int, Objective>
*/
public function getViewerObjectives(): Collection
{
return $this->viewerObjectives;
}
public function addViewerObjective(Objective $viewerObjective): self
{
if (!$this->viewerObjectives->contains($viewerObjective)) {
$this->viewerObjectives[] = $viewerObjective;
$viewerObjective->setViewer($this);
}
return $this;
}
public function removeViewerObjective(Objective $viewerObjective): self
{
if ($this->viewerObjectives->removeElement($viewerObjective)) {
// set the owning side to null (unless already changed)
if ($viewerObjective->getViewer() === $this) {
$viewerObjective->setViewer(null);
}
}
return $this;
}
/**
* @return Collection<int, MonitoringObjective>
*/
public function getMonitoringObjectives(): Collection
{
return $this->monitoringObjectives;
}
public function addMonitoringObjective(MonitoringObjective $monitoringObjective): self
{
if (!$this->monitoringObjectives->contains($monitoringObjective)) {
$this->monitoringObjectives[] = $monitoringObjective;
$monitoringObjective->setCreator($this);
}
return $this;
}
public function removeMonitoringObjective(MonitoringObjective $monitoringObjective): self
{
if ($this->monitoringObjectives->removeElement($monitoringObjective)) {
// set the owning side to null (unless already changed)
if ($monitoringObjective->getCreator() === $this) {
$monitoringObjective->setCreator(null);
}
}
return $this;
}
/**
* @return Collection<int, Objective>
*/
public function getCreatorObjectives(): Collection
{
return $this->creatorObjectives;
}
public function addCreatorObjective(Objective $creatorObjective): self
{
if (!$this->creatorObjectives->contains($creatorObjective)) {
$this->creatorObjectives[] = $creatorObjective;
$creatorObjective->setCreator($this);
}
return $this;
}
public function removeCreatorObjective(Objective $creatorObjective): self
{
if ($this->creatorObjectives->removeElement($creatorObjective)) {
// set the owning side to null (unless already changed)
if ($creatorObjective->getCreator() === $this) {
$creatorObjective->setCreator(null);
}
}
return $this;
}
public function getHashTelegram(): ?string
{
return $this->hashTelegram;
}
public function setHashTelegram(?string $hashTelegram): self
{
$this->hashTelegram = $hashTelegram;
return $this;
}
/**
* @return Collection<int, Employee>
*/
public function getEmployees(): Collection
{
return $this->employees;
}
public function addEmployee(Employee $employee): self
{
if (!$this->employees->contains($employee)) {
$this->employees[] = $employee;
$employee->setCreator($this);
}
return $this;
}
public function removeEmployee(Employee $employee): self
{
if ($this->employees->removeElement($employee)) {
// set the owning side to null (unless already changed)
if ($employee->getCreator() === $this) {
$employee->setCreator(null);
}
}
return $this;
}
public function isDashboard(): ?bool
{
return $this->dashboard;
}
public function setDashboard(?bool $dashboard): self
{
$this->dashboard = $dashboard;
return $this;
}
}