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