1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of the 2amigos/yii2-usuario project. |
5
|
|
|
* |
6
|
|
|
* (c) 2amigOS! <http://2amigos.us/> |
7
|
|
|
* |
8
|
|
|
* For the full copyright and license information, please view |
9
|
|
|
* the LICENSE file that was distributed with this source code. |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
namespace Da\User\Model; |
13
|
|
|
|
14
|
|
|
use Da\User\Helper\SecurityHelper; |
15
|
|
|
use Da\User\Query\TokenQuery; |
16
|
|
|
use Da\User\Traits\ContainerAwareTrait; |
17
|
|
|
use Da\User\Traits\ModuleAwareTrait; |
18
|
|
|
use RuntimeException; |
19
|
|
|
use yii\db\ActiveRecord; |
20
|
|
|
use yii\helpers\Url; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* Token Active Record model. |
24
|
|
|
* |
25
|
|
|
* @property int $user_id |
26
|
|
|
* @property string $code |
27
|
|
|
* @property int $type |
28
|
|
|
* @property string $url |
29
|
|
|
* @property bool $isExpired |
30
|
|
|
* @property int $created_at |
31
|
|
|
* @property User $user |
32
|
|
|
*/ |
33
|
|
|
class Token extends ActiveRecord |
34
|
|
|
{ |
35
|
|
|
use ModuleAwareTrait; |
36
|
|
|
use ContainerAwareTrait; |
37
|
|
|
|
38
|
|
|
const TYPE_CONFIRMATION = 0; |
39
|
|
|
const TYPE_RECOVERY = 1; |
40
|
|
|
const TYPE_CONFIRM_NEW_EMAIL = 2; |
41
|
|
|
const TYPE_CONFIRM_OLD_EMAIL = 3; |
42
|
|
|
|
43
|
|
|
protected $routes = [ |
44
|
|
|
self::TYPE_CONFIRMATION => '/user/registration/confirm', |
45
|
|
|
self::TYPE_RECOVERY => '/user/recovery/reset', |
46
|
|
|
self::TYPE_CONFIRM_NEW_EMAIL => '/user/settings/confirm', |
47
|
|
|
self::TYPE_CONFIRM_OLD_EMAIL => '/user/settings/confirm', |
48
|
|
|
]; |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* {@inheritdoc} |
52
|
|
|
*/ |
53
|
4 |
|
public function beforeSave($insert) |
54
|
|
|
{ |
55
|
4 |
|
if ($insert) { |
56
|
4 |
|
$this->setAttribute('code', $this->make(SecurityHelper::class)->generateRandomString()); |
57
|
4 |
|
static::deleteAll(['user_id' => $this->user_id, 'type' => $this->type]); |
58
|
4 |
|
$this->setAttribute('created_at', time()); |
59
|
|
|
} |
60
|
|
|
|
61
|
4 |
|
return parent::beforeSave($insert); |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* {@inheritdoc} |
66
|
|
|
*/ |
67
|
5 |
|
public static function tableName() |
68
|
|
|
{ |
69
|
5 |
|
return '{{%token}}'; |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* {@inheritdoc} |
74
|
|
|
*/ |
75
|
2 |
|
public static function primaryKey() |
76
|
|
|
{ |
77
|
2 |
|
return ['user_id', 'code', 'type']; |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
/** |
81
|
|
|
* @return \yii\db\ActiveQuery |
82
|
|
|
*/ |
83
|
2 |
|
public function getUser() |
84
|
|
|
{ |
85
|
2 |
|
return $this->hasOne($this->getClassMap()->get(User::class), ['id' => 'user_id']); |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
/** |
89
|
|
|
* @return string |
90
|
|
|
*/ |
91
|
4 |
|
public function getUrl() |
92
|
|
|
{ |
93
|
4 |
|
return Url::to([$this->routes[$this->type], 'id' => $this->user_id, 'code' => $this->code], true); |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
/** |
97
|
|
|
* @return bool Whether token has expired |
98
|
|
|
*/ |
99
|
3 |
|
public function getIsExpired() |
100
|
|
|
{ |
101
|
3 |
|
if ($this->type == static::TYPE_RECOVERY) { |
102
|
1 |
|
$expirationTime = $this->getModule()->tokenRecoveryLifespan; |
103
|
2 |
|
} elseif ($this->type >= static::TYPE_CONFIRMATION && $this->type <= static::TYPE_CONFIRM_OLD_EMAIL) { |
104
|
2 |
|
$expirationTime = $this->getModule()->tokenConfirmationLifespan; |
105
|
|
|
} else { |
106
|
|
|
throw new RuntimeException(); |
107
|
|
|
} |
108
|
|
|
|
109
|
3 |
|
return ($this->created_at + $expirationTime) < time(); |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
/** |
113
|
|
|
* @return TokenQuery |
114
|
|
|
*/ |
115
|
4 |
|
public static function find() |
116
|
|
|
{ |
117
|
4 |
|
return new TokenQuery(static::class); |
118
|
|
|
} |
119
|
|
|
} |
120
|
|
|
|