PageRenderTime 41ms CodeModel.GetById 16ms RepoModel.GetById 0ms app.codeStats 0ms

/vendor/laravel/framework/src/Illuminate/Auth/Reminders/DatabaseReminderRepository.php

https://gitlab.com/xolotsoft/pumasruiz
PHP | 195 lines | 70 code | 29 blank | 96 comment | 1 complexity | 7bf0d004c0a81f1f25e810993e011e73 MD5 | raw file
  1. <?php namespace Illuminate\Auth\Reminders;
  2. use Carbon\Carbon;
  3. use Illuminate\Database\Connection;
  4. class DatabaseReminderRepository implements ReminderRepositoryInterface {
  5. /**
  6. * The database connection instance.
  7. *
  8. * @var \Illuminate\Database\Connection
  9. */
  10. protected $connection;
  11. /**
  12. * The reminder database table.
  13. *
  14. * @var string
  15. */
  16. protected $table;
  17. /**
  18. * The hashing key.
  19. *
  20. * @var string
  21. */
  22. protected $hashKey;
  23. /**
  24. * The number of seconds a reminder should last.
  25. *
  26. * @var int
  27. */
  28. protected $expires;
  29. /**
  30. * Create a new reminder repository instance.
  31. *
  32. * @param \Illuminate\Database\Connection $connection
  33. * @param string $table
  34. * @param string $hashKey
  35. * @param int $expires
  36. * @return void
  37. */
  38. public function __construct(Connection $connection, $table, $hashKey, $expires = 60)
  39. {
  40. $this->table = $table;
  41. $this->hashKey = $hashKey;
  42. $this->expires = $expires * 60;
  43. $this->connection = $connection;
  44. }
  45. /**
  46. * Create a new reminder record and token.
  47. *
  48. * @param \Illuminate\Auth\Reminders\RemindableInterface $user
  49. * @return string
  50. */
  51. public function create(RemindableInterface $user)
  52. {
  53. $email = $user->getReminderEmail();
  54. $this->deleteExisting($user);
  55. // We will create a new, random token for the user so that we can e-mail them
  56. // a safe link to the password reset form. Then we will insert a record in
  57. // the database so that we can verify the token within the actual reset.
  58. $token = $this->createNewToken($user);
  59. $this->getTable()->insert($this->getPayload($email, $token));
  60. return $token;
  61. }
  62. /**
  63. * Delete all existing reset tokens from the database.
  64. *
  65. * @param \Illuminate\Auth\Reminders\RemindableInterface $user
  66. * @return int
  67. */
  68. protected function deleteExisting(RemindableInterface $user)
  69. {
  70. return $this->getTable()->where('email', $user->getReminderEmail())->delete();
  71. }
  72. /**
  73. * Build the record payload for the table.
  74. *
  75. * @param string $email
  76. * @param string $token
  77. * @return array
  78. */
  79. protected function getPayload($email, $token)
  80. {
  81. return array('email' => $email, 'token' => $token, 'created_at' => new Carbon);
  82. }
  83. /**
  84. * Determine if a reminder record exists and is valid.
  85. *
  86. * @param \Illuminate\Auth\Reminders\RemindableInterface $user
  87. * @param string $token
  88. * @return bool
  89. */
  90. public function exists(RemindableInterface $user, $token)
  91. {
  92. $email = $user->getReminderEmail();
  93. $reminder = (array) $this->getTable()->where('email', $email)->where('token', $token)->first();
  94. return $reminder && ! $this->reminderExpired($reminder);
  95. }
  96. /**
  97. * Determine if the reminder has expired.
  98. *
  99. * @param array $reminder
  100. * @return bool
  101. */
  102. protected function reminderExpired($reminder)
  103. {
  104. $createdPlusHour = strtotime($reminder['created_at']) + $this->expires;
  105. return $createdPlusHour < $this->getCurrentTime();
  106. }
  107. /**
  108. * Get the current UNIX timestamp.
  109. *
  110. * @return int
  111. */
  112. protected function getCurrentTime()
  113. {
  114. return time();
  115. }
  116. /**
  117. * Delete a reminder record by token.
  118. *
  119. * @param string $token
  120. * @return void
  121. */
  122. public function delete($token)
  123. {
  124. $this->getTable()->where('token', $token)->delete();
  125. }
  126. /**
  127. * Delete expired reminders.
  128. *
  129. * @return void
  130. */
  131. public function deleteExpired()
  132. {
  133. $expired = Carbon::now()->subSeconds($this->expires);
  134. $this->getTable()->where('created_at', '<', $expired)->delete();
  135. }
  136. /**
  137. * Create a new token for the user.
  138. *
  139. * @param \Illuminate\Auth\Reminders\RemindableInterface $user
  140. * @return string
  141. */
  142. public function createNewToken(RemindableInterface $user)
  143. {
  144. $email = $user->getReminderEmail();
  145. $value = str_shuffle(sha1($email.spl_object_hash($this).microtime(true)));
  146. return hash_hmac('sha1', $value, $this->hashKey);
  147. }
  148. /**
  149. * Begin a new database query against the table.
  150. *
  151. * @return \Illuminate\Database\Query\Builder
  152. */
  153. protected function getTable()
  154. {
  155. return $this->connection->table($this->table);
  156. }
  157. /**
  158. * Get the database connection instance.
  159. *
  160. * @return \Illuminate\Database\Connection
  161. */
  162. public function getConnection()
  163. {
  164. return $this->connection;
  165. }
  166. }