<?php
namespace App\Entity;
use DateTime;
use Doctrine\ORM\Mapping as ORM;
use Gedmo\Mapping\Annotation as Gedmo;
use Serializable;
use Symfony\Component\Security\Core\User\UserInterface;
use Symfony\Component\Validator\Constraints as Assert;
/**
* App\Entity\User
*
* @ORM\Table(name="users")
* @ORM\Entity(repositoryClass="App\Repository\UserRepository")
*/
class User implements UserInterface, Serializable
{
/**
* @ORM\Column(type="integer")
* @ORM\Id
* @ORM\GeneratedValue(strategy="AUTO")
*/
private int $id;
/**
* @ORM\Column(type="string", length=25, unique=true)
*/
private string $username;
/**
* @var null|string
* @Gedmo\Slug(fields={"username"}, updatable=false)
* @ORM\Column(name="slug", type="string", length=255, unique=true, nullable=true)
*/
private ?string $slug;
/**
* @ORM\Column(type="string", length=32, nullable=true)
*/
private ?string $salt;
/**
* @ORM\Column(type="string", length=128, nullable=true)
*/
private ?string $password;
/**
*
* @Assert\Regex(
* pattern="/^(?=.*\d)(?=.*[a-z])(?=.*[A-Z])(?!.*\s).*$/",
* message="Please user at least one upper case letter, one lower case letter, and one number"
* )
*/
private $plainPassword;
/**
* @ORM\Column(type="string", length=60, unique=true, nullable=true)
*/
private ?string $email;
/**
* @ORM\Column(name="is_active", type="boolean")
*/
private bool $isActive;
/**
* @ORM\Column(name="roles", type="json")
*/
private ?array $roles = [];
/**
* @Gedmo\Timestampable(on="create")
* @ORM\Column(type="datetime")
*/
private ?DateTime $created;
/**
* @Gedmo\Timestampable(on="update")
* @ORM\Column(type="datetime")
*/
private ?DateTime $updated;
/**
* @Gedmo\Blameable(on="create")
* @ORM\ManyToOne(targetEntity="User")
* @ORM\JoinColumn(name="created_by", referencedColumnName="id", onDelete="set null")
*/
private ?User $createdBy;
/**
* @Gedmo\Blameable(on="update")
* @ORM\ManyToOne(targetEntity="User")
* @ORM\JoinColumn(name="updated_by", referencedColumnName="id", onDelete="set null")
*/
private ?User $updatedBy;
/**
* @ORM\OneToOne(targetEntity="App\Entity\People", mappedBy="user")
**/
private ?People $person;
public function __construct()
{
$this->isActive = true;
$this->salt = md5(uniqid(null, true));
$this->roles = [];
}
public function getUsername(): string
{
return $this->username;
}
public function getSalt(): ?string
{
return $this->salt;
}
public function getPassword(): ?string
{
return $this->password;
}
public function eraseCredentials()
{
// If you store any temporary, sensitive data on the user, clear it here
$this->plainPassword = null;
}
/** @see Serializable::serialize */
public function serialize(): ?string
{
return serialize([
$this->id,
$this->username,
$this->password,
$this->salt,
$this->isActive,
]);
}
/** @see Serializable::unserialize */
public function unserialize($data)
{
[
$this->id,
$this->username,
$this->password,
$this->salt,
$this->isActive
] = unserialize($data);
}
public function getId(): int
{
return $this->id;
}
public function setUsername(string $username): User
{
$this->username = $username;
return $this;
}
public function setSalt(?string $salt): User
{
$this->salt = $salt;
return $this;
}
public function setPassword(?string $password): User
{
$this->password = $password;
return $this;
}
public function setEmail(?string $email): User
{
$this->email = $email;
return $this;
}
public function getEmail(): ?string
{
return $this->email;
}
public function setIsActive(bool $isActive): User
{
$this->isActive = $isActive;
return $this;
}
public function getIsActive(): bool
{
return $this->isActive;
}
public function getCreatedBy(): ?User
{
return $this->createdBy;
}
public function getUpdatedBy(): ?User
{
return $this->updatedBy;
}
public function setCreatedBy(?User $createdBy = null): ?User
{
$this->createdBy = $createdBy;
return $this;
}
public function setUpdatedBy(?User $updatedBy = null): ?User
{
$this->updatedBy = $updatedBy;
return $this;
}
public function setSlug($slug)
{
$this->slug = $slug;
}
public function getSlug(): ?string
{
return $this->slug;
}
public function setCreated(?DateTime $created): User
{
$this->created = $created;
return $this;
}
public function getCreated(): ?DateTime
{
return $this->created;
}
public function setUpdated(?DateTime $updated): User
{
$this->updated = $updated;
return $this;
}
public function getUpdated(): ?DateTime
{
return $this->updated;
}
public function setRoles(?array $roles = []): User
{
$this->roles = $roles;
return $this;
}
public function getRoles(): array
{
$roles = $this->roles;
// guarantee every user at least has ROLE_USER
$roles[] = 'ROLE_USER';
$roles[] = 'ROLE_ADMIN';
$roles[] = 'ROLE_EDITOR';
$roles[] = 'ROLE_SUPER_ADMIN';
// $roles[] = 'ROLE_BLOGGER';
$roles[] = 'ROLE_PUBLISHER';
return array_unique($roles);
}
/**
* @return mixed
*/
public function getPlainPassword()
{
return $this->plainPassword;
}
/**
* @param $plainPassword
*/
public function setPlainPassword($plainPassword)
{
$this->plainPassword = $plainPassword;
}
public function setPerson(?People $person = null): User
{
$this->person = $person;
return $this;
}
public function getPerson(): ?People
{
return $this->person;
}
public function isAccountNonExpired(): bool
{
return true;
}
public function isAccountNonLocked(): bool
{
return true;
}
public function isCredentialsNonExpired(): bool
{
return true;
}
public function isEnabled(): bool
{
return $this->isActive;
}
}