<?php
namespace App\Entity;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
use Doctrine\ORM\Mapping as ORM;
use Ramsey\Uuid\UuidInterface as Uuid;
use Symfony\Component\HttpFoundation\File\File;
use Symfony\Component\Security\Core\User\UserInterface;
use Symfony\Component\Serializer\Annotation\Groups;
use Vich\UploaderBundle\Mapping\Annotation as Vich;
/**
* @ORM\Entity(repositoryClass="App\Repository\UserRepository")
* @ORM\InheritanceType("SINGLE_TABLE")
* @ORM\DiscriminatorColumn(name="userType", type="string")
* @ORM\DiscriminatorMap({"external_user" = "ExternalUser", "employee" = "App\Entity\Employees\Employee"})
* @Vich\Uploadable
*/
class User implements UserInterface, \Serializable
{
/**
* @var Uuid
*
* @ORM\Column(name="id", type="uuid", nullable=false)
* @ORM\Id
* @ORM\GeneratedValue(strategy="CUSTOM")
* @ORM\CustomIdGenerator(class="Ramsey\Uuid\Doctrine\UuidOrderedTimeGenerator")
*/
protected $id;
/**
* @ORM\Column(type="string", length=180, unique=true)
*/
#[Groups('main')]
protected $email;
/**
* @ORM\Column(type="json")
*/
protected $roles = [];
/**
* @ORM\Column(type="string", length=255)
*/
#[Groups('main')]
protected $firstName;
/**
* @ORM\Column(type="string", length=255)
*/
#[Groups('main')]
protected $lastName;
/**
* @var string|null
* @ORM\Column(name="display_name", type="string", length=255, nullable=true, options={"default"=null})
*/
#[Groups(['employee:read', 'employee:write'])]
protected $displayName = null;
/**
* @ORM\Column(type="string", length=255)
*/
protected $password;
/**
* @ORM\OneToMany(targetEntity=UserPermission::class, mappedBy="permission_user", orphanRemoval=true)
*/
protected $userPermissions;
/**
* @ORM\Column(type="datetime", nullable=true)
*
* @var \DateTimeInterface|null
*/
#[Groups(['employee:read', 'employee:write'])]
protected $updatedAt;
/**
* @var string|null
* @ORM\Column(name="image_filename", type="string", length=2048, nullable=true, options={"default"=null})
*/
#[Groups(['employee:read', 'employee:write'])]
protected $imageFilename;
/**
* @ORM\Column(type="integer", nullable=true)
*
* @var int|null
*/
#[Groups(['employee:read', 'employee:write'])]
protected $imageSize;
/**
* NOTE: This is not a mapped field of entity metadata, just a simple property.
*
* @Vich\UploadableField(mapping="employee_avatar", fileNameProperty="imageFilename", size="imageSize")
*
* @var File|null
*/
protected $imageFile;
/**
* If manually uploading a file (i.e. not using Symfony Form) ensure an instance
* of 'UploadedFile' is injected into this setter to trigger the update. If this
* bundle's configuration parameter 'inject_on_load' is set to 'true' this setter
* must be able to accept an instance of 'File' as the bundle will inject one here
* during Doctrine hydration.
*
* @param File|\Symfony\Component\HttpFoundation\File\UploadedFile|null $imageFile
*
* @throws \Exception
*/
public function setImageFile(?File $imageFile = null): void
{
$this->imageFile = $imageFile;
if (null !== $imageFile) {
// It is required that at least one field changes if you are using doctrine
// otherwise the event listeners won't be called and the file is lost
$this->updatedAt = new \DateTimeImmutable();
}
}
public function getImageFile(): ?File
{
return $this->imageFile;
}
public function getImageFilename(): ?string
{
return $this->imageFilename;
}
public function setImageFilename(?string $imageFilename): User
{
$this->imageFilename = $imageFilename;
return $this;
}
public function getImageSize(): ?int
{
return $this->imageSize;
}
public function setImageSize(?int $imageSize): User
{
$this->imageSize = $imageSize;
return $this;
}
public function __construct()
{
$this->userPermissions = new ArrayCollection();
}
public function getId()
{
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 getUsername(): 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';
return array_unique($roles);
}
public function setRoles(array $roles): self
{
$this->roles = $roles;
return $this;
}
/**
* @see UserInterface
*/
public function getPassword()
{
return $this->password;
}
/**
* @see UserInterface
*/
public function getSalt(): ?string
{
// not needed when using bcrypt or argon
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 getFirstName(): ?string
{
return $this->firstName;
}
public function setFirstName(string $firstName): self
{
$this->firstName = $firstName;
return $this;
}
/**
* @return string|null
*/
public function getLastName(): ?string
{
return $this->lastName;
}
/**
* @param mixed $lastName
*
* @return User
*/
public function setLastName($lastName): self
{
$this->lastName = $lastName;
return $this;
}
public function setPassword(string $password): self
{
$this->password = $password;
return $this;
}
public function __toString()
{
return $this->getFirstName();
}
/**
* @return Collection|UserPermission[]
*/
public function getUserPermissions(): Collection
{
return $this->userPermissions;
}
public function addUserPermission(UserPermission $userPermission): self
{
if (!$this->userPermissions->contains($userPermission)) {
$this->userPermissions[] = $userPermission;
$userPermission->setPermissionUser($this);
}
return $this;
}
public function removeUserPermission(UserPermission $userPermission): self
{
if ($this->userPermissions->removeElement($userPermission)) {
// set the owning side to null (unless already changed)
if ($userPermission->getPermissionUser() === $this) {
$userPermission->setPermissionUser(null);
}
}
return $this;
}
/**
* @param mixed $userPermissions
*
* @return User
*/
public function setUserPermissions($userPermissions)
{
$this->userPermissions = $userPermissions;
return $this;
}
public function getUpdatedAt(): ?\DateTimeInterface
{
return $this->updatedAt;
}
public function setUpdatedAt(?\DateTimeInterface $updatedAt): User
{
$this->updatedAt = $updatedAt;
return $this;
}
/**
* @return string|null
*/
public function getDisplayName()
{
if (is_null($this->displayName)) {
return $this->firstName.' '.$this->lastName;
}
return $this->displayName;
}
/**
* @param string|null $displayName
*
* @return User
*/
public function setDisplayName($displayName)
{
$this->displayName = $displayName;
return $this;
}
public function getEntityType()
{
return 'user';
}
public function serialize()
{
return serialize([
$this->id,
$this->email,
$this->password,
// see section on salt below
// $this->salt,
]);
}
/**
* @see \Serializable::unserialize()
*/
public function unserialize($serialized)
{
list(
$this->id,
$this->email,
$this->password,
// see section on salt below
// $this->salt
) = unserialize($serialized);
}
}