<?php
namespace App\Entity;
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\UserInterface;
use Symfony\Component\Validator\Constraints as Assert;
use Rollerworks\Component\PasswordStrength\Validator\Constraints as RollerworksPassword;
/**
* @ORM\Entity(repositoryClass="App\Repository\UserRepository")
* @ORM\Table(name="users")
* @UniqueEntity(fields={"email"}, message="There is already an account with this email")
*/
class User implements UserInterface
{
/**
* @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")
* @RollerworksPassword\PasswordStrength(minLength=8, minStrength=3)
*/
private $password;
/**
* @ORM\ManyToMany(targetEntity="App\Entity\Newsletter", inversedBy="subscribers")
*/
private $subscriptions;
/**
* @ORM\OneToMany(targetEntity="App\Entity\ApplicationRole", mappedBy="user")
*/
private $applicationRoles;
/**
* @ORM\Column(type="boolean")
*/
private $verified;
/**
* @ORM\Column(type="string", length=255, nullable=true)
*/
private $verificationToken;
/**
* @ORM\Column(type="string", length=255, nullable=true)
*/
private $title;
/**
* @ORM\Column(type="string", length=255)
*/
private $forename;
/**
* @ORM\Column(type="string", length=255)
*/
private $surname;
/**
* @ORM\OneToMany(targetEntity="App\Entity\Audit", mappedBy="user")
*/
private $audits;
/**
* @ORM\OneToMany(targetEntity="App\Entity\Comment", mappedBy="user")
*/
private $comments;
/**
* @ORM\OneToMany(targetEntity="App\Entity\Student", mappedBy="user")
*/
private $studentships;
/**
* @ORM\OneToMany(targetEntity=ApplicationReportRole::class, mappedBy="createdBy")
*/
private $applicationReportRoles;
public const TITLES = [
'dr' => 'Dr',
'prof' => 'Professor',
'mr' => 'Mr',
'ms' => 'Ms',
'mrs' => 'Mrs',
'miss' => 'Miss',
];
public function __construct()
{
$this->subscriptions = new ArrayCollection();
$this->applicationRoles = new ArrayCollection();
$this->setRoles(['ROLE_USER']);
$this->setVerified(false);
$this->setVerificationToken(uniqid());
$this->audits = new ArrayCollection();
$this->comments = new ArrayCollection();
$this->studentships = new ArrayCollection();
$this->applicationReportRoles = new ArrayCollection();
}
public function getId(): ?int
{
return $this->id;
}
public function getEmail(): ?string
{
return strpos($this->email, '@') ? $this->email : null;
}
public function setEmail(?string $email): self
{
$this->email = $email;
return $this;
}
public function getUsername(): string
{
return (string)$this->email;
}
/**
* A visual identifier that represents this user.
*
* @see UserInterface
*/
public function getUserIdentifier(): string
{
return (string) $this->email;
}
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;
}
public function removeRoles(array $roles)
{
$currentRoles = $this->getRoles();
$this->roles = array_diff($currentRoles, $roles);
return $this;
}
public function getPassword(): string
{
return (string)$this->password;
}
public function setPassword(string $password): self
{
$this->password = $password;
return $this;
}
public function getSalt()
{
// not needed when using the "bcrypt" algorithm in security.yaml
}
public function eraseCredentials()
{
// If you store any temporary, sensitive data on the user, clear it here
// $this->plainPassword = null;
}
public function isUserAdmin()
{
foreach($this->roles as $role) {
if ($role == "ROLE_ADMIN") {
return true;
}
}
return false;
}
public function getFullName(): ?string
{
$title = $this->getTitle() ? self::TITLES[$this->getTitle()] : '';
return implode(' ', array_filter([
$title,
$this->getForename(),
$this->getSurname(),
]));
}
/**
* @return Collection|Newsletter[]
*/
public function getSubscriptions(): Collection
{
return $this->subscriptions;
}
public function addSubscription(Newsletter $subscription): self
{
if (!$this->subscriptions->contains($subscription)) {
$this->subscriptions[] = $subscription;
}
return $this;
}
public function removeSubscription(Newsletter $subscription): self
{
if ($this->subscriptions->contains($subscription)) {
$this->subscriptions->removeElement($subscription);
}
return $this;
}
/**
* @return Collection|ApplicationRole[]
*/
public function getApplicationRoles(): Collection
{
return $this->applicationRoles;
}
/**
* Get all application roles where the role type acronym is not 'RL' or 'RM'
*
* @return Collection|ApplicationRole[]
*/
public function getFilteredApplicationRoles(): Collection
{
return $this->applicationRoles->filter(function ($role) {
$acronym = $role->getApplicationRoleType()->getAcronym();
return $acronym !== 'RL' && $acronym !== 'RM' && $acronym !== '3R';
});
}
public function addApplicationRole(ApplicationRole $applicationRole): self
{
if (!$this->applicationRoles->contains($applicationRole)) {
$this->applicationRoles[] = $applicationRole;
$applicationRole->setUser($this);
}
return $this;
}
public function removeApplicationRole(ApplicationRole $applicationRole): self
{
if ($this->applicationRoles->contains($applicationRole)) {
$this->applicationRoles->removeElement($applicationRole);
// set the owning side to null (unless already changed)
if ($applicationRole->getUser() === $this) {
$applicationRole->setUsery(null);
}
}
return $this;
}
public function isVerified(): ?bool
{
return $this->verified;
}
public function setVerified(bool $verified): self
{
$this->verified = $verified;
return $this;
}
public function getVerificationToken(): ?string
{
return $this->verificationToken;
}
public function setVerificationToken(?string $verificationToken): self
{
$this->verificationToken = $verificationToken;
return $this;
}
public function getApplications(): Collection
{
$applications = new ArrayCollection();
foreach ($this->applicationRoles as $role) {
$applications->add($role->getApplication());
}
return $applications;
}
public function getTitle(): ?string
{
return $this->title;
}
public function setTitle(?string $title): self
{
$this->title = $title;
return $this;
}
public function getForename(): ?string
{
return $this->forename;
}
public function getForenameById($id): ?string
{
return $this->getDoctrine()->getRepository(User::class)->find($id);
}
public function setForename(string $forename): self
{
$this->forename = $forename;
return $this;
}
public function getSurname(): ?string
{
return $this->surname;
}
public function setSurname(string $surname): self
{
$this->surname = $surname;
return $this;
}
public function getVerified(): ?bool
{
return $this->verified;
}
/**
* @return Collection|Audit[]
*/
public function getAudits(): Collection
{
return $this->audits;
}
public function addAudit(Audit $audit): self
{
if (!$this->audits->contains($audit)) {
$this->audits[] = $audit;
$audit->setUser($this);
}
return $this;
}
public function removeAudit(Audit $audit): self
{
if ($this->audits->contains($audit)) {
$this->audits->removeElement($audit);
// set the owning side to null (unless already changed)
if ($audit->getUser() === $this) {
$audit->setUser(null);
}
}
return $this;
}
/**
* @return Collection|Comment[]
*/
public function getComments(): Collection
{
return $this->comments;
}
public function addComment(Comment $comment): self
{
if (!$this->comments->contains($comment)) {
$this->comments[] = $comment;
$comment->setUser($this);
}
return $this;
}
public function removeComment(Comment $comment): self
{
if ($this->comments->contains($comment)) {
$this->comments->removeElement($comment);
// set the owning side to null (unless already changed)
if ($comment->getUser() === $this) {
$comment->setUser(null);
}
}
return $this;
}
/**
* @return Collection|Student[]
*/
public function getStudentships(): Collection
{
return $this->studentships;
}
public function addStudentship(Student $studentship): self
{
if (!$this->studentships->contains($studentship)) {
$this->studentships[] = $studentship;
$studentship->setUser($this);
}
return $this;
}
public function removeStudentship(Student $studentship): self
{
if ($this->studentships->contains($studentship)) {
$this->studentships->removeElement($studentship);
// set the owning side to null (unless already changed)
if ($studentship->getUser() === $this) {
$studentship->setUser(null);
}
}
return $this;
}
/**
* @return Collection<int, ApplicationReportRole>
*/
public function getApplicationReportRoles(): Collection
{
return $this->applicationReportRoles;
}
public function addApplicationReportRole(ApplicationReportRole $applicationReportRole): self
{
if (!$this->applicationReportRoles->contains($applicationReportRole)) {
$this->applicationReportRoles[] = $applicationReportRole;
$applicationReportRole->setCreatedBy($this);
}
return $this;
}
public function removeApplicationReportRole(ApplicationReportRole $applicationReportRole): self
{
if ($this->applicationReportRoles->removeElement($applicationReportRole)) {
// set the owning side to null (unless already changed)
if ($applicationReportRole->getCreatedBy() === $this) {
$applicationReportRole->setCreatedBy(null);
}
}
return $this;
}
}