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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
<?php
namespace common\models;
use yii\db\ActiveRecord;
use yii\helpers\Security;
use yii\web\Identity;
/**
* Class User
* @package common\models
*
* @property integer $id
* @property string $username
* @property string $password_hash
* @property string $password_reset_token
* @property string $email
* @property string $auth_key
* @property integer $role
* @property integer $status
* @property integer $create_time
* @property integer $update_time
*/
class User extends ActiveRecord implements Identity
{
/**
* @var string the raw password. Used to collect password input and isn't saved in database
*/
public $password;
const STATUS_DELETED = 0;
const STATUS_ACTIVE = 10;
const ROLE_USER = 10;
public function behaviors()
{
return array(
'timestamp' => array(
'class' => 'yii\behaviors\AutoTimestamp',
'attributes' => array(
ActiveRecord::EVENT_BEFORE_INSERT => array('create_time', 'update_time'),
ActiveRecord::EVENT_BEFORE_UPDATE => 'update_time',
),
),
);
}
/**
* Finds an identity by the given ID.
*
* @param string|integer $id the ID to be looked for
* @return Identity|null the identity object that matches the given ID.
*/
public static function findIdentity($id)
{
return static::find($id);
}
/**
* Finds user by username
*
* @param string $username
* @return null|User
*/
public static function findByUsername($username)
{
return static::find(array('username' => $username, 'status' => static::STATUS_ACTIVE));
}
/**
* @return int|string current user ID
*/
public function getId()
{
return $this->id;
}
/**
* @return string current user auth key
*/
public function getAuthKey()
{
return $this->auth_key;
}
/**
* @param string $authKey
* @return boolean if auth key is valid for current user
*/
public function validateAuthKey($authKey)
{
return $this->getAuthKey() === $authKey;
}
/**
* @param string $password password to validate
* @return bool if password provided is valid for current user
*/
public function validatePassword($password)
{
return Security::validatePassword($password, $this->password_hash);
}
public function rules()
{
return array(
array('username', 'filter', 'filter' => 'trim'),
array('username', 'required'),
array('username', 'string', 'min' => 2, 'max' => 255),
array('email', 'filter', 'filter' => 'trim'),
array('email', 'required'),
array('email', 'email'),
array('email', 'unique', 'message' => 'This email address has already been taken.', 'on' => 'signup'),
array('email', 'exist', 'message' => 'There is no user with such email.', 'on' => 'requestPasswordResetToken'),
array('password', 'required'),
array('password', 'string', 'min' => 6),
);
}
public function scenarios()
{
return array(
'signup' => array('username', 'email', 'password'),
'login' => array('username', 'password'),
'resetPassword' => array('password'),
'requestPasswordResetToken' => array('email'),
);
}
public function beforeSave($insert)
{
if (parent::beforeSave($insert)) {
if (($this->isNewRecord || $this->getScenario() === 'resetPassword') && !empty($this->password)) {
$this->password_hash = Security::generatePasswordHash($this->password);
}
if ($this->isNewRecord) {
$this->auth_key = Security::generateRandomKey();
}
return true;
}
return false;
}
}