<?php
namespace App\Entity;
use App\Enum\User\Role;
use App\Repository\UserRepository;
use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\Security\Core\User\PasswordAuthenticatedUserInterface;
use Symfony\Component\Security\Core\User\UserInterface;
/**
* @ORM\Entity(repositoryClass=UserRepository::class)
*/
class User implements UserInterface, PasswordAuthenticatedUserInterface
{
const USER_FIELD_NAME = "user";
/**
* @ORM\Id
* @ORM\GeneratedValue
* @ORM\Column(type="integer")
*/
private $id;
/**
* @ORM\Column(type="string", length=180, unique=true)
*/
private $username;
/**
* @ORM\Column(type="json")
*/
private $roles = [];
/**
* @var string The hashed password
* @ORM\Column(type="string")
*/
private $password;
/**
* @ORM\Column(type="string", length=255)
*/
private $firstName;
/**
* @ORM\Column(type="string", length=255, nullable=true)
*/
private $lastName;
/**
* @var string|null
*/
private $oldPassword;
/**
* @var string|null
*/
private $newPassword;
/**
* @ORM\OneToOne(targetEntity=UserSettings::class, cascade={"persist", "remove"})
* @ORM\JoinColumn(nullable=false)
*/
private $settings;
/**
* @ORM\Column(type="string", length=30, nullable=true)
*/
private $telegramUserId;
/**
* @ORM\Column(type="string", length=30, nullable=true)
*/
private $telegramPhone;
/**
* @ORM\Column(type="integer", nullable=true)
*/
private $sortId;
public function getId(): ?int
{
return $this->id;
}
/**
* @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 getUserIdentifier(): string
{
return (string) $this->username;
}
/**
* @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 getRolesData(): array
{
$rolesData = [];
foreach ($this->getRoles() as $role) {
$rolesData[] = [
'value' => $role,
'text' => Role::getText($role),
];
}
return $rolesData;
}
public function setRoles(array $roles): self
{
$this->roles = $roles;
return $this;
}
public function isAdmin(): bool
{
return in_array(Role::ROLE_ADMIN, $this->getRoles(), true);
}
public function isModerator(): bool
{
return in_array(Role::ROLE_MODERATOR, $this->getRoles(), true);
}
public function isDeveloper(): bool
{
return in_array(Role::ROLE_DEVELOPER, $this->getRoles(), true);
}
/**
* @see PasswordAuthenticatedUserInterface
*/
public function getPassword(): ?string
{
return $this->password;
}
public function setPassword(?string $password): self
{
$this->password = $password;
return $this;
}
public function getOldPassword(): ?string
{
return $this->oldPassword;
}
public function setOldPassword(string $oldPassword): self
{
$this->oldPassword = $oldPassword;
return $this;
}
public function getNewPassword(): ?string
{
return $this->newPassword;
}
public function setNewPassword(string $newPassword): self
{
$this->newPassword = $newPassword;
return $this;
}
/**
* Returning a salt is only needed, if you are not using a modern
* hashing algorithm (e.g. bcrypt or sodium) in your security.yaml.
*
* @see UserInterface
*/
public function getSalt(): ?string
{
return null;
}
/**
* @see UserInterface
*/
public function eraseCredentials()
{
// If you store any temporary, sensitive data on the user, clear it here
// $this->plainPassword = null;
}
public function getFirstName(): ?string
{
return $this->firstName;
}
public function setFirstName(string $firstName): self
{
$this->firstName = $firstName;
return $this;
}
public function getLastName(): ?string
{
return $this->lastName;
}
public function setLastName(?string $lastName): self
{
$this->lastName = $lastName;
return $this;
}
public function getName(): string
{
return $this->firstName . ' ' . $this->lastName;
}
public function getSettings(): ?UserSettings
{
return $this->settings;
}
public function setSettings(UserSettings $settings): self
{
$this->settings = $settings;
return $this;
}
public function getTelegramUserId(): ?string
{
return $this->telegramUserId;
}
public function setTelegramUserId(?string $telegramUserId): self
{
$this->telegramUserId = $telegramUserId;
return $this;
}
public function getTelegramPhone(): ?string
{
return $this->telegramPhone;
}
public function setTelegramPhone(?string $telegramPhone): self
{
$this->telegramPhone = $telegramPhone;
return $this;
}
public function getSortId(): ?int
{
return $this->sortId;
}
public function setSortId(?int $sortId): self
{
$this->sortId = $sortId;
return $this;
}
}