Why Gemfury? Push, build, and install  RubyGems npm packages Python packages Maven artifacts PHP packages Go Modules Debian packages RPM packages NuGet packages

Repository URL to install this package:

Details    
digitalascetic/base-user / Entity / AbstractBaseUser.php
Size: Mime:
<?php


namespace DigitalAscetic\BaseUserBundle\Entity;

use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\Security\Core\User\PasswordAuthenticatedUserInterface;
use Symfony\Component\Security\Core\User\UserInterface;
use Symfony\Component\Serializer\Attribute\Groups;
use Symfony\Component\Serializer\Attribute\Ignore;

/**
 * Class AbstractBaseUser
 * @package DigitalAscetic\BaseUserBundle\Entity
 */
abstract class AbstractBaseUser implements UserInterface, PasswordAuthenticatedUserInterface
{

    /**
     * @ORM\Id
     * @ORM\GeneratedValue
     * @ORM\Column(type="integer")
     * @Groups({"id"})
     */
    #[ORM\Id]
    #[ORM\GeneratedValue]
    #[ORM\Column(type: "integer")]
    #[Groups('id')]
    protected ?int $id = null;

    /**
     * @ORM\Column(type="string", length=180, unique=true)
     * @Groups({"user.default"})
     */
    #[ORM\Column(type: "string", length: 180, unique: true)]
    #[Groups('user.default')]
    protected string $email;

    /**
     * @ORM\Column(type="string", length=180, unique=true)
     * @Groups({"user.default"})
     */
    #[ORM\Column(type: "string", length: 180, unique: true)]
    #[Groups('user.default')]
    protected string $username;

    /**
     * @var string The hashed password
     * @ORM\Column(type="string")
     */
    #[ORM\Column(type: "string")]
    #[Ignore]
    protected string $password;

    /**
     * Plain password. Used for model validation. Must not be persisted.
     *
     * @var string|null
     * @Groups({"user.default"})
     */
    #[Groups('user.default')]
    protected ?string $plainPassword = null;

    /**
     * @ORM\Column(type="json")
     * @Groups({"user.roles"})
     */
    #[ORM\Column(type: "json")]
    #[Groups('user.roles')]
    protected array $roles = [];

    /**
     * The salt to use for hashing.
     *
     * @var string|null
     */
    #[ORM\Column(type: "string", nullable: true)]
    #[Ignore]
    protected ?string $salt = null;

    /**
     * The token generated at requesting reset password
     *
     * @var string|null
     * @ORM\Column(type="string", unique=true, nullable=true)
     */
    #[ORM\Column(type: "string", unique: true, nullable: true)]
    protected ?string $passwordRequestToken = null;

    /**
     * The datetime about token generated at requesting reset password
     *
     * @var \DateTime|null
     * @ORM\Column(type="datetime", nullable=true)
     */
    #[ORM\Column(type: "datetime", nullable: true)]
    protected ?\DateTime $passwordRequestTokenAt = null;

    /**
     * @var bool
     * @ORM\Column(type="boolean", options={"default": 0})
     * @Groups({"user.default"})
     */
    #[ORM\Column(type: "boolean", options: ["default" => 0])]
    #[Groups('user.default')]
    protected ?bool $enabled = null;


    public function getId(): ?int
    {
        return $this->id;
    }

    /**
     * A visual identifier that represents this user.
     *
     * @see UserInterface
     */
    public function getUsername(): string
    {
        return $this->username;
    }


    /**
     * This method allow us to handle empty username field
     *
     * @return bool
     */
    public function isEmptyUsername(): bool
    {
        return empty($this->username);
    }

    /**
     * A visual identifier that represents this user.
     *
     * @see UserInterface
     */
    public function getEmail(): string
    {
        return $this->email;
    }

    public function setUsername(string $username): self
    {
        $this->username = $username;

        return $this;
    }

    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;
    }

    /**
     * @param string $role
     * @return $this
     */
    public function addRole(string $role): self
    {
        $role = strtoupper($role);

        if (!in_array($role, $this->roles, true)) {
            $this->roles[] = $role;
        }

        return $this;
    }

    /**
     * @see UserInterface
     */
    public function getPassword(): string
    {
        return $this->password;
    }

    public function setPassword(string $password): self
    {
        $this->password = $password;

        return $this;
    }

    /**
     * @see UserInterface
     */
    public function getSalt()
    {
        return $this->salt;
    }

    /**
     * @see UserInterface
     */
    public function eraseCredentials()
    {
        // If you store any temporary, sensitive data on the user, clear it here
        $this->plainPassword = null;
    }

    /**
     * @return string|null
     */
    public function getPlainPassword(): ?string
    {
        return $this->plainPassword;
    }

    /**
     * @param string|null $plainPassword
     * @return $this
     */
    public function setPlainPassword(?string $plainPassword): self
    {
        $this->plainPassword = $plainPassword;

        return $this;
    }

    /**
     * @return string|null
     */
    public function getPasswordRequestToken(): ?string
    {
        return $this->passwordRequestToken;
    }

    /**
     * @param string|null $passwordRequestToken
     * @return $this
     */
    public function setPasswordRequestToken(?string $passwordRequestToken): self
    {
        $this->passwordRequestToken = $passwordRequestToken;

        return $this;
    }

    public function clearPasswordRequestToken(): self
    {
        $this->passwordRequestToken = null;
        $this->passwordRequestTokenAt = null;

        return $this;
    }

    /**
     * @return \DateTime|null
     */
    public function getPasswordRequestTokenAt(): ?\DateTime
    {
        return $this->passwordRequestTokenAt;
    }

    /**
     * @param \DateTime|null $passwordRequestTokenAt
     */
    public function setPasswordRequestTokenAt(?\DateTime $passwordRequestTokenAt): void
    {
        $this->passwordRequestTokenAt = $passwordRequestTokenAt;
    }

    public function isPasswordRequestTokenNonExpired($ttl): bool
    {
        return $this->passwordRequestTokenAt instanceof \DateTime &&
            $this->passwordRequestTokenAt->getTimestamp() + $ttl > time();
    }

    /**
     * @return bool|null
     */
    public function isEnabled(): ?bool
    {
        return $this->enabled;
    }

    /**
     * @param bool $enabled
     * @return $this
     */
    public function setEnabled(?bool $enabled): self
    {
        $this->enabled = $enabled;

        return $this;
    }

    public function getUserIdentifier(): string
    {
        return $this->username;
    }
}