PageRenderTime 47ms CodeModel.GetById 22ms RepoModel.GetById 0ms app.codeStats 0ms

/modules/auth2/classes/models/authusertoken.php

http://github.com/enormego/EightPHP
PHP | 161 lines | 82 code | 26 blank | 53 comment | 15 complexity | 0e01dd8a7adb3fdb34ac81763f75b4ff 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_Core 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' => 0,
  21. 'user_token_user_id' => 0,
  22. 'user_token_token' => '',
  23. 'user_token_user_agent' => '',
  24. 'user_token_created' => 0,
  25. 'user_token_expires' => 0,
  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. return self::db()->use_master(TRUE)->where('user_token_token', $token)->get('user_tokens')->result(TRUE, 'Model_UserToken')->current();
  113. }
  114. /**
  115. * Finds a token for the given user
  116. *
  117. * Accepts a user ID or user object
  118. */
  119. public static function find_token_for_user($user) {
  120. if(is_null($user) OR str::e($user)) {
  121. return FALSE;
  122. }
  123. if(!is_object($user)) {
  124. $user = new Model_User($user);
  125. }
  126. $data = self::db()->where('user_token_user_id', $user->id)->get('user_tokens')->row_array();
  127. if($data === FALSE) {
  128. return FALSE;
  129. } else {
  130. $token = new Model_UserToken(NULL, FALSE);
  131. $token->set($data);
  132. return $token;
  133. }
  134. }
  135. } // End Auth User Token Model