src/Entity/User.php line 16
<?php
namespace App\Entity;
use DateTimeImmutable;
use Doctrine\DBAL\Types\Types;
use Doctrine\ORM\Mapping as ORM;
use App\Repository\UserRepository;
use Doctrine\Common\Collections\Collection;
use Doctrine\Common\Collections\ArrayCollection;
use Symfony\Component\Security\Core\User\UserInterface;
use Symfony\Component\Security\Core\User\PasswordAuthenticatedUserInterface;
#[ORM\Entity(repositoryClass: UserRepository::class)]
#[ORM\Table(name: '`user`')]
class User implements UserInterface, PasswordAuthenticatedUserInterface
{
#[ORM\Id]
#[ORM\GeneratedValue]
#[ORM\Column]
private ?int $id = null;
#[ORM\Column(length: 180, unique: true)]
private ?string $email = null;
#[ORM\Column]
private array $roles = [];
/**
* @var string The hashed password
*/
#[ORM\Column]
private ?string $password = null;
private ?string $plainPassword = null;
private ?string $name = null;
#[ORM\Column]
private ?bool $firstLogin = null;
#[ORM\Column]
private ?bool $active = null;
#[ORM\Column(type: Types::DATETIME_IMMUTABLE, nullable: true)]
private ?\DateTimeImmutable $expirationPassword = null;
#[ORM\OneToMany(mappedBy: 'user', targetEntity: PasswordHistory::class, orphanRemoval: true)]
private Collection $passwordHistories;
#[ORM\OneToOne(inversedBy: 'user', cascade: ['persist', 'remove'])]
private ?CompanyMember $companyMember = null;
#[ORM\OneToOne(inversedBy: 'user', cascade: ['persist', 'remove'])]
private ?Accompanist $accompanist = null;
#[ORM\OneToMany(mappedBy: 'author', targetEntity: Evaluation::class)]
private Collection $evaluations;
#[ORM\Column(nullable: true)]
private ?\DateTimeImmutable $lastVisit = null;
#[ORM\Column(nullable: true)]
private ?\DateTimeImmutable $lastLogin = null;
#[ORM\OneToMany(mappedBy: 'stakeholder', targetEntity: Objective::class)]
private Collection $objectives;
public function __construct()
{
$this->firstLogin = true;
$this->active = false;
$this->passwordHistories = new ArrayCollection();
$this->evaluations = new ArrayCollection();
$this->objectives = new ArrayCollection();
}
public function __toString()
{
if ($this->companyMember) {
$name = $this->companyMember->getFirstname() . ' ' . $this->companyMember->getLastname();
} else {
$name = $this->accompanist->getFirstname() . ' ' . substr($this->accompanist->getLastname(), 0, 1) . '.';
}
return $name;
}
public function getName()
{
if ($this->companyMember) {
$name = $this->companyMember->getFirstname() . ' ' . $this->companyMember->getLastname();
} else {
$name = $this->accompanist->getFirstname() . ' ' . substr($this->accompanist->getLastname(), 0, 1) . '.';
}
return $name;
}
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';
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;
}
/**
* Get the value of plainPassword
*/
public function getPlainPassword()
{
return $this->plainPassword;
}
/**
* Set the value of plainPassword
*
* @return self
*/
public function setPlainPassword($plainPassword)
{
$this->plainPassword = $plainPassword;
return $this;
}
public function isFirstLogin(): ?bool
{
return $this->firstLogin;
}
public function setFirstLogin(bool $firstLogin): self
{
$this->firstLogin = $firstLogin;
return $this;
}
public function isActive(): ?bool
{
return $this->active;
}
public function setActive(bool $active): self
{
$this->active = $active;
return $this;
}
public function getExpirationPassword(): ?\DateTimeImmutable
{
return $this->expirationPassword;
}
public function setExpirationPassword(\DateTimeImmutable $expirationPassword): self
{
$this->expirationPassword = $expirationPassword;
return $this;
}
/**
* @return Collection<int, PasswordHistory>
*/
public function getPasswordHistories(): Collection
{
return $this->passwordHistories;
}
public function addPasswordHistory(PasswordHistory $passwordHistory): self
{
if (!$this->passwordHistories->contains($passwordHistory)) {
$this->passwordHistories->add($passwordHistory);
$passwordHistory->setUser($this);
}
return $this;
}
public function removePasswordHistory(PasswordHistory $passwordHistory): self
{
if ($this->passwordHistories->removeElement($passwordHistory)) {
// set the owning side to null (unless already changed)
if ($passwordHistory->getUser() === $this) {
$passwordHistory->setUser(null);
}
}
return $this;
}
public function getCompanyMember(): ?CompanyMember
{
return $this->companyMember;
}
public function setCompanyMember(?CompanyMember $companyMember): self
{
$this->companyMember = $companyMember;
return $this;
}
public function getAccompanist(): ?Accompanist
{
return $this->accompanist;
}
public function setAccompanist(?Accompanist $accompanist): self
{
$this->accompanist = $accompanist;
return $this;
}
/**
* @return Collection<int, Evaluation>
*/
public function getEvaluations(): Collection
{
return $this->evaluations;
}
public function addEvaluation(Evaluation $evaluation): self
{
if (!$this->evaluations->contains($evaluation)) {
$this->evaluations->add($evaluation);
$evaluation->setAuthor($this);
}
return $this;
}
public function removeEvaluation(Evaluation $evaluation): self
{
if ($this->evaluations->removeElement($evaluation)) {
// set the owning side to null (unless already changed)
if ($evaluation->getAuthor() === $this) {
$evaluation->setAuthor(null);
}
}
return $this;
}
public function isAllowed($granted, $company = null): ?bool
{
if (in_array('ROLE_ADMIN', $this->roles)) {
return true;
}
if ($granted == "company") {
if (in_array('ROLE_BILBEA', $this->roles)) {
return true;
} elseif ($this->companyMember->getCompany() == $company) {
return true;
}
} elseif ($granted == "accompanist") {
if (in_array('ROLE_BILBEA', $this->roles)) {
return true;
}
} elseif ($granted == "admin") {
if (in_array('ROLE_ADMIN', $this->roles)) {
return true;
}
}
return false;
}
public function isAccountActive(): ?bool
{
if ($this->companyMember) {
return $this->companyMember->isActive();
}
if ($this->accompanist) {
return $this->accompanist->isActive();
}
}
public function getLastVisit(): ?\DateTimeImmutable
{
return $this->lastVisit;
}
public function setLastVisit(?\DateTimeImmutable $lastVisit): self
{
$this->lastVisit = $lastVisit;
return $this;
}
public function getLastLogin(): ?\DateTimeImmutable
{
return $this->lastLogin;
}
public function setLastLogin(?\DateTimeImmutable $lastLogin): self
{
$this->lastLogin = $lastLogin;
return $this;
}
public function hasMemberShip(): ?bool
{
if (in_array('ROLE_ADMIN', $this->roles) || in_array('ROLE_BILBEA', $this->roles)) {
return true;
}
$now = new DateTimeImmutable();
$memberships = array_reverse($this->companyMember->getCompany()->getMemberships()->toArray());
if (count($memberships) > 0) {
$this->getCompanyMember()->getCompany()->setLastMembership($memberships[0]);
}
foreach (array_reverse($this->companyMember->getCompany()->getMemberships()->toArray()) as $key => $membership) {
if ($membership->isActive() && $membership->getStartedAt() < $now && $membership->getEndAt() > $now) {
return true;
}
}
return false;
}
/**
* @return Collection<int, Objective>
*/
public function getObjectives(): Collection
{
return $this->objectives;
}
public function addObjective(Objective $objective): self
{
if (!$this->objectives->contains($objective)) {
$this->objectives->add($objective);
$objective->setStakeholder($this);
}
return $this;
}
public function removeObjective(Objective $objective): self
{
if ($this->objectives->removeElement($objective)) {
// set the owning side to null (unless already changed)
if ($objective->getStakeholder() === $this) {
$objective->setStakeholder(null);
}
}
return $this;
}
}