<?php
namespace App\Entity;
use App\Entity\Traits\ActiveTrait;
use App\Entity\Traits\DeleteTrait;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
use Doctrine\ORM\Mapping as ORM;
use Gedmo\Mapping\Annotation as Gedmo;
use Gedmo\Timestampable\Traits\TimestampableEntity;
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;
/**
* @Gedmo\Loggable
*/
#[UniqueEntity(fields: ['username'], errorPath: 'username', message: 'A user has already be created with that username.')]
#[UniqueEntity(fields: ['email'], errorPath: 'email', message: 'The email has already be used by another user. If this user was previously deleted - ask your developer to recover his account')]
#[ORM\Entity(repositoryClass: 'UserRepository')]
#[ORM\Table(name: 'Users')]
class User implements UserInterface, PasswordAuthenticatedUserInterface, \Stringable
{
use ActiveTrait;
use DeleteTrait;
use TimestampableEntity;
#[ORM\Id]
#[ORM\GeneratedValue(strategy: 'AUTO')]
#[ORM\Column(type: 'integer')]
private ?int $id = null;
#[Assert\NotBlank]
#[ORM\Column(type: 'string', length: 25, unique: true)]
private ?string $username = null;
#[Assert\Length(max: 4096)]
private ?string $plainPassword = null;
#[ORM\Column(type: 'string', length: 64)]
private ?string $password = null;
#[Assert\NotBlank]
#[Assert\Email]
#[ORM\Column(type: 'string', length: 60, unique: true)]
private ?string $email = null;
#[ORM\Column(type: 'array')]
private ?array $roles = null;
#[ORM\Column(name: 'name', type: 'string', nullable: true)]
private ?string $name = null;
#[ORM\Column(name: 'profile_image', type: 'string', nullable: true)]
private ?string $profile_image = null;
#[ORM\OneToMany(targetEntity: \App\Entity\Page::class, mappedBy: 'updatedBy')]
private ?Collection $pagesupdated = null;
#[ORM\OneToMany(targetEntity: \App\Entity\Menu::class, mappedBy: 'updatedBy')]
private ?Collection $menuupdated = null;
#[ORM\Column(name: 'emailresetkey', type: 'string', nullable: true)]
private ?string $emailresetkey = null;
#[ORM\OneToMany(mappedBy: 'updatedBy', targetEntity: News::class, orphanRemoval: true)]
private array|\Doctrine\Common\Collections\Collection $news;
public function __construct()
{
$pagesupdated = new ArrayCollection();
$menuupdated = new ArrayCollection();
// may not be needed, see section on salt below
// $this->salt = md5(uniqid(null, true));
$this->news = new ArrayCollection();
}
public function __toString(): string
{
return (string) $this->username;
}
public function getUsername(): ?string
{
return $this->username;
}
public function getSalt(): ?string
{
// you *may* need a real salt depending on your encoder
// see section on salt below
return null;
}
public function getPassword(): ?string
{
return $this->password;
}
public function eraseCredentials(): void {}
public function serialize(): string
{
return serialize([
$this->id,
$this->username,
$this->password,
$this->active,
// see section on salt below
// $this->salt,
]);
}
public function unserialize($serialized): void
{
[$this->id, $this->username, $this->password, $this->active] = unserialize($serialized);
}
public function getId(): int
{
return $this->id;
}
public function setUsername($username): self
{
$this->username = $username;
return $this;
}
public function setPassword($password): self
{
$this->password = $password;
return $this;
}
public function setEmail($email): self
{
$this->email = $email;
return $this;
}
public function getEmail(): ?string
{
return $this->email;
}
public function isAccountNonExpired(): bool
{
return true;
}
public function isAccountNonLocked(): bool
{
return true;
}
public function isCredentialsNonExpired(): bool
{
return true;
}
public function isEnabled(): bool
{
return $this->isActive();
}
public function getRoles(): ?array
{
return $this->roles;
}
public function getPlainPassword(): ?string
{
return $this->plainPassword;
}
public function setPlainPassword($password): void
{
$this->plainPassword = $password;
}
public function setRoles($roles): self
{
$this->roles = $roles;
return $this;
}
public function addRole($role): self
{
$role = strtoupper((string) $role);
if (!in_array($role, $this->roles, true)) {
$this->roles[] = $role;
}
return $this;
}
public function addPagesupdated(Page $pagesupdated): self
{
$this->pagesupdated[] = $pagesupdated;
return $this;
}
public function removePagesupdated(Page $pagesupdated): void
{
$this->pagesupdated->removeElement($pagesupdated);
}
public function getPagesupdated(): ?Collection
{
return $this->pagesupdated;
}
public function getMenuupdated(): ?Collection
{
return $this->menuupdated;
}
public function setName($name): self
{
$this->name = $name;
return $this;
}
public function getName(): ?string
{
return $this->name;
}
public function getFilePath(): string
{
return 'userfiles/images/user';
}
public function setProfileImage($profileImage): self
{
$this->profile_image = $profileImage;
return $this;
}
public function getProfileImagePath(): string
{
if ('' == $this->profile_image) {
return '/takeflight/user-placeholder.png';
}
return 'userfiles/images/user/'.$this->profile_image;
}
public function getProfileImage(): ?string
{
return $this->profile_image;
}
public function setEmailresetkey($emailresetkey): self
{
$this->emailresetkey = $emailresetkey;
return $this;
}
public function getEmailresetkey(): ?string
{
return $this->emailresetkey;
}
public function getUserIdentifier()
{
return $this->username;
}
/**
* @return Collection<int, News>
*/
public function getNews(): Collection
{
return $this->news;
}
public function addNews(News $news): self
{
if (!$this->news->contains($news)) {
$this->news[] = $news;
$news->setUpdatedBy($this);
}
return $this;
}
public function removeNews(News $news): self
{
if ($this->news->removeElement($news)) {
// set the owning side to null (unless already changed)
if ($news->getUpdatedBy() === $this) {
$news->setUpdatedBy(null);
}
}
return $this;
}
}