<?php
namespace App\Entity;
use Symfony\Component\Security\Core\User\UserInterface;
use Symfony\Bridge\Doctrine\Validator\Constraints\UniqueEntity;
use Doctrine\ORM\Mapping as ORM;
use Doctrine\Common\Collections\{Collection, ArrayCollection};
use App\Repository\UserRepository;
/**
* User
*
* @UniqueEntity({"email"}, message="Email пользователя должен быть уникальным")
*/
#[ORM\Table('user')]
#[ORM\Entity(UserRepository::class)]
class User implements UserInterface
{
public const SALT = 'motivitydigital';
public const ROLE_ADMIN = 'ROLE_ADMIN';
public const ROLE_USER = 'ROLE_USER';
#[ORM\Column('id', 'integer')]
#[ORM\Id]
#[ORM\GeneratedValue('IDENTITY')]
private ?int $id = null;
#[ORM\Column('email', 'string', 255, unique: true)]
private ?string $email = null;
#[ORM\Column('password', 'text', 255, nullable: true)]
private ?string $password = null;
#[ORM\Column('first_name', 'text', 255)]
private ?string $firstName = null;
#[ORM\Column('last_name', 'text', 255)]
private ?string $lastName = null;
#[ORM\Column('middle_name', 'text', 255, nullable: true)]
private ?string $middleName = null;
#[ORM\Column('deleted', 'boolean')]
private bool $deleted = false;
#[ORM\Column('active', 'boolean')]
private bool $active = true;
#[ORM\ManyToMany(Role::class)]
#[ORM\JoinTable('usergroup')]
#[ORM\JoinColumn('user_id')]
#[ORM\InverseJoinColumn('group_id')]
private ?Collection $groups = null;
#[ORM\Column('created', 'date_immutable', nullable: true)]
private ?\DateTimeImmutable $created;
public function __construct()
{
$this->created = new \DateTimeImmutable();
$this->groups = new ArrayCollection();
}
public function getSalt(): string
{
return self::SALT;
}
public function getUsername(): ?string
{
return $this->email;
}
public function eraseCredentials()
{
// TODO: Implement eraseCredentials() method.
}
public function getId(): ?int
{
return $this->id;
}
public function setId(int $id): User
{
$this->id = $id;
return $this;
}
public function getEmail(): ?string
{
return $this->email;
}
public function setEmail(?string $email): User
{
$this->email = $email;
return $this;
}
public function getPassword(): ?string
{
return $this->password;
}
public function setPassword(?string $password): User
{
if ($password) {
$this->password = md5($password . self::SALT);
}
return $this;
}
public function getUserRolesSystemNames(): array
{
$result = [];
foreach ($this->groups as $role) {
$result[] = $role->getSystemName();
}
return $result;
}
public function getGroups(): ?Collection
{
return $this->groups;
}
/**
* @todo multiple roles
*/
public function getGroup(): Role
{
return $this->groups->current();
}
public function setGroups(Collection $groups): User
{
$this->groups = $groups;
return $this;
}
/**
* @see UserInterface
*/
public function getRoles(): array
{
return [self::ROLE_ADMIN, self::ROLE_USER];
}
public function setRoles(Collection $roles): User
{
$this->groups = $roles;
return $this;
}
public function getFirstName(): ?string
{
return $this->firstName;
}
public function setFirstName(?string $firstName): User
{
$this->firstName = $firstName;
return $this;
}
public function getLastName(): ?string
{
return $this->lastName;
}
public function setLastName(?string $lastName): User
{
$this->lastName = $lastName;
return $this;
}
public function getMiddleName(): ?string
{
return $this->middleName;
}
public function setMiddleName(?string $middleName): User
{
$this->middleName = $middleName;
return $this;
}
public function isDeleted(): bool
{
return $this->deleted;
}
public function setDeleted(bool $deleted): User
{
$this->deleted = $deleted;
return $this;
}
public function isActive(): bool
{
return $this->active;
}
public function setActive(bool $active): User
{
$this->active = $active;
return $this;
}
public function getCreated(): ?\DateTimeImmutable
{
return $this->created;
}
public function setCreated(?\DateTimeImmutable $created): User
{
$this->created = $created;
return $this;
}
public function getFullName(): string
{
return "{$this->firstName} {$this->lastName}";
}
public function getFullTitle(): string
{
return $this->getFullName() . ' - ' . $this->email;
}
public function getUserIdentifier()
{
return $this->id;
}
}