<?php
namespace App\Entity;
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)
* @UniqueEntity(fields={"email"}, message="There is already an account with this email")
* @ORM\HasLifecycleCallbacks()
*/
class User implements UserInterface, PasswordAuthenticatedUserInterface
{
use Timestamps;
public const USER_TYPE_ADMIN = "admin";
public const USER_TYPE_PUBLICATION = "publication";
public const USER_TYPE_INDIVIDUAL = "individual";
/**
* @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="string", length=255)
*/
private $name;
/**
* @ORM\Column(type="boolean")
*/
private $isActive = false;
/**
* @ORM\ManyToOne(targetEntity=Publication::class, inversedBy="users")
*/
private $publication;
/**
* @ORM\Column(type="string", length=20)
*/
private $type;
/**
* @ORM\Column(type="string", length=255, nullable=true)
*/
private $country;
/**
* @ORM\OneToMany(targetEntity=Files::class, mappedBy="userid")
*/
private $files;
public function __construct()
{
$this->files = 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
{
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 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 getName(): ?string
{
return $this->name;
}
public function setName(string $name): self
{
$this->name = $name;
return $this;
}
public function getPrettyRole()
{
return $this->type;
}
public function getAvatar()
{
$keys = array_merge(explode(" ", $this->getName()), [" "," "]);
$keys = array_filter($keys);
$keys = array_merge($keys, ["", ""]);
$avatar = "";
for ($i=0; $i < 2; $i++) {
$avatar .= $keys[$i][0];
}
return trim($avatar);
}
public function isActive(): ?bool
{
return $this->isActive;
}
public function getActive(): ?bool
{
return $this->isActive;
}
public function setIsActive(bool $isActive): self
{
$this->isActive = $isActive;
return $this;
}
public function getPublication(): ?Publication
{
return $this->publication;
}
public function setPublication(?Publication $publication): self
{
$this->publication = $publication;
return $this;
}
public function getType(): ?string
{
return $this->type;
}
public function setType(string $type): self
{
$this->type = $type;
$roles = ['ROLE_USER'];
switch ($type) {
case self::USER_TYPE_ADMIN:
$roles[] = "ROLE_ADMIN";
$this->setRoles($roles);
break;
case self::USER_TYPE_INDIVIDUAL:
$roles[] = "ROLE_INDIVIDUAL";
$this->setRoles($roles);
break;
case self::USER_TYPE_PUBLICATION:
$roles[] = "ROLE_PUBLICATION";
$this->setRoles($roles);
break;
}
return $this;
}
public function getCountry(): ?string
{
return $this->country;
}
public function setCountry(?string $country): self
{
$this->country = $country;
return $this;
}
public static function getTypeChoices()
{
return [
self::USER_TYPE_ADMIN => self::USER_TYPE_ADMIN,
self::USER_TYPE_INDIVIDUAL => self::USER_TYPE_INDIVIDUAL,
self::USER_TYPE_PUBLICATION => self::USER_TYPE_PUBLICATION,
];
}
public function getReportTypes()
{
$types = ["individual" => "Individual"];
if ($this->type ==self::USER_TYPE_ADMIN) {
$types['anonymous'] = "Anonymous";
}
if ($this->type == self::USER_TYPE_ADMIN || $this->type == self::USER_TYPE_PUBLICATION) {
$types['publications'] = "Publications";
}
$types['aggregated'] = "Aggregated";
return $types;
}
/**
* @return Collection<int, Files>
*/
public function getFiles(): Collection
{
return $this->files;
}
public function addFile(Files $file): self
{
if (!$this->files->contains($file)) {
$this->files[] = $file;
$file->setUserid($this);
}
return $this;
}
public function removeFile(Files $file): self
{
if ($this->files->removeElement($file)) {
// set the owning side to null (unless already changed)
if ($file->getUserid() === $this) {
$file->setUserid(null);
}
}
return $this;
}
}