]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - classes/Login_token.php
move core schema to class files
[quix0rs-gnu-social.git] / classes / Login_token.php
1 <?php
2 /**
3  * Table Definition for login_token
4  *
5  * StatusNet - the distributed open-source microblogging tool
6  * Copyright (C) 2009, StatusNet, Inc.
7  *
8  * This program is free software: you can redistribute it and/or modify
9  * it under the terms of the GNU Affero General Public License as published by
10  * the Free Software Foundation, either version 3 of the License, or
11  * (at your option) any later version.
12  *
13  * This program is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16  * GNU Affero General Public License for more details.
17  *
18  * You should have received a copy of the GNU Affero General Public License
19  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
20  */
21
22 if (!defined('STATUSNET') && !defined('LACONICA')) { exit(1); }
23
24 require_once INSTALLDIR.'/classes/Memcached_DataObject.php';
25
26 class Login_token extends Managed_DataObject
27 {
28     ###START_AUTOCODE
29     /* the code below is auto generated do not remove the above tag */
30
31     public $__table = 'login_token';         // table name
32     public $user_id;                         // int(4)  primary_key not_null
33     public $token;                           // char(32)  not_null
34     public $created;                         // datetime()   not_null
35     public $modified;                        // timestamp()   not_null default_CURRENT_TIMESTAMP
36
37     /* Static get */
38     function staticGet($k,$v=NULL) { return Memcached_DataObject::staticGet('Login_token',$k,$v); }
39
40     /* the code above is auto generated do not remove the tag below */
41     ###END_AUTOCODE
42
43     public static function schemaDef()
44     {
45         return array(
46             'fields' => array(
47                 'user_id' => array('type' => 'int', 'not null' => true, 'description' => 'user owning this token'),
48                 'token' => array('type' => 'char', 'length' => 32, 'not null' => true, 'description' => 'token useable for logging in'),
49                 'created' => array('type' => 'datetime', 'not null' => true, 'description' => 'date this record was created'),
50                 'modified' => array('type' => 'timestamp', 'not null' => true, 'description' => 'date this record was modified'),
51             ),
52             'primary key' => array('user_id'),
53             'foreign keys' => array(
54                 'login_token_user_id_fkey' => array('user', array('user_id' => 'id')),
55             ),
56         );
57     }
58
59     const TIMEOUT = 120; // seconds after which to timeout the token
60
61     /*
62     DB_DataObject calculates the sequence key(s) by taking the first key returned by the keys() function.
63     In this case, the keys() function returns user_id as the first key. user_id is not a sequence, but
64     DB_DataObject's sequenceKey() will incorrectly think it is. Then, since the sequenceKey() is a numeric
65     type, but is not set to autoincrement in the database, DB_DataObject will create a _seq table and
66     manage the sequence itself. This is not the correct behavior for the user_id in this class.
67     So we override that incorrect behavior, and simply say there is no sequence key.
68     */
69     function sequenceKey()
70     {
71         return array(false,false);
72     }
73
74     function makeNew($user)
75     {
76         $login_token = Login_token::staticGet('user_id', $user->id);
77
78         if (!empty($login_token)) {
79             $login_token->delete();
80         }
81
82         $login_token = new Login_token();
83
84         $login_token->user_id = $user->id;
85         $login_token->token   = common_good_rand(16);
86         $login_token->created = common_sql_now();
87
88         $result = $login_token->insert();
89
90         if (!$result) {
91             common_log_db_error($login_token, 'INSERT', __FILE__);
92             // TRANS: Exception thrown when trying creating a login token failed.
93             // TRANS: %s is the user nickname for which token creation failed.
94             throw new Exception(sprintf(_('Could not create login token for %s'),
95                                                  $user->nickname));
96         }
97
98         return $login_token;
99     }
100 }