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