X-Git-Url: https://git.mxchange.org/?a=blobdiff_plain;f=classes%2FLogin_token.php;h=5c463d55c663d708100ff7c003db93357eb6e44c;hb=5c768d7ef71ce08e505056c6f3fbdd092cd3649d;hp=bd6381f9034b6650e87f8243d120251df2a67826;hpb=d9cde0ef80ee838a99035d44f0286b3cc902e332;p=quix0rs-gnu-social.git diff --git a/classes/Login_token.php b/classes/Login_token.php index bd6381f903..5c463d55c6 100644 --- a/classes/Login_token.php +++ b/classes/Login_token.php @@ -1,6 +1,6 @@ array( + 'user_id' => array('type' => 'int', 'not null' => true, 'description' => 'user owning this token'), + 'token' => array('type' => 'char', 'length' => 32, 'not null' => true, 'description' => 'token useable for logging in'), + 'created' => array('type' => 'datetime', 'not null' => true, 'description' => 'date this record was created'), + 'modified' => array('type' => 'timestamp', 'not null' => true, 'description' => 'date this record was modified'), + ), + 'primary key' => array('user_id'), + 'foreign keys' => array( + 'login_token_user_id_fkey' => array('user', array('user_id' => 'id')), + ), + ); + } + + const TIMEOUT = 120; // seconds after which to timeout the token + + function makeNew($user) + { + $login_token = Login_token::getKV('user_id', $user->id); + + if (!empty($login_token)) { + $login_token->delete(); + } + + $login_token = new Login_token(); + + $login_token->user_id = $user->id; + $login_token->token = common_random_hexstr(16); + $login_token->created = common_sql_now(); + + $result = $login_token->insert(); + + if (!$result) { + common_log_db_error($login_token, 'INSERT', __FILE__); + // TRANS: Exception thrown when trying creating a login token failed. + // TRANS: %s is the user nickname for which token creation failed. + throw new Exception(sprintf(_('Could not create login token for %s'), + $user->nickname)); + } + + return $login_token; + } }