src/Entity/System/MultifactorAuth.php line 20

Open in your IDE?
  1. <?php
  2. namespace App\Entity\System;
  3. use Doctrine\ORM\Mapping as ORM;
  4. /**
  5. * @ORM\Table(name="multifactor_auth",
  6. * indexes={
  7. *
  8. * @ORM\Index(name="auth_code_idx", columns={"auth_code"}),
  9. * @ORM\Index(name="cookie_value_idx", columns={"cookie_value"}),
  10. * @ORM\Index(name="valid_until_idx", columns={"valid_until"}),
  11. * @ORM\Index(name="date_add_idx", columns={"date_add"})
  12. * }
  13. * )
  14. *
  15. * @ORM\Entity(repositoryClass="App\Repository\System\MultifactorAuthRepository")
  16. */
  17. class MultifactorAuth
  18. {
  19. public const AUTH_CODE_LENGTH = 6;
  20. /**
  21. * @ORM\Id()
  22. *
  23. * @ORM\GeneratedValue()
  24. *
  25. * @ORM\Column(name="id", type="integer")
  26. */
  27. private $id;
  28. /**
  29. * @var Customer
  30. *
  31. * @ORM\ManyToOne(targetEntity="App\Entity\System\Customer")
  32. *
  33. * @ORM\JoinColumn(name="customer_id", referencedColumnName="id_customer")
  34. */
  35. private $customer;
  36. /**
  37. * @var Employee
  38. *
  39. * @ORM\ManyToOne(targetEntity="App\Entity\System\Employee")
  40. *
  41. * @ORM\JoinColumn(name="employee_id", referencedColumnName="id_employee")
  42. */
  43. private $employee;
  44. /**
  45. * @var string|null
  46. *
  47. * @ORM\Column(name="auth_code", type="string", length=64, nullable=true)
  48. */
  49. private $authCode;
  50. /**
  51. * @var int
  52. *
  53. * @ORM\Column(name="times_auth_code_emailed", type="integer", nullable=false)
  54. */
  55. private $timesAuthCodeEmailed;
  56. /**
  57. * @var string|null
  58. *
  59. * @ORM\Column(name="cookie_value", type="string", length=64, nullable=true)
  60. */
  61. private $cookieValue;
  62. /**
  63. * @var \DateTime
  64. *
  65. * @ORM\Column(type="datetime")
  66. */
  67. private $dateAdd;
  68. /**
  69. * @var \DateTime|null
  70. *
  71. * @ORM\Column(name="valid_until", type="datetime", nullable=true)
  72. */
  73. private $validUntil;
  74. /**
  75. * @param Customer $customer
  76. *
  77. * @return MultifactorAuth
  78. *
  79. * @throws \Exception
  80. */
  81. public static function fromCustomer(Customer $customer): MultifactorAuth
  82. {
  83. $multifactorAuth = new self();
  84. $multifactorAuth->customer = $customer;
  85. $multifactorAuth->authCode = $multifactorAuth->generateAuthCode();
  86. $multifactorAuth->dateAdd = new \DateTime();
  87. $multifactorAuth->timesAuthCodeEmailed = 0;
  88. return $multifactorAuth;
  89. }
  90. /**
  91. * @param Employee $employee
  92. *
  93. * @return MultifactorAuth
  94. *
  95. * @throws \Exception
  96. */
  97. public static function fromEmployee(Employee $employee): MultifactorAuth
  98. {
  99. $multifactorAuth = new self();
  100. $multifactorAuth->employee = $employee;
  101. $multifactorAuth->authCode = $multifactorAuth->generateAuthCode();
  102. $multifactorAuth->dateAdd = new \DateTime();
  103. $multifactorAuth->timesAuthCodeEmailed = 0;
  104. return $multifactorAuth;
  105. }
  106. /**
  107. * @param string $cookieValue
  108. * @param int $timestampValidUntil
  109. *
  110. * @return void
  111. */
  112. public function setCookieData(string $cookieValue, int $timestampValidUntil)
  113. {
  114. $this->authCode = null;
  115. $this->cookieValue = $cookieValue;
  116. $this->validUntil = \DateTime::createFromFormat('U', $timestampValidUntil);
  117. }
  118. /**
  119. * @return string
  120. *
  121. * @throws \Exception
  122. */
  123. private function generateAuthCode(): string
  124. {
  125. $authCode = '';
  126. for ($i = 0; $i < self::AUTH_CODE_LENGTH; $i++) {
  127. $authCode .= random_int(0, 9);
  128. }
  129. return $authCode;
  130. }
  131. /**
  132. * @return string|null
  133. */
  134. public function getAuthCode(): ?string
  135. {
  136. return $this->authCode;
  137. }
  138. /**
  139. * @return int
  140. */
  141. public function getTimesAuthCodeEmailed(): int
  142. {
  143. return $this->timesAuthCodeEmailed;
  144. }
  145. /**
  146. * @return void
  147. */
  148. public function emailedOnceAgain()
  149. {
  150. $this->timesAuthCodeEmailed++;
  151. }
  152. }