PageRenderTime 44ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 0ms

/modules/sprig-auth/classes/model/auth/user/token.php

https://bitbucket.org/seyar/parshin.local
PHP | 117 lines | 73 code | 13 blank | 31 comment | 4 complexity | c93d67da6653a4ffd2f9fd9ca6b49eeb MD5 | raw file
Possible License(s): BSD-3-Clause, LGPL-2.1
  1. <?php defined('SYSPATH') or die ('No direct script access.');
  2. /**
  3. * Sprig Auth User Token Model
  4. * @package Sprig Auth
  5. * @author Paul Banks
  6. */
  7. class Model_Auth_User_Token extends Sprig
  8. {
  9. protected function _init()
  10. {
  11. $this->_fields += array(
  12. 'id' => new Sprig_Field_Auto,
  13. 'token' => new Sprig_Field_Char(array(
  14. 'unique' => TRUE,
  15. 'empty' => FALSE,
  16. 'max_length' => 32
  17. )),
  18. 'user' => new Sprig_Field_BelongsTo(array(
  19. 'model' => 'User',
  20. )),
  21. 'user_agent' => new Sprig_Field_Char(array(
  22. 'empty' => FALSE,
  23. )),
  24. 'created' => new Sprig_Field_Timestamp(array(
  25. 'auto_now_create' => TRUE,
  26. )),
  27. 'expires' => new Sprig_Field_Timestamp,
  28. );
  29. if (mt_rand(1, 100) === 1)
  30. {
  31. // Do garbage collection
  32. $this->delete_expired();
  33. }
  34. }
  35. /**
  36. * Handle deletion of expired token on load
  37. * @param Database_Query_Builder_Select $query [optional]
  38. * @param int $limit [optional]
  39. * @return Sprig
  40. */
  41. public function load(Database_Query_Builder_Select $query = NULL, $limit = 1)
  42. {
  43. parent::load($query, $limit);
  44. if ($limit === 1 AND $this->loaded() AND $this->expires < time())
  45. {
  46. $this->delete();
  47. $this->_loaded = FALSE;
  48. }
  49. return $this;
  50. }
  51. public function create()
  52. {
  53. // Set hash of the user agent
  54. $this->user_agent = sha1(Request::$user_agent);
  55. // Create a new token each time the token is saved
  56. $this->token = $this->create_token();
  57. return parent::create();
  58. }
  59. public function update()
  60. {
  61. // Create a new token each time the token is saved
  62. $this->token = $this->create_token();
  63. return parent::update();
  64. }
  65. /**
  66. * Deletes all expired tokens.
  67. *
  68. * @return void
  69. */
  70. public function delete_expired()
  71. {
  72. // Delete all expired tokens
  73. DB::delete($this->_table)
  74. ->where('expires', '<', time())
  75. ->execute($this->_db);
  76. return $this;
  77. }
  78. /**
  79. * Finds a new unique token, using a loop to make sure that the token does
  80. * not already exist in the database. This could potentially become an
  81. * infinite loop, but the chances of that happening are very unlikely.
  82. *
  83. * @return string
  84. */
  85. public function create_token()
  86. {
  87. while (TRUE)
  88. {
  89. // Create a random token
  90. $token = text::random('alnum', 32);
  91. // Make sure the token does not already exist
  92. $count = DB::select('id')
  93. ->where('token', '=', $token)
  94. ->from($this->_table)
  95. ->execute($this->_db)
  96. ->count();
  97. if ($count === 0)
  98. {
  99. // A unique token has been found
  100. return $token;
  101. }
  102. }
  103. }
  104. } // End Model_Auth_User_Token