Login_token.php 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. <?php
  2. // This file is part of GNU social - https://www.gnu.org/software/social
  3. //
  4. // GNU social is free software: you can redistribute it and/or modify
  5. // it under the terms of the GNU Affero General Public License as published by
  6. // the Free Software Foundation, either version 3 of the License, or
  7. // (at your option) any later version.
  8. //
  9. // GNU social is distributed in the hope that it will be useful,
  10. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. // GNU Affero General Public License for more details.
  13. //
  14. // You should have received a copy of the GNU Affero General Public License
  15. // along with GNU social. If not, see <http://www.gnu.org/licenses/>.
  16. /**
  17. * Table Definition for login_token
  18. *
  19. * @copyright 2009 StatusNet, Inc.
  20. * @license https://www.gnu.org/licenses/agpl.html GNU AGPL v3 or later
  21. */
  22. defined('GNUSOCIAL') || die();
  23. class Login_token extends Managed_DataObject
  24. {
  25. ###START_AUTOCODE
  26. /* the code below is auto generated do not remove the above tag */
  27. public $__table = 'login_token'; // table name
  28. public $user_id; // int(4) primary_key not_null
  29. public $token; // char(32) not_null
  30. public $created; // datetime()
  31. public $modified; // timestamp() not_null default_CURRENT_TIMESTAMP
  32. /* the code above is auto generated do not remove the tag below */
  33. ###END_AUTOCODE
  34. public static function schemaDef()
  35. {
  36. return array(
  37. 'fields' => array(
  38. 'user_id' => array('type' => 'int', 'not null' => true, 'description' => 'user owning this token'),
  39. 'token' => array('type' => 'char', 'length' => 32, 'not null' => true, 'description' => 'token useable for logging in'),
  40. 'created' => array('type' => 'datetime', 'description' => 'date this record was created'),
  41. 'modified' => array('type' => 'timestamp', 'not null' => true, 'description' => 'date this record was modified'),
  42. ),
  43. 'primary key' => array('user_id'),
  44. 'foreign keys' => array(
  45. 'login_token_user_id_fkey' => array('user', array('user_id' => 'id')),
  46. ),
  47. );
  48. }
  49. const TIMEOUT = 120; // seconds after which to timeout the token
  50. public function makeNew($user)
  51. {
  52. $login_token = Login_token::getKV('user_id', $user->id);
  53. if (!empty($login_token)) {
  54. $login_token->delete();
  55. }
  56. $login_token = new Login_token();
  57. $login_token->user_id = $user->id;
  58. $login_token->token = common_random_hexstr(16);
  59. $login_token->created = common_sql_now();
  60. $result = $login_token->insert();
  61. if (!$result) {
  62. common_log_db_error($login_token, 'INSERT', __FILE__);
  63. // TRANS: Exception thrown when trying creating a login token failed.
  64. // TRANS: %s is the user nickname for which token creation failed.
  65. throw new Exception(sprintf(
  66. _('Could not create login token for %s'),
  67. $user->nickname
  68. ));
  69. }
  70. return $login_token;
  71. }
  72. }