PageRenderTime 40ms CodeModel.GetById 15ms RepoModel.GetById 0ms app.codeStats 0ms

/modules/auth/classes/models/authusertoken.php

http://github.com/enormego/EightPHP
PHP | 168 lines | 89 code | 26 blank | 53 comment | 17 complexity | 6fce853cbb7e0fab91c20a3b661e5f56 MD5 | raw file
Possible License(s): BSD-3-Clause
  1. <?php
  2. /**
  3. * @package Modules
  4. * @subpackage Authentication
  5. * @author EightPHP Development Team
  6. * @copyright (c) 2009-2010 EightPHP
  7. * @license http://license.eightphp.com
  8. */
  9. class Model_AuthUserToken extends Modeler {
  10. // Database table name
  11. protected $table_name = 'user_tokens';
  12. // Table primary key
  13. protected $primary_key = 'user_token_token';
  14. // Column prefix
  15. protected $column_prefix = 'user_token_';
  16. // Run all queries on master db
  17. protected $use_master = YES;
  18. // Database fields and default values
  19. public $data = array(
  20. 'user_token_id' => '',
  21. 'user_token_user_id' => '',
  22. 'user_token_token' => '',
  23. 'user_token_user_agent' => '',
  24. 'user_token_created' => '',
  25. 'user_token_expires' => '',
  26. );
  27. public function __construct($id = NULL, $create_token = TRUE) {
  28. parent::__construct($id);
  29. // Current time
  30. $this->now = time();
  31. // Don't run this stuff if we're only looking for an empty shell
  32. if($create_token === TRUE && is_null($id)) {
  33. $this->token = $this->create_token();
  34. }
  35. // Should we handle the expired ones?
  36. if(mt_rand(1, 100) === 1) {
  37. // Do garbage collection
  38. $this->delete_expired();
  39. }
  40. // Did the token expire?
  41. if(!str::e($this->id) && $this->expires < $this->now) {
  42. // This object has expired
  43. $this->delete();
  44. }
  45. }
  46. /**
  47. * Saves the token value.
  48. *
  49. * @return void
  50. */
  51. public function save() {
  52. // Reset primary key so we don't break Modeler
  53. $this->primary_key = 'user_token_id';
  54. // Add the user_agent
  55. if(str::e($this->user_agent)) {
  56. $this->user_agent = sha1(Eight::$user_agent);
  57. }
  58. // Add the created time
  59. if(str::e($this->created)) {
  60. $this->created = time();
  61. }
  62. self::db()->use_master(YES);
  63. return parent::save();
  64. }
  65. /**
  66. * Deletes all expired tokens.
  67. *
  68. * @return void
  69. */
  70. public function delete_expired() {
  71. // Delete all expired tokens
  72. self::db()->use_master(YES);
  73. self::db()->where('user_token_expires <', $this->now)->delete($this->table_name);
  74. return $this;
  75. }
  76. /**
  77. * Determines whether or not the current token is valid
  78. */
  79. public function is_valid() {
  80. if($this->expires > time()) {
  81. return TRUE;
  82. } else {
  83. return FALSE;
  84. }
  85. }
  86. /**
  87. * Finds a new unique token, using a loop to make sure that the token does
  88. * not already exist in the database. This could potentially become an
  89. * infinite loop, but the chances of that happening are very unlikely.
  90. *
  91. * @return string
  92. */
  93. protected function create_token() {
  94. while(true) {
  95. // Create a random token
  96. $token = str::random('alnum', 32);
  97. // Make sure the token does not already exist
  98. self::db()->use_master(YES);
  99. if (self::db()->select('user_token_id')->where('user_token_token', $token)->get($this->table_name)->count() === 0) {
  100. // A unique token has been found
  101. return $token;
  102. }
  103. }
  104. }
  105. /**
  106. * Search for the provided token
  107. */
  108. public static function find_token($token) {
  109. if(empty($token)) {
  110. return FALSE;
  111. }
  112. $data = self::db()->use_master(TRUE)->where('user_token_token', $token)->get('user_tokens')->row_array();
  113. if($data === FALSE) {
  114. return FALSE;
  115. } else {
  116. $token = new Model_UserToken(NULL, TRUE);
  117. $token->set($data);
  118. return $token;
  119. }
  120. }
  121. /**
  122. * Finds a token for the given user
  123. *
  124. * Accepts a user ID or user object
  125. */
  126. public static function find_token_for_user($user) {
  127. if(is_null($user) OR str::e($user)) {
  128. return FALSE;
  129. }
  130. if(!is_object($user)) {
  131. $user = new Model_User($user);
  132. }
  133. $data = self::db()->where('user_token_user_id', $user->id)->get('user_tokens')->row_array();
  134. if($data === FALSE) {
  135. return FALSE;
  136. } else {
  137. $token = new Model_UserToken(NULL, FALSE);
  138. $token->set($data);
  139. return $token;
  140. }
  141. }
  142. } // End Auth User Token Model