<?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\Component\Security\Core\User\UserInterface;
use App\Exception\ValidationException;
use Symfony\Component\Validator\Constraints as Assert;
use Symfony\Component\Validator\Mapping\ClassMetadata;
use App\Validator\Constraints\RegulationClassValidator;
/**
* @ORM\Entity(repositoryClass=UserRepository::class)
*/
class User implements UserInterface
{
const ROLE_USER = 'ROLE_USER';
const ROLE_CREATOR = 'ROLE_CREATOR';
const ROLE_EDITOR = 'ROLE_EDITOR';
const ROLE_WORKER = 'ROLE_WORKER';
const ROLE_ADMIN = 'ROLE_ADMIN';
/**
* @ORM\Id
* @ORM\GeneratedValue
* @ORM\Column(type="integer")
*/
private $id;
/**
* @ORM\Column(type="string", length=180, unique=true)
*/
private $username;
/**
* @ORM\Column(type="string", length=250)
*/
private $displayName;
/**
* @ORM\Column(type="string", length=250)
*/
private $email;
/**
* @ORM\Column(type="json")
*/
private $roles = [];
/**
* @var string The hashed password
* @ORM\Column(type="string")
*/
private $password;
/**
* @var \DateTime
*
* @ORM\Column(name="modified", type="datetime", nullable=true, options={"comment"="Módosítva"})
*/
private $modified;
private $passwordRetryA;
private $passwordRetryB;
public static $rolesList = [
'Dolgozó'=>'ROLE_WORKER',
'Néző'=>'ROLE_USER',
'Létrehozó' => 'ROLE_CREATOR',
'Szerkesztő' => 'ROLE_EDITOR',
'Adminisztrátor' => 'ROLE_ADMIN'
];
/**
* @ORM\OneToOne(targetEntity=Workers::class, inversedBy="user", cascade={"persist", "remove"})
*/
private $worker;
/**
* @ORM\OneToMany(targetEntity=Attachment::class, mappedBy="user")
*/
private $attachments;
public function __construct()
{
$this->attachments = new ArrayCollection();
}
public static function loadValidatorMetadata(ClassMetadata $metadata)
{
$metadata->addGetterConstraint('checkPassword', new Assert\IsTrue([
'message' => 'Jelszavak nem egyeznek meg',
]));
}
public function getCheckPassword(){
return $this->passwordRetryA == $this->passwordRetryB;
}
/**
* A visual identifier that represents this user.
*
* @see UserInterface
*/
public function getUserIdentifier(): string
{
return (string) $this->username;
}
public function getId(): ?int
{
return $this->id;
}
/**
* A visual identifier that represents this user.
*
* @see UserInterface
*/
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 getDisplayName(): string
{
return (string) $this->displayName;
}
public function setDisplayName(string $displayName): self
{
$this->displayName = $displayName;
return $this;
}
/**
* A visual identifier that represents this user.
*
* @see UserInterface
*/
public function getEmail(): string
{
return (string) $this->email;
}
public function setEmail(string $email): self
{
$this->email = $email;
return $this;
}
/**
* @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;
}
public function hasRoles(){
foreach (func_get_args() as $k => $role) {
if(in_array($role, $this->roles)){
return true;
}
}
return false;
}
/**
* @see UserInterface
*/
public function getPassword(): string
{
return (string) $this->password;
}
public function setPassword(string $password): self
{
$this->password = $password;
return $this;
}
public function getPasswordRetryA(): string
{
return (string) $this->passwordRetryA;
}
public function setPasswordRetryA(?string $password): self
{
$this->passwordRetryA = $password;
return $this;
}
public function getPasswordRetryB(): string
{
return (string) $this->passwordRetryB;
}
public function setPasswordRetryB(?string $password): self
{
$this->passwordRetryB = $password;
return $this;
}
/**
* @see UserInterface
*/
public function getSalt()
{
// not needed when using the "bcrypt" algorithm in security.yaml
}
/**
* @see UserInterface
*/
public function eraseCredentials()
{
// If you store any temporary, sensitive data on the user, clear it here
// $this->plainPassword = null;
}
public function getModified(): ?\DateTimeInterface
{
return $this->modified;
}
public function getWorker(): ?Workers
{
return $this->worker;
}
public function setWorker(?Workers $worker): self
{
$this->worker = $worker;
return $this;
}
/**
* @return Collection|Attachment[]
*/
public function getAttachments(): Collection
{
return $this->attachments;
}
public function addAttachment(Attachment $attachment): self
{
if (!$this->attachments->contains($attachment)) {
$this->attachments[] = $attachment;
$attachment->setUser($this);
}
return $this;
}
public function removeAttachment(Attachment $attachment): self
{
if ($this->attachments->removeElement($attachment)) {
// set the owning side to null (unless already changed)
if ($attachment->getUser() === $this) {
$attachment->setUser(null);
}
}
return $this;
}
public function __toString()
{
return (string)$this->displayName;
}
}