<?php
namespace App\Entity;
use App\Repository\UserRepository;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
use Doctrine\DBAL\Types\Types;
use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\Security\Core\User\PasswordAuthenticatedUserInterface;
use Symfony\Component\Security\Core\User\UserInterface;
use Symfony\Component\Serializer\Annotation\Groups;
#[ORM\Entity(repositoryClass: UserRepository::class)]
#[ORM\Table(name: '`user`')]
class User implements UserInterface, PasswordAuthenticatedUserInterface
{
public const TYPE_ADMIN = "ADMIN";
public const TYPE_USER = "USER";
#[ORM\Id]
#[ORM\GeneratedValue]
#[ORM\Column(type: 'integer')]
#[Groups(['list'])]
private ?int $id;
#[ORM\Column(type: 'string', length: 180, unique: true)]
#[Groups(['list'])]
private ?string $email;
#[ORM\Column(type: 'boolean')]
#[Groups(['list'])]
private ?string $superUser;
#[ORM\Column(type: 'string', length: 10)]
#[Groups(['list'])]
private ?string $userType;
#[ORM\Column(type: 'json')]
private array $roles = [];
#[ORM\Column(type: 'string')]
private string $password;
#[ORM\Column(type: 'datetime')]
private ?\DateTimeInterface $timeCreated;
#[ORM\Column(type: 'boolean')]
private ?bool $isDeleted;
#[ORM\OneToOne(mappedBy: 'user', targetEntity: UserInfo::class)]
private ?UserInfo $userInfo;
#[ORM\ManyToMany(targetEntity: UserGroup::class)]
#[ORM\JoinTable(name: 'users_groups')]
private $groups;
#[ORM\ManyToMany(targetEntity: Department::class)]
#[ORM\JoinTable(name: 'users_departments')]
private $departments;
#[ORM\ManyToMany(targetEntity: Degree::class)]
#[ORM\JoinTable(name: 'users_degrees')]
private $degrees;
#[ORM\ManyToMany(targetEntity: GlobalConstant::class)]
#[ORM\JoinTable(name: 'users_global_constants')]
private $globalConstants;
#[ORM\ManyToMany(targetEntity: Sector::class)]
#[ORM\JoinTable(name: 'users_sectors')]
private $sectors;
public function __construct()
{
$this->groups = new ArrayCollection();
$this->departments = new ArrayCollection();
$this->degrees = new ArrayCollection();
$this->globalConstants = new ArrayCollection();
$this->sectors = 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;
}
/**
* @see UserInterface
*/
public function getRoles(): array
{
$roles = $this->roles;
// guarantee every user at least has ROLE_USER
$roles[] = 'ROLE_USER';
if ($this->userType === self::TYPE_ADMIN) {
$roles[] = 'ROLE_ADMIN';
if ($this->getSuperUser()) {
$roles[] = 'ROLE_SUPER_USER';
}
foreach ($this->getGroups() as $group) {
foreach ($group->getRoles() as $role) {
$roles[] = 'ROLE_' . $role;
}
}
}
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;
}
/**
* @see UserInterface
*/
public function eraseCredentials()
{
// If you store any temporary, sensitive data on the user, clear it here
// $this->plainPassword = null;
}
public function getTimeCreated(): ?\DateTimeInterface
{
return $this->timeCreated;
}
public function setTimeCreated(\DateTimeInterface $timeCreated): self
{
$this->timeCreated = $timeCreated;
return $this;
}
public function getIsDeleted(): ?bool
{
return $this->isDeleted;
}
public function setIsDeleted(bool $isDeleted): self
{
$this->isDeleted = $isDeleted;
return $this;
}
public function getUserType(): ?string
{
return $this->userType;
}
public function setUserType(string $userType): self
{
$this->userType = $userType;
return $this;
}
/**
* @return Collection<int, UserGroup>
*/
public function getGroups(): Collection
{
return $this->groups;
}
public function addGroup(UserGroup $group): self
{
if (!$this->groups->contains($group)) {
$this->groups[] = $group;
}
return $this;
}
public function removeGroup(UserGroup $group): self
{
$this->groups->removeElement($group);
return $this;
}
public function getSuperUser(): ?bool
{
return $this->superUser;
}
public function setSuperUser(bool $superUser): self
{
$this->superUser = $superUser;
return $this;
}
public function getUserInfo(): ?UserInfo
{
return $this->userInfo;
}
public function setUserInfo(?UserInfo $userInfo): self
{
// unset the owning side of the relation if necessary
if ($userInfo === null && $this->userInfo !== null) {
$this->userInfo->setUser(null);
}
// set the owning side of the relation if necessary
if ($userInfo !== null && $userInfo->getUser() !== $this) {
$userInfo->setUser($this);
}
$this->userInfo = $userInfo;
return $this;
}
public function isSuperUser(): ?bool
{
return $this->superUser;
}
public function isIsDeleted(): ?bool
{
return $this->isDeleted;
}
/**
* @return Collection<int, Department>
*/
public function getDepartments(): Collection
{
return $this->departments;
}
public function addDepartment(Department $department): self
{
if (!$this->departments->contains($department)) {
$this->departments[] = $department;
}
return $this;
}
public function removeDepartment(Department $department): self
{
$this->departments->removeElement($department);
return $this;
}
/**
* @return Collection<int, Degree>
*/
public function getDegrees(): Collection
{
return $this->degrees;
}
public function addDegree(Degree $degree): self
{
if (!$this->degrees->contains($degree)) {
$this->degrees->add($degree);
}
return $this;
}
public function removeDegree(Degree $degree): self
{
$this->degrees->removeElement($degree);
return $this;
}
/**
* @return Collection<int, GlobalConstant>
*/
public function getGlobalConstant(): Collection
{
return $this->globalConstants;
}
public function addGlobalConstant(GlobalConstant $globalConstant): self
{
if (!$this->globalConstants->contains($globalConstant)) {
$this->globalConstants->add($globalConstant);
}
return $this;
}
public function removeGlobalConstant(GlobalConstant $globalConstant): self
{
$this->globalConstants->removeElement($globalConstant);
return $this;
}
/**
* @return Collection<int, GlobalConstant>
*/
public function getGlobalConstants(): Collection
{
return $this->globalConstants;
}
/**
* @return Collection<int, Sector>
*/
public function getSector(): Collection
{
return $this->sector;
}
public function addSector(Sector $sector): self
{
if (!$this->sector->contains($sector)) {
$this->sector->add($sector);
}
return $this;
}
public function removeSector(Sector $sector): self
{
$this->sector->removeElement($sector);
return $this;
}
/**
* @return Collection<int, Sector>
*/
public function getSectors(): Collection
{
return $this->sectors;
}
}