|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace app\models; |
|
4
|
|
|
|
|
5
|
|
|
use app\components\ArrayHelper; |
|
6
|
|
|
use Yii; |
|
7
|
|
|
use yii\behaviors\AttributeBehavior; |
|
8
|
|
|
use yii\behaviors\TimestampBehavior; |
|
9
|
|
|
use yii\db\ActiveRecord; |
|
10
|
|
|
use yii\db\BaseActiveRecord; |
|
11
|
|
|
use yii\web\IdentityInterface; |
|
12
|
|
|
|
|
13
|
|
|
/** |
|
14
|
|
|
* This is the model class for table "user". |
|
15
|
|
|
* |
|
16
|
|
|
* @property int $id |
|
17
|
|
|
* @property string $username |
|
18
|
|
|
* @property string $email |
|
19
|
|
|
* @property string $image |
|
20
|
|
|
* @property string $google_user_id |
|
21
|
|
|
* @property int $active |
|
22
|
|
|
* @property string $updated_at |
|
23
|
|
|
* @property string $created_at |
|
24
|
|
|
* @property string $access_token |
|
25
|
|
|
* |
|
26
|
|
|
* @property AccountCategory[] $accountCategories |
|
27
|
|
|
*/ |
|
28
|
|
|
class User extends ActiveRecord implements IdentityInterface |
|
29
|
|
|
{ |
|
30
|
|
|
|
|
31
|
|
|
public function behaviors() |
|
32
|
|
|
{ |
|
33
|
|
|
return ArrayHelper::merge(parent::behaviors(), [ |
|
34
|
|
|
'time' => TimestampBehavior::class, |
|
35
|
|
|
'access_token' => [ |
|
36
|
|
|
'class' => AttributeBehavior::class, |
|
37
|
|
|
'attributes' => [ |
|
38
|
|
|
BaseActiveRecord::EVENT_BEFORE_INSERT => ['access_token'], |
|
39
|
|
|
BaseActiveRecord::EVENT_BEFORE_UPDATE => ['access_token'], |
|
40
|
|
|
], |
|
41
|
|
|
'preserveNonEmptyValues' => true, |
|
42
|
|
|
'value' => function() { |
|
43
|
|
|
do { |
|
44
|
|
|
$token = Yii::$app->security->generateRandomString(64); |
|
45
|
|
|
$tokenExist = static::find() |
|
46
|
|
|
->andWhere(['user.access_token' => $token]) |
|
47
|
|
|
->exists(); |
|
48
|
|
|
} while ($tokenExist); |
|
49
|
|
|
|
|
50
|
|
|
return $token; |
|
51
|
|
|
}, |
|
52
|
|
|
], |
|
53
|
|
|
]); |
|
54
|
|
|
} |
|
55
|
|
|
|
|
56
|
|
|
/** |
|
57
|
|
|
* @inheritdoc |
|
58
|
|
|
*/ |
|
59
|
|
|
public static function tableName() |
|
60
|
|
|
{ |
|
61
|
|
|
return 'user'; |
|
62
|
|
|
} |
|
63
|
|
|
|
|
64
|
|
|
/** |
|
65
|
|
|
* @inheritdoc |
|
66
|
|
|
*/ |
|
67
|
|
|
public function rules() |
|
68
|
|
|
{ |
|
69
|
|
|
return [ |
|
70
|
|
|
[['username', 'email', 'google_user_id'], 'required'], |
|
71
|
|
|
[['active'], 'integer'], |
|
72
|
|
|
[['updated_at', 'created_at'], 'safe'], |
|
73
|
|
|
[['username', 'email', 'image', 'google_user_id', 'access_token'], 'string', 'max' => 255], |
|
74
|
|
|
[['access_token'], 'unique'], |
|
75
|
|
|
]; |
|
76
|
|
|
} |
|
77
|
|
|
|
|
78
|
|
|
/** |
|
79
|
|
|
* @inheritdoc |
|
80
|
|
|
*/ |
|
81
|
|
|
public function attributeLabels() |
|
82
|
|
|
{ |
|
83
|
|
|
return [ |
|
84
|
|
|
'id' => 'ID', |
|
85
|
|
|
'username' => 'Username', |
|
86
|
|
|
'email' => 'Email', |
|
87
|
|
|
'image' => 'Image', |
|
88
|
|
|
'google_user_id' => 'Google User ID', |
|
89
|
|
|
'active' => 'Active', |
|
90
|
|
|
'updated_at' => 'Updated At', |
|
91
|
|
|
'created_at' => 'Created At', |
|
92
|
|
|
]; |
|
93
|
|
|
} |
|
94
|
|
|
|
|
95
|
|
|
/** |
|
96
|
|
|
* Finds an identity by the given ID. |
|
97
|
|
|
* |
|
98
|
|
|
* @param string|int $id the ID to be looked for |
|
99
|
|
|
* @return IdentityInterface the identity object that matches the given ID. |
|
100
|
|
|
* Null should be returned if such an identity cannot be found |
|
101
|
|
|
* or the identity is not in an active state (disabled, deleted, etc.) |
|
102
|
|
|
*/ |
|
103
|
|
|
public static function findIdentity($id) |
|
104
|
|
|
{ |
|
105
|
|
|
return static::findOne($id); |
|
|
|
|
|
|
106
|
|
|
} |
|
107
|
|
|
|
|
108
|
|
|
/** |
|
109
|
|
|
* Finds an identity by the given token. |
|
110
|
|
|
* |
|
111
|
|
|
* @param mixed $token the token to be looked for |
|
112
|
|
|
* @param mixed $type the type of the token. The value of this parameter depends on the implementation. |
|
113
|
|
|
* For example, [[\yii\filters\auth\HttpBearerAuth]] will set this parameter to be `yii\filters\auth\HttpBearerAuth`. |
|
114
|
|
|
* @return IdentityInterface the identity object that matches the given token. |
|
115
|
|
|
* Null should be returned if such an identity cannot be found |
|
116
|
|
|
* or the identity is not in an active state (disabled, deleted, etc.) |
|
117
|
|
|
*/ |
|
118
|
|
|
public static function findIdentityByAccessToken($token, $type = null) |
|
119
|
|
|
{ |
|
120
|
|
|
return User::findOne(['access_token' => $token]); |
|
|
|
|
|
|
121
|
|
|
} |
|
122
|
|
|
|
|
123
|
|
|
/** |
|
124
|
|
|
* Returns an ID that can uniquely identify a user identity. |
|
125
|
|
|
* |
|
126
|
|
|
* @return string|int an ID that uniquely identifies a user identity. |
|
127
|
|
|
*/ |
|
128
|
|
|
public function getId() |
|
129
|
|
|
{ |
|
130
|
|
|
return $this->id; |
|
131
|
|
|
} |
|
132
|
|
|
|
|
133
|
|
|
/** |
|
134
|
|
|
* Returns a key that can be used to check the validity of a given identity ID. |
|
135
|
|
|
* |
|
136
|
|
|
* The key should be unique for each individual user, and should be persistent |
|
137
|
|
|
* so that it can be used to check the validity of the user identity. |
|
138
|
|
|
* |
|
139
|
|
|
* The space of such keys should be big enough to defeat potential identity attacks. |
|
140
|
|
|
* |
|
141
|
|
|
* This is required if [[User::enableAutoLogin]] is enabled. |
|
142
|
|
|
* |
|
143
|
|
|
* @return string a key that is used to check the validity of a given identity ID. |
|
144
|
|
|
* @see validateAuthKey() |
|
145
|
|
|
*/ |
|
146
|
|
|
public function getAuthKey() |
|
147
|
|
|
{ |
|
148
|
|
|
// TODO: Implement getAuthKey() method. |
|
149
|
|
|
} |
|
150
|
|
|
|
|
151
|
|
|
/** |
|
152
|
|
|
* Validates the given auth key. |
|
153
|
|
|
* |
|
154
|
|
|
* This is required if [[User::enableAutoLogin]] is enabled. |
|
155
|
|
|
* |
|
156
|
|
|
* @param string $authKey the given auth key |
|
157
|
|
|
* @return bool whether the given auth key is valid. |
|
158
|
|
|
* @see getAuthKey() |
|
159
|
|
|
*/ |
|
160
|
|
|
public function validateAuthKey($authKey) |
|
161
|
|
|
{ |
|
162
|
|
|
// TODO: Implement validateAuthKey() method. |
|
163
|
|
|
} |
|
164
|
|
|
|
|
165
|
|
|
/** |
|
166
|
|
|
* @return \yii\db\ActiveQuery |
|
167
|
|
|
*/ |
|
168
|
|
|
public function getAccountCategories() |
|
169
|
|
|
{ |
|
170
|
|
|
return $this->hasMany(AccountCategory::class, ['user_id' => 'id']); |
|
171
|
|
|
} |
|
172
|
|
|
} |
|
173
|
|
|
|