<?php
namespace App\Entity;
use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\Serializer\Annotation as SA;
use AdScore\Common\StrUtils;
/**
* UserInteractive
*
* @ORM\Table(name="users_interactive", uniqueConstraints={@ORM\UniqueConstraint(name="email", columns={"email"})}, indexes={@ORM\Index(name="phone_number", columns={"phone_number"})})
* @ORM\Entity(repositoryClass="App\Repository\UserRepository")
*/
class UserInteractive extends User
{
/**
* @var string
*
* @ORM\Column(name="email", type="string", length=255, nullable=false, options={"comment"="E-mail used also as login"})
* @SA\Groups({"user_list", "user_read", "user_search"})
*/
private $email;
/**
* @var string
*
* @SA\Ignore
* @ORM\Column(name="password", type="string", length=255, nullable=false, options={"comment"="Password hashed with password_hash()"})
* @SA\Groups({"user_list", "user_read"})
*/
private $password;
/**
* @var string
*
* @ORM\Column(name="first_name", type="string", length=255, nullable=false, options={"comment"="First name"})
* @SA\Groups({"user_list", "user_read", "user_search", "account_search"})
*/
private $firstName;
/**
* @var string
*
* @ORM\Column(name="last_name", type="string", length=255, nullable=false, options={"comment"="Last name"})
* @SA\Groups({"user_list", "user_read", "user_search", "account_search"})
*/
private $lastName;
/**
* @var string
*
* @ORM\Column(name="phone_number", type="string", length=64, nullable=false, options={"comment"="Phone number (with country code)"})
* @SA\Groups({"user_list", "user_read", "user_search"})
*/
private $phoneNumber;
/**
* @var string
*
* @ORM\Column(name="country", type="string", length=5, nullable=false, options={"fixed"=true,"comment"="ISO 3166-1 alpha-2"})
* @SA\Groups({"user_list", "user_read"})
*/
private $country;
/**
* @var string|null
*
* @SA\Ignore
* @ORM\Column(name="session_token", type="string", length=255, nullable=true, options={"comment"="Temporary session token"})
* @SA\Groups({"user_list", "user_read"})
*/
private $sessionToken;
/**
* @var object|null
*
* @ORM\Column(name="meta", type="json", nullable=true)
*/
private $meta;
public function getEmail(): ?string
{
return $this->email;
}
public function setEmail(string $email): self
{
$this->email = $email;
return $this;
}
public function verifyPassword(string $password): bool
{
return \password_verify($password, $this->password);
}
public function resetSessionToken() : self {
$this->sessionToken = hash('sha256', mt_rand() . time() . $this->id);
return $this;
}
public function setPassword(string $password): self
{
$this->password = \password_hash($password, PASSWORD_DEFAULT);
return $this;
}
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 getPhoneNumber(): ?string
{
return $this->phoneNumber;
}
public function setPhoneNumber(string $phoneNumber): self
{
$this->phoneNumber = $phoneNumber;
return $this;
}
public function getCountry(): ?string
{
return $this->country;
}
public function setCountry(string $country): self
{
$this->country = $country;
return $this;
}
public function getSessionToken(): ?string
{
return $this->sessionToken;
}
public function setSessionToken(?string $sessionToken): self
{
$this->sessionToken = $sessionToken;
return $this;
}
/**
* @return object
* @SA\Groups({"user_list", "user_read"})
*/
public function getMeta(): ?object
{
return $this->meta;
}
public function setMeta(?object $meta): self
{
$this->meta = $meta;
return $this;
}
/**
* Generates a credential-derived token
* @param string $salt
* @return string
*/
public function getCredentialDerivedToken(string $salt = '') : string {
return StrUtils::toBase64(
\hash_hmac('sha256', $this->password . $salt, $this->sessionToken, true),
StrUtils::BASE64_VARIANT_URLSAFE_NO_PADDING
);
}
public function verifyCredentialDerivedToken(string $token, string $salt = '') : bool {
$refToken = $this->getCredentialDerivedToken($salt);
return \hash_equals($refToken, $token);
}
}