Nie możesz wybrać więcej, niż 25 tematów Tematy muszą się zaczynać od litery lub cyfry, mogą zawierać myślniki ('-') i mogą mieć do 35 znaków.

LoginModel.php 2.7KB

2 lat temu
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. <?php
  2. namespace App\Models;
  3. use CodeIgniter\Model;
  4. use DateTime;
  5. class LoginModel extends Model
  6. {
  7. protected $table = 'auth_logins';
  8. protected $primaryKey = 'id';
  9. protected $returnType = 'object';
  10. protected $useSoftDeletes = false;
  11. protected $allowedFields = [
  12. 'ip_address', 'email', 'user_id', 'date', 'success',
  13. ];
  14. protected $useTimestamps = false;
  15. protected $validationRules = [
  16. 'ip_address' => 'required',
  17. 'email' => 'required',
  18. 'user_id' => 'permit_empty|integer',
  19. 'date' => 'required|valid_date',
  20. ];
  21. protected $validationMessages = [];
  22. protected $skipValidation = false;
  23. /**
  24. * Stores a remember-me token for the user.
  25. *
  26. * @return mixed
  27. */
  28. public function rememberUser(int $userID, string $selector, string $validator, string $expires)
  29. {
  30. $expires = new DateTime($expires);
  31. return $this->db->table('auth_tokens')->insert([
  32. 'user_id' => $userID,
  33. 'selector' => $selector,
  34. 'hashedValidator' => $validator,
  35. 'expires' => $expires->format('Y-m-d H:i:s'),
  36. ]);
  37. }
  38. /**
  39. * Returns the remember-me token info for a given selector.
  40. *
  41. * @return mixed
  42. */
  43. public function getRememberToken(string $selector)
  44. {
  45. return $this->db->table('auth_tokens')
  46. ->where('selector', $selector)
  47. ->get()
  48. ->getRow();
  49. }
  50. /**
  51. * Updates the validator for a given selector.
  52. *
  53. * @return mixed
  54. */
  55. public function updateRememberValidator(string $selector, string $validator)
  56. {
  57. return $this->db->table('auth_tokens')
  58. ->where('selector', $selector)
  59. ->update([
  60. 'hashedValidator' => hash('sha256', $validator),
  61. 'expires' => (new DateTime())->modify('+' . config('Auth')->rememberLength . ' seconds')->format('Y-m-d H:i:s'),
  62. ]);
  63. }
  64. /**
  65. * Removes all persistent login tokens (RememberMe) for a single user
  66. * across all devices they may have logged in with.
  67. *
  68. * @return mixed
  69. */
  70. public function purgeRememberTokens(int $id)
  71. {
  72. return $this->builder('auth_tokens')->where(['user_id' => $id])->delete();
  73. }
  74. /**
  75. * Purges the 'auth_tokens' table of any records that are past
  76. * their expiration date already.
  77. */
  78. public function purgeOldRememberTokens()
  79. {
  80. $config = config('Auth');
  81. if (! $config->allowRemembering) {
  82. return;
  83. }
  84. $this->db->table('auth_tokens')
  85. ->where('expires <=', date('Y-m-d H:i:s'))
  86. ->delete();
  87. }
  88. }