<?php
namespace App\Entity;
use App\Repository\UserRepository;
use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\Security\Core\User\UserInterface;
use Symfony\Component\Validator\Constraints as Assert;
use Symfony\Component\Validator\Mapping\ClassMetadata;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
/**
* @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_SETTINGS = 'ROLE_SETTINGS';
const ROLE_DELETE = 'ROLE_DELETE';
const ROLE_PRINT = 'ROLE_PRINT';
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 = [
'Megtekintés' => 'ROLE_USER',
'Létrehozás' => 'ROLE_CREATOR',
'Szerkesztés' => 'ROLE_EDITOR',
'Törlés' => 'ROLE_DELETE',
'Beállítás' => 'ROLE_SETTINGS',
'Nyomtatás' => 'ROLE_PRINT',
'Adminisztrátor' => 'ROLE_ADMIN',
'Group Adminisztrátor' => 'ROLE_GROUP_ADMIN',
];
/**
* @ORM\ManyToMany(targetEntity=Group::class, mappedBy="member")
*/
private $groups;
/**
* @ORM\OneToMany(targetEntity=Group::class, mappedBy="owner")
*/
private $ownedGroups;
/**
* @ORM\ManyToOne(targetEntity=User::class, inversedBy="users")
*/
private $parent;
/**
* @ORM\OneToMany(targetEntity=User::class, mappedBy="parent")
*/
private $users;
public function __construct()
{
$this->groups = new ArrayCollection();
$this->ownedGroups = new ArrayCollection();
$this->users = new ArrayCollection();
}
public function getRoleName(string $role){
foreach (User::$rolesList as $key => $value) {
if($value == $role){
return $key;
}
}
}
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;
}
public function getId(): ?int
{
return $this->id;
}
/**
* A visual identifier that represents this user.
*
* @see UserInterface
*/
public function getUserIdentifier(): string
{
return (string) $this->email;
}
/**
* @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 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;
}
public function setRole(string $role): self
{
$this->roles[] = $role;
return $this;
}
/**
* @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;
}
/**
* @return Array<int>
*/
public function getGroupsIds(): Array
{
foreach ($this->getOwnedGroups() as $key => $value) {
$this->groups->add($value);
}
$groups = [];
foreach ($this->groups as $key => $value) {
$groups[] = $value->getId();
}
return $groups;
}
/**
* @return Collection<int, Group>
*/
public function getGroups(): Collection
{
foreach ($this->getOwnedGroups() as $key => $value) {
$this->groups->add($value);
}
return $this->groups;
}
public function addGroup(Group $group): self
{
if (!$this->groups->contains($group)) {
$this->groups[] = $group;
$group->addMember($this);
}
return $this;
}
public function removeGroup(Group $group): self
{
if ($this->groups->removeElement($group)) {
$group->removeMember($this);
}
return $this;
}
public function __toString()
{
return $this->displayName;
}
/**
* @return Collection<int, Group>
*/
public function getOwnedGroups(): Collection
{
return $this->ownedGroups;
}
public function addOwnedGroup(Group $ownedGroup): self
{
if (!$this->ownedGroups->contains($ownedGroup)) {
$this->ownedGroups[] = $ownedGroup;
$ownedGroup->setOwner($this);
}
return $this;
}
public function removeOwnedGroup(Group $ownedGroup): self
{
if ($this->ownedGroups->removeElement($ownedGroup)) {
// set the owning side to null (unless already changed)
if ($ownedGroup->getOwner() === $this) {
$ownedGroup->setOwner(null);
}
}
return $this;
}
public function getParent(): ?self
{
return $this->parent;
}
public function setParent(?self $parent): self
{
$this->parent = $parent;
return $this;
}
/**
* @return Collection<int, self>
*/
public function getUsers(): Collection
{
return $this->users;
}
public function addUser(self $user): self
{
if (!$this->users->contains($user)) {
$this->users[] = $user;
$user->setParent($this);
}
return $this;
}
public function removeUser(self $user): self
{
if ($this->users->removeElement($user)) {
// set the owning side to null (unless already changed)
if ($user->getParent() === $this) {
$user->setParent(null);
}
}
return $this;
}
}