<?php
namespace App\Entity;
use App\Entity\Utils\Cloudinary\Media;
use App\Entity\Utils\HistoryInterface;
use App\Entity\Utils\Traits\TimestampTrait;
use App\Repository\BackUserRepository;
use Doctrine\DBAL\Types\Types;
use Doctrine\ORM\Mapping as ORM;
use Stringable;
use Symfony\Component\Security\Core\User\PasswordAuthenticatedUserInterface;
use Symfony\Component\Security\Core\User\UserInterface;
use Symfony\Component\Validator\Constraints as Assert;
/**
* Class BackUser.
*/
#[ORM\Table(name: 'back_user')]
#[ORM\Entity(repositoryClass: BackUserRepository::class)]
class BackUser implements UserInterface, HistoryInterface, PasswordAuthenticatedUserInterface, Stringable
{
use TimestampTrait;
#[ORM\Id]
#[ORM\GeneratedValue]
#[ORM\Column(type: Types::INTEGER)]
private ?int $id = null;
#[ORM\Column(type: Types::STRING)]
#[Assert\NotBlank(message: 'form.error.required', groups: ['edit', 'create'])]
#[Assert\Length(min: 2, max: 255, minMessage: 'form.error.short', maxMessage: 'form.error.long', groups: ['edit', 'create'])]
private string $fullName;
#[ORM\Column(type: Types::STRING, unique: true)]
#[Assert\NotBlank(message: 'form.error.required', groups: ['create'])]
#[Assert\Length(min: 2, max: 255, minMessage: 'form.error.short', maxMessage: 'form.error.long', groups: ['create'])]
#[Assert\Regex(pattern: '/^[a-z0-9](-?[a-z0-9]+)*$/i', message: 'form.error.username', match: true, groups: ['create'])]
private string $username;
#[Assert\Valid]
#[ORM\OneToOne(targetEntity: Media::class, cascade: ['all'])]
#[ORM\JoinColumn(name: 'image_id', onDelete: 'cascade')]
protected ?Media $image = null;
#[ORM\Column(type: Types::STRING, unique: true)]
#[Assert\NotBlank(message: 'form.error.required', groups: ['edit', 'create'])]
#[Assert\Length(min: 2, max: 255, minMessage: 'form.error.short', maxMessage: 'form.error.long', groups: ['edit', 'create'])]
#[Assert\Email(message: 'The email {{ value }} is not a valid email.', groups: ['edit', 'create'])]
protected string $email;
#[ORM\Column(type: Types::STRING, nullable: true)]
protected ?string $preferredLocale = null;
#[ORM\Column(type: Types::STRING, nullable: true)]
protected ?string $preferredLanguage = null;
#[ORM\Column(type: Types::STRING, nullable: true)]
protected ?string $timezone = null;
#[ORM\Column(type: Types::STRING, nullable: true)]
protected ?string $theme = null;
#[ORM\Column(type: Types::STRING)]
protected string $password;
/**
* @var ?string
*/
#[Assert\NotBlank(message: 'form.error.required', groups: ['create'])]
#[Assert\Regex(pattern: '/^(?=.*[a-z])(?=.*[A-Z])(?=.*\d)[A-Za-z\d\/@$!%*#?&]{8,4096}$/', message: 'form.error.password', match: true, groups: ['create'])]
protected ?string $plainPassword = null;
#[ORM\Column(type: Types::STRING, nullable: true)]
protected ?string $resetPasswordHash = null;
#[ORM\Column(type: Types::STRING, nullable: true)]
protected ?string $unlockingHash = null;
#[ORM\Column(type: Types::JSON)]
protected array $roles = [];
#[ORM\Column(type: Types::JSON)]
protected array $notifications = [];
/**
* Auto Save contents.
*/
#[ORM\Column(type: Types::BOOLEAN)]
protected bool $autoSave = true;
/**
* Enabled.
*/
#[ORM\Column(type: Types::BOOLEAN)]
protected bool $enabled = true;
/**
* Excerpt.
*/
#[ORM\Column(type: Types::TEXT, nullable: true)]
#[Assert\Length(min: 1, max: 5000, minMessage: 'form.error.short', maxMessage: 'form.error.long')]
protected ?string $excerpt = null;
#[ORM\Column(type: Types::DATETIME_MUTABLE, nullable: true)]
protected ?\DateTime $lastConnection = null;
#[ORM\Column(type: Types::STRING, nullable: true)]
protected ?string $lastIp = null;
public function __toString(): string
{
return $this->fullName;
}
public function getId(): ?int
{
return $this->id;
}
public function setFullName(string $fullName): void
{
$this->fullName = $fullName;
}
public function getFullName(): ?string
{
return $this->fullName;
}
public function getUsername(): ?string
{
return $this->username;
}
public function setUsername(string $username): void
{
$this->username = $username;
}
public function getEmail(): ?string
{
return $this->email;
}
public function setEmail(string $email): void
{
$this->email = $email;
}
public function getPreferredLocale(): ?string
{
return $this->preferredLocale;
}
public function setPreferredLocale(?string $preferredLocale = null): void
{
$this->preferredLocale = $preferredLocale;
}
public function getPreferredLanguage(): ?string
{
return $this->preferredLanguage;
}
public function setPreferredLanguage(?string $preferredLanguage = null): void
{
$this->preferredLanguage = $preferredLanguage;
}
public function getTimezone(): ?string
{
return $this->timezone;
}
public function setTimezone(string $timezone): void
{
$this->timezone = $timezone;
}
public function getTheme(): ?string
{
return $this->theme;
}
public function setTheme(string $theme): void
{
$this->theme = $theme;
}
public function getPassword(): ?string
{
return $this->password;
}
public function setPassword(string $password): void
{
$this->plainPassword = null;
$this->password = $password;
}
public function getPlainPassword(): ?string
{
return $this->plainPassword;
}
public function setPlainPassword(?string $plainPassword = null): void
{
$this->plainPassword = $plainPassword;
}
public function getResetPasswordHash(): ?string
{
return $this->resetPasswordHash;
}
public function setResetPasswordHash(string $resetPasswordHash = null): void
{
$this->resetPasswordHash = $resetPasswordHash;
}
public function generateResetPasswordHash(): void
{
try {
$hash = md5($this->getId().'_'.date_timestamp_get(new \DateTime()).'_'.uniqid());
$this->setResetPasswordHash($hash);
} catch (\Exception) {
}
}
public function getUnlockingHash(): ?string
{
return $this->unlockingHash;
}
public function setUnlockingHash(string $unlockingHash = null): void
{
$this->unlockingHash = $unlockingHash;
}
public function generateUnlockingHash(): void
{
try {
$hash = md5($this->getId().'_'.date_timestamp_get(new \DateTime()).'_'.uniqid());
$this->setUnlockingHash($hash);
} catch (\Exception) {
}
}
public static function getRolesString(): string
{
return 'ROLE_SUPER_ADMIN,ROLE_ADMIN,ROLE_REDACTOR,ROLE_READER';
}
public function getRoles(): array
{
$roles = $this->roles;
if (empty($roles)) {
$roles[] = 'ROLE_USER';
}
return array_unique($roles);
}
public function setRoles(array $roles): void
{
$this->roles = $roles;
}
public function getSalt(): ?string
{
return null;
}
public function eraseCredentials(): void
{
}
public function getInitials(): ?string
{
$initials = [];
$nameParts = explode(' ', $this->fullName);
foreach ($nameParts as $part) {
$initials[] = substr($part, 0, 1);
}
return implode('', $initials);
}
public function getImage(): ?Media
{
return $this->image;
}
public function setImage(?Media $image): void
{
$this->image = $image;
}
public function getNotifications(): ?array
{
return $this->notifications;
}
public function setNotifications(array $notifications): void
{
$this->notifications = $notifications;
}
public function emptyNotifications(): void
{
$this->notifications = [];
}
public function getAutoSave(): bool
{
return $this->autoSave;
}
public function setAutoSave(bool $autoSave): void
{
$this->autoSave = $autoSave;
}
public function switchAutoSave(): void
{
$this->autoSave = !$this->autoSave;
}
public function getEnabled(): bool
{
return $this->enabled;
}
public function setEnabled(bool $enabled): void
{
$this->enabled = $enabled;
}
public function getExcerpt(): ?string
{
return $this->excerpt;
}
public function setExcerpt(?string $excerpt): void
{
$this->excerpt = $excerpt;
}
public function getLastConnection(): ?\DateTime
{
return $this->lastConnection;
}
public function setLastConnection(?\DateTime $lastConnection): void
{
$this->lastConnection = $lastConnection;
}
public function getLastIp(): ?string
{
return $this->lastIp;
}
public function setLastIp(?string $lastIp): void
{
$this->lastIp = $lastIp;
}
/*public function normalize(NormalizerInterface $normalizer, $format = null, array $context = []): array
{
$image = null;
if ($this->image && !empty($this->image->getSingleMedia()) && $this->image->getSingleMedia()->getPublicId() != null) {
$image = $this->image->getSingleMedia()->getPublicId();
}
return [
'name' => $this->fullName,
'username' => $this->username,
'email' => $this->email,
'roles' => $this->roles,
'lastIp' => $this->lastIp,
'image' => $image,
'url' => '/back/profile/' . $this->id,
];
}*/
public function getEntityType(): ?string
{
return 'back_user';
}
public function getUserIdentifier(): string
{
return $this->username;
}
// PHP 8.1 and higher
public function __serialize(): array
{
return [
'id' => $this->id,
'username' => $this->username,
'password' => $this->password,
];
}
// PHP 8.1 and higher
public function __unserialize(array $data): void
{
$this->id = $data['id'];
$this->username = $data['username'];
$this->password = $data['password'];
}
/*// PHP 8.0 and lower and will be remove in PHP 9
public function serialize(): string
{
return serialize([$this->id, $this->username, $this->password]);
}
// PHP 8.0 and lower and will be remove in PHP 9
public function unserialize($data): void
{
[$this->id, $this->username, $this->password] = unserialize($data, ['allowed_classes' => false]);
}*/
}