Token.php 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. <?php
  2. /**
  3. * Default auth user toke
  4. *
  5. * @package Kohana/Auth
  6. * @author Kohana Team
  7. * @copyright (c) Kohana Team
  8. * @license https://koseven.ga/LICENSE.md
  9. */
  10. class Model_Auth_User_Token extends ORM {
  11. // Relationships
  12. protected $_belongs_to = [
  13. 'user' => ['model' => 'User']
  14. ];
  15. protected $_created_column = [
  16. 'column' => 'created',
  17. 'format' => TRUE
  18. ];
  19. /**
  20. * Handles garbage collection and deleting of expired objects.
  21. *
  22. * @return void
  23. */
  24. public function __construct($id = NULL)
  25. {
  26. parent::__construct($id);
  27. if (random_int(1, 100) === 1)
  28. {
  29. // Do garbage collection
  30. $this->delete_expired();
  31. }
  32. if ($this->expires < time() AND $this->_loaded)
  33. {
  34. // This object has expired
  35. $this->delete();
  36. }
  37. }
  38. /**
  39. * Deletes all expired tokens.
  40. *
  41. * @return ORM
  42. */
  43. public function delete_expired()
  44. {
  45. // Delete all expired tokens
  46. DB::delete($this->_table_name)
  47. ->where('expires', '<', time())
  48. ->execute($this->_db);
  49. return $this;
  50. }
  51. public function create(Validation $validation = NULL)
  52. {
  53. $this->token = $this->create_token();
  54. return parent::create($validation);
  55. }
  56. protected function create_token()
  57. {
  58. do
  59. {
  60. $token = sha1(uniqid(Text::random('alnum', 32), TRUE));
  61. }
  62. while (ORM::factory('User_Token', ['token' => $token])->loaded());
  63. return $token;
  64. }
  65. } // End Auth User Token Model