PageRenderTime 45ms CodeModel.GetById 21ms RepoModel.GetById 0ms app.codeStats 0ms

/modules/orm/classes/model/auth/user/token.php

https://bitbucket.org/seyar/startech.local
PHP | 70 lines | 37 code | 11 blank | 22 comment | 2 complexity | 7cb54c455bb0a8a915508e364d12c0ad MD5 | raw file
Possible License(s): BSD-3-Clause, LGPL-2.1
  1. <?php defined('SYSPATH') or die('No direct access allowed.');
  2. /**
  3. * Default auth user toke
  4. *
  5. * @package Kohana/Auth
  6. * @author Kohana Team
  7. * @copyright (c) 2007-2011 Kohana Team
  8. * @license http://kohanaframework.org/license
  9. */
  10. class Model_Auth_User_Token extends ORM {
  11. // Relationships
  12. protected $_belongs_to = array('user' => array());
  13. /**
  14. * Handles garbage collection and deleting of expired objects.
  15. *
  16. * @return void
  17. */
  18. public function __construct($id = NULL)
  19. {
  20. parent::__construct($id);
  21. if (mt_rand(1, 100) === 1)
  22. {
  23. // Do garbage collection
  24. $this->delete_expired();
  25. }
  26. if ($this->expires < time() AND $this->_loaded)
  27. {
  28. // This object has expired
  29. $this->delete();
  30. }
  31. }
  32. /**
  33. * Deletes all expired tokens.
  34. *
  35. * @return ORM
  36. */
  37. public function delete_expired()
  38. {
  39. // Delete all expired tokens
  40. DB::delete($this->_table_name)
  41. ->where('expires', '<', time())
  42. ->execute($this->_db);
  43. return $this;
  44. }
  45. public function create(Validation $validation = NULL)
  46. {
  47. $this->token = $this->create_token();
  48. return parent::create($validation);
  49. }
  50. protected function create_token()
  51. {
  52. do
  53. {
  54. $token = sha1(uniqid(Text::random('alnum', 32), TRUE));
  55. }
  56. while(ORM::factory('user_token', array('token' => $token))->loaded());
  57. return $token;
  58. }
  59. } // End Auth User Token Model