-
Notifications
You must be signed in to change notification settings - Fork 148
/
User.php
153 lines (142 loc) · 4.21 KB
/
User.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
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
146
147
148
149
150
151
152
153
<?php
/**
* This is the model class for table "{{user}}".
*
* The followings are the available columns in table '{{user}}':
* @property integer $id
* @property string $password
* @property string $salt
* @property string $email
* @property string $username
* @property string $login_ip
* @property integer $login_attempts
* @property integer $login_time
* @property string $validation_key
* @property string $password_strategy
* @property boolean $requires_new_password
* @property integer $create_id
* @property integer $create_time
* @property integer $update_id
* @property integer $update_time
* @property integer $delete_id
* @property integer $delete_time
* @property integer $status
*
* @method bool verifyPassword
*
* @package YiiBoilerplate\Models
*/
class User extends CActiveRecord
{
/** @var string Field to hold a new password when user updates it. */
public $newPassword;
/** @var string Password confirmation. Is used only in validation on login. */
public $passwordConfirm;
/**
* Name of the database table associated with this ActiveRecord
*
* @return string
*/
public function tableName()
{
return 'user';
}
/**
* Behaviors associated with this ActiveRecord.
*
* We are using the APasswordBehavior because it allows neat things
* like changing the password hashing methods without rebuilding the whole user database.
*
* @see https://github.com/phpnode/YiiPassword
*
* @return array Configuration for the behavior classes.
*/
public function behaviors()
{
Yii::import('common.extensions.behaviors.password.*');
return array(
// Password behavior strategy
"APasswordBehavior" => array(
"class" => "APasswordBehavior",
"defaultStrategyName" => "bcrypt",
"strategies" => array(
"bcrypt" => array(
"class" => "ABcryptPasswordStrategy",
"workFactor" => 14,
"minLength" => 8
)
),
)
);
}
/**
* Validation rules for model attributes.
*
* @see http://www.yiiframework.com/wiki/56/
* @return array
*/
public function rules()
{
// NOTE: you should only define rules for those attributes that
// will receive user inputs.
return array(
array('email', 'email'),
array('username, email', 'unique'),
array('passwordConfirm', 'compare', 'compareAttribute' => 'newPassword', 'message' => Yii::t('validation', "Passwords don't match")),
array('newPassword, password_strategy ', 'length', 'max' => 50, 'min' => 8),
array('email, password, salt', 'length', 'max' => 255),
array('requires_new_password, login_attempts', 'numerical', 'integerOnly' => true),
// The following rule is used by search().
// Please remove those attributes that should not be searched.
array('id, username, email', 'safe', 'on' => 'search'),
);
}
/**
* Customized attribute labels (attr=>label)
*
* @return array
*/
public function attributeLabels()
{
return array(
'id' => 'ID',
'username' => Yii::t('labels', 'Username'),
'password' => Yii::t('labels', 'Password'),
'newPassword' => Yii::t('labels', 'Password'),
'passwordConfirm' => Yii::t('labels', 'Confirm password'),
'email' => Yii::t('labels', 'Email'),
);
}
/**
* Retrieves a list of models based on the current search/filter conditions.
* @return CActiveDataProvider the data provider that can return the models based on the search/filter conditions.
*/
public function search()
{
$PARTIAL = true;
$criteria = new CDbCriteria;
$criteria->compare('id', $this->id);
$criteria->compare('username', $this->username, $PARTIAL);
$criteria->compare('email', $this->email, $PARTIAL);
return new CActiveDataProvider(get_class($this), compact('criteria'));
}
/**
* Generates a new validation key (additional security for cookie)
*/
public function regenerateValidationKey()
{
$validation_key = md5(mt_rand() . mt_rand() . mt_rand());
$this->saveAttributes(compact('validation_key'));
}
/**
* Returns the static model of the specified AR class.
* Mandatory method for ActiveRecord descendants.
*
* @param string $className
* @return User the static model class
*/
public static function model($className = __CLASS__)
{
return parent::model($className);
}
}