<?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;
use Symfony\Component\Validator\Constraints as Assert;
/**
* @ORM\Entity(repositoryClass=UserRepository::class)
* @UniqueEntity(fields={"username"}, message="Dieser Benutzername wird bereits verwendet")
* @UniqueEntity(
* fields={"email"},
* message="This email already exists!"
* )
*/
class User implements UserInterface, PasswordAuthenticatedUserInterface
{
/**
* @ORM\Id
* @ORM\GeneratedValue
* @ORM\Column(type="integer")
*/
private $id;
/**
* @ORM\Column(type="string", length=180, unique=true)
*/
private $username;
/**
* @ORM\Column(type="json")
*/
private $roles = [];
/**
* @var string The hashed password
* @ORM\Column(type="string")
*/
private $password;
/**
* @ORM\Column(type="string", length=255)
*/
private $firstname;
/**
* @ORM\Column(type="string", length=255)
*/
private $lastname;
/**
* @ORM\Column(type="datetime_immutable")
*/
private $createdAt;
/**
* @ORM\Column(type="string", length=255)
*/
private $email;
/**
* @ORM\Column(type="boolean")
*/
private $isVerified = false;
/**
* @ORM\ManyToOne(targetEntity=District::class, inversedBy="user")
*/
private $district;
/**
* @ORM\Column(type="integer")
*/
private $salutation;
/**
* @ORM\Column(type="string", length=255, nullable=true)
*/
private $title;
/**
* @ORM\Column(type="boolean", nullable=true)
*/
private $contactAllowed;
/**
* @ORM\Column(type="boolean", nullable=true)
*/
private $activated = false;
/**
* @ORM\Column(type="datetime_immutable", nullable=true)
*/
private $activatedAt;
/**
* @ORM\OneToMany(targetEntity=JuryMember::class, mappedBy="user")
*/
private $juryMembers;
/**
* @ORM\OneToMany(targetEntity=Rating::class, mappedBy="user")
*/
private $ratings;
/**
* @ORM\Column(type="string", length=255, nullable=true)
*/
private $function;
/**
* @ORM\Column(type="string", length=255, nullable=true)
*/
private $institution;
/**
* @ORM\Column(type="string", length=50, nullable=true)
*/
private $phone;
/**
* @ORM\Column(type="string", length=50, nullable=true)
*/
private $mobile;
/**
* @ORM\Column(type="boolean", nullable=true)
*/
private $notify;
public function __construct()
{
$this->juryMembers = new ArrayCollection();
$this->ratings = new ArrayCollection();
}
public function getId(): ?int
{
return $this->id;
}
/**
* @deprecated since Symfony 5.3, use getUserIdentifier instead
*/
public function getUsername(): string
{
return (string)$this->username;
}
public function setUsername(?string $username): self
{
$this->username = $username;
return $this;
}
/**
* A visual identifier that represents this user.
*
* @see UserInterface
*/
public function getUserIdentifier(): string
{
return (string)$this->username;
}
/**
* @see UserInterface
*/
public function getRoles(): array
{
$roles = $this->roles;
// guarantee every user at least has ROLE_USER
$roles[] = 'ROLE_USER';
try {
$roles = array_unique($roles);
} catch (\Exception $e) {
dd($roles);
}
return $roles;
}
public function setRoles(array $roles): self
{
$roles = array_diff($roles, ["ROLE_USER"]);
$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;
}
/**
* @Assert\NotBlank(message="Bitte geben Sie einen Vornamen an")
*/
public function getFirstname(): ?string
{
return $this->firstname;
}
public function setFirstname(?string $firstname): self
{
$this->firstname = $firstname;
return $this;
}
/**
* @Assert\NotBlank(message="Bitte geben Sie einen Nachnamen an")
*/
public function getLastname(): ?string
{
return $this->lastname;
}
public function setLastname(?string $lastname): self
{
$this->lastname = $lastname;
return $this;
}
public function getCreatedAt(): ?\DateTimeImmutable
{
return $this->createdAt;
}
public function setCreatedAt(\DateTimeImmutable $createdAt): self
{
$this->createdAt = $createdAt;
return $this;
}
/**
* @Assert\NotBlank(message="Bitte geben Sie eine E-Mail-Adresse an")
* @Assert\Email(
* message="Bitte geben Sie eine gültige E-Mail-Adresse an"
* )
*/
public function getEmail(): ?string
{
return $this->email;
}
public function setEmail(?string $email): self
{
$this->email = $email;
return $this;
}
public function isVerified(): bool
{
return $this->isVerified;
}
public function setIsVerified(bool $isVerified): self
{
$this->isVerified = $isVerified;
return $this;
}
public function getDistrict(): ?District
{
return $this->district;
}
public function setDistrict(?District $district): self
{
$this->district = $district;
return $this;
}
public function getSalutation(): ?int
{
return $this->salutation;
}
public function setSalutation(?int $salutation): self
{
$this->salutation = $salutation;
return $this;
}
public function getTitle(): ?string
{
return $this->title;
}
public function setTitle(?string $title): self
{
$this->title = $title;
return $this;
}
public function isContactAllowed(): ?bool
{
return $this->contactAllowed;
}
public function setContactAllowed(?bool $contactAllowed): self
{
$this->contactAllowed = $contactAllowed;
return $this;
}
public function isActivated(): ?bool
{
return $this->activated;
}
public function setActivated(?bool $activated): self
{
$this->activated = $activated;
return $this;
}
public function getActivatedAt(): ?\DateTimeImmutable
{
return $this->activatedAt;
}
public function setActivatedAt(?\DateTimeImmutable $activatedAt): self
{
$this->activatedAt = $activatedAt;
return $this;
}
// Helper Functions
public function isDistrictUser(): bool
{
return in_array('ROLE_DISTRICT', $this->getRoles(), true);
}
public function isManagerUser(): bool
{
return in_array('ROLE_MANAGER', $this->getRoles(), true);
}
public function isJuryUser(): ?bool
{
return in_array('ROLE_JURY', $this->getRoles(), true);
}
public function setJuryUser(?bool $juryUser): self
{
$roles = $this->getRoles();
if ($juryUser === true) {
if (!$this->isJuryUser()) {
$roles[] = 'ROLE_JURY';
$this->setRoles($roles);
}
} else {
if ($this->isJuryUser()) {
$roles = array_diff($roles, ['ROLE_JURY']);
$this->setRoles($roles);
}
}
return $this;
}
public function setManagerUser(?bool $juryUser): self
{
$roles = $this->getRoles();
if ($juryUser === true) {
if (!$this->isJuryUser()) {
$roles[] = 'ROLE_MANAGER';
$this->setRoles($roles);
}
} else {
if ($this->isJuryUser()) {
$roles = array_diff($roles, ['ROLE_MANAGER']);
$this->setRoles($roles);
}
}
return $this;
}
/**
* @return Collection<int, JuryMember>
*/
public function getJuryMembers(): Collection
{
return $this->juryMembers;
}
public function addJuryMember(JuryMember $juryMember): self
{
if (!$this->juryMembers->contains($juryMember)) {
$this->juryMembers[] = $juryMember;
$juryMember->setUser($this);
}
return $this;
}
public function removeJuryMember(JuryMember $juryMember): self
{
if ($this->juryMembers->removeElement($juryMember)) {
// set the owning side to null (unless already changed)
if ($juryMember->getUser() === $this) {
$juryMember->setUser(null);
}
}
return $this;
}
public function isJuryMember(Contest $contest): bool
{
$juryMembers = $this->getJuryMembers();
foreach ($juryMembers as $juryMember) {
if ($juryMember->getContest() === $contest) {
return true;
}
}
return false;
}
public function __toString(): string
{
return $this->getFirstname() . ' ' . $this->getLastname();
}
/**
* @return Collection<int, Rating>
*/
public function getRatings(): Collection
{
return $this->ratings;
}
public function addRating(Rating $rating): self
{
if (!$this->ratings->contains($rating)) {
$this->ratings[] = $rating;
$rating->setUser($this);
}
return $this;
}
public function removeRating(Rating $rating): self
{
if ($this->ratings->removeElement($rating)) {
// set the owning side to null (unless already changed)
if ($rating->getUser() === $this) {
$rating->setUser(null);
}
}
return $this;
}
public function getFunction(): ?string
{
return $this->function;
}
public function setFunction(?string $function): self
{
$this->function = $function;
return $this;
}
public function getInstitution(): ?string
{
return $this->institution;
}
public function setInstitution(?string $institution): self
{
$this->institution = $institution;
return $this;
}
public function getPhone(): ?string
{
return $this->phone;
}
public function setPhone(?string $phone): self
{
$this->phone = $phone;
return $this;
}
public function getMobile(): ?string
{
return $this->mobile;
}
public function setMobile(?string $mobile): self
{
$this->mobile = $mobile;
return $this;
}
public function getFullSalutation(): string
{
$output = $this->getFirstname() . ' ' . $this->getLastname();
if ($this->getTitle()) {
$output = $this->getTitle() . ' ' . $output;
}
if ($this->getSalutation() == 1) {
return 'Sehr geehrte Frau ' . $output;
} elseif ($this->getSalutation() == 2) {
return 'Sehr geehrter Herr ' . $output;
} else {
return 'Hallo ' . $output;
}
}
public function isNotify(): ?bool
{
return $this->notify;
}
public function setNotify(?bool $notify): self
{
$this->notify = $notify;
return $this;
}
}