src/Entity/UserInteractive.php line 15

Open in your IDE?
  1. <?php
  2. namespace App\Entity;
  3. use Doctrine\ORM\Mapping as ORM;
  4. use Symfony\Component\Serializer\Annotation as SA;
  5. use AdScore\Common\StrUtils;
  6. /**
  7.  * UserInteractive
  8.  *
  9.  * @ORM\Table(name="users_interactive", uniqueConstraints={@ORM\UniqueConstraint(name="email", columns={"email"})}, indexes={@ORM\Index(name="phone_number", columns={"phone_number"})})
  10.  * @ORM\Entity(repositoryClass="App\Repository\UserRepository")
  11.  */
  12. class UserInteractive extends User
  13. {
  14.     /**
  15.      * @var string
  16.      *
  17.      * @ORM\Column(name="email", type="string", length=255, nullable=false, options={"comment"="E-mail used also as login"})
  18.      * @SA\Groups({"user_list", "user_read", "user_search"})
  19.      */
  20.     private $email;
  21.     /**
  22.      * @var string
  23.      * 
  24.      * @SA\Ignore
  25.      * @ORM\Column(name="password", type="string", length=255, nullable=false, options={"comment"="Password hashed with password_hash()"})
  26.      * @SA\Groups({"user_list", "user_read"})
  27.      */
  28.     private $password;
  29.     /**
  30.      * @var string
  31.      *
  32.      * @ORM\Column(name="first_name", type="string", length=255, nullable=false, options={"comment"="First name"})
  33.      * @SA\Groups({"user_list", "user_read", "user_search", "account_search"})
  34.      */
  35.     private $firstName;
  36.     /**
  37.      * @var string
  38.      *
  39.      * @ORM\Column(name="last_name", type="string", length=255, nullable=false, options={"comment"="Last name"})
  40.      * @SA\Groups({"user_list", "user_read", "user_search", "account_search"})
  41.      */
  42.     private $lastName;
  43.     /**
  44.      * @var string
  45.      *
  46.      * @ORM\Column(name="phone_number", type="string", length=64, nullable=false, options={"comment"="Phone number (with country code)"})
  47.      * @SA\Groups({"user_list", "user_read", "user_search"})
  48.      */
  49.     private $phoneNumber;
  50.     /**
  51.      * @var string
  52.      *
  53.      * @ORM\Column(name="country", type="string", length=5, nullable=false, options={"fixed"=true,"comment"="ISO 3166-1 alpha-2"})
  54.      * @SA\Groups({"user_list", "user_read"})
  55.      */
  56.     private $country;
  57.     /**
  58.      * @var string|null
  59.      *
  60.      * @SA\Ignore
  61.      * @ORM\Column(name="session_token", type="string", length=255, nullable=true, options={"comment"="Temporary session token"})
  62.      * @SA\Groups({"user_list", "user_read"})
  63.      */
  64.     private $sessionToken;
  65.     /**
  66.      * @var object|null
  67.      *
  68.      * @ORM\Column(name="meta", type="json", nullable=true)
  69.      */
  70.     private $meta;
  71.     public function getEmail(): ?string
  72.     {
  73.         return $this->email;
  74.     }
  75.     public function setEmail(string $email): self
  76.     {
  77.         $this->email $email;
  78.         return $this;
  79.     }
  80.     public function verifyPassword(string $password): bool
  81.     {
  82.         return \password_verify($password$this->password);
  83.     }
  84.     public function resetSessionToken() : self {
  85.         $this->sessionToken hash('sha256'mt_rand() . time() . $this->id);
  86.         return $this;
  87.     }
  88.     
  89.     public function setPassword(string $password): self
  90.     {
  91.         $this->password = \password_hash($passwordPASSWORD_DEFAULT);
  92.         return $this;
  93.     }
  94.     public function getFirstName(): ?string
  95.     {
  96.         return $this->firstName;
  97.     }
  98.     public function setFirstName(string $firstName): self
  99.     {
  100.         $this->firstName $firstName;
  101.         return $this;
  102.     }
  103.     public function getLastName(): ?string
  104.     {
  105.         return $this->lastName;
  106.     }
  107.     public function setLastName(string $lastName): self
  108.     {
  109.         $this->lastName $lastName;
  110.         return $this;
  111.     }
  112.     public function getPhoneNumber(): ?string
  113.     {
  114.         return $this->phoneNumber;
  115.     }
  116.     public function setPhoneNumber(string $phoneNumber): self
  117.     {
  118.         $this->phoneNumber $phoneNumber;
  119.         return $this;
  120.     }
  121.     public function getCountry(): ?string
  122.     {
  123.         return $this->country;
  124.     }
  125.     public function setCountry(string $country): self
  126.     {
  127.         $this->country $country;
  128.         return $this;
  129.     }
  130.     public function getSessionToken(): ?string
  131.     {
  132.         return $this->sessionToken;
  133.     }
  134.     public function setSessionToken(?string $sessionToken): self
  135.     {
  136.         $this->sessionToken $sessionToken;
  137.         return $this;
  138.     }
  139.     /**
  140.      * @return object
  141.      * @SA\Groups({"user_list", "user_read"})
  142.      */
  143.     public function getMeta(): ?object
  144.     {
  145.         return $this->meta;
  146.     }
  147.     public function setMeta(?object $meta): self
  148.     {
  149.         $this->meta $meta;
  150.         return $this;
  151.     }
  152.     
  153.     /**
  154.      * Generates a credential-derived token
  155.      * @param string $salt
  156.      * @return string
  157.      */
  158.     public function getCredentialDerivedToken(string $salt '') : string {
  159.         return StrUtils::toBase64(
  160.             \hash_hmac('sha256'$this->password $salt$this->sessionTokentrue),
  161.             StrUtils::BASE64_VARIANT_URLSAFE_NO_PADDING
  162.         );
  163.     }
  164.     
  165.     public function verifyCredentialDerivedToken(string $tokenstring $salt '') : bool {
  166.         $refToken $this->getCredentialDerivedToken($salt);
  167.         return \hash_equals($refToken$token);
  168.     }
  169. }