1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace app\models; |
4
|
|
|
|
5
|
|
|
use Yii; |
6
|
|
|
use yii\helpers\ArrayHelper; |
7
|
|
|
use yii2mod\user\models\BaseUserDetailsModel; |
8
|
|
|
use yii2mod\user\models\BaseUserModel; |
9
|
|
|
|
10
|
|
|
/** |
11
|
|
|
* Class UserModel |
12
|
|
|
* @package app\models |
13
|
|
|
*/ |
14
|
|
|
class UserModel extends BaseUserModel |
15
|
|
|
{ |
16
|
|
|
/** |
17
|
|
|
* @var string password field |
18
|
|
|
*/ |
19
|
|
|
public $password; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* Returns the validation rules for attributes. |
23
|
|
|
* |
24
|
|
|
* Validation rules are used by [[validate()]] to check if attribute values are valid. |
25
|
|
|
* Child classes may override this method to declare different validation rules. |
26
|
|
|
* @return array validation rules |
27
|
|
|
* @see scenarios() |
28
|
|
|
*/ |
29
|
|
|
public function rules() |
30
|
|
|
{ |
31
|
|
|
return ArrayHelper::merge([ |
32
|
|
|
[['username', 'email'], 'required'], |
33
|
|
|
['email', 'unique', 'message' => 'This email address has already been taken.'], |
34
|
|
|
['username', 'unique', 'message' => 'This username has already been taken.'], |
35
|
|
|
['username', 'string', 'min' => 2, 'max' => 30], |
36
|
|
|
['email','email'], |
37
|
|
|
['password', 'string', 'min' => 6, 'max' => 24], |
38
|
|
|
['password', 'required', 'on' => 'createUser'], |
39
|
|
|
], parent::rules()); |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* Returns the attribute labels. |
44
|
|
|
* |
45
|
|
|
* Attribute labels are mainly used for display purpose. For example, given an attribute |
46
|
|
|
* `firstName`, we can declare a label `First Name` which is more user-friendly and can |
47
|
|
|
* be displayed to end users. |
48
|
|
|
* |
49
|
|
|
* @return array attribute labels (name => label) |
50
|
|
|
*/ |
51
|
|
|
public function attributeLabels() |
52
|
|
|
{ |
53
|
|
|
return ArrayHelper::merge([ |
54
|
|
|
//Extends AttributeLabels |
55
|
|
|
], parent::attributeLabels()); |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* update user password when new password not blank. |
60
|
|
|
* |
61
|
|
|
*/ |
62
|
|
|
|
63
|
|
|
public function beforeSave($insert) |
64
|
|
|
{ |
65
|
|
|
if (parent::beforeSave($insert)) { |
66
|
|
|
if (!$this->isNewRecord && $this->password != '') { |
67
|
|
|
$this->setPassword($this->password); |
68
|
|
|
} |
69
|
|
|
return true; |
70
|
|
|
} else { |
71
|
|
|
return false; |
72
|
|
|
} |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* Returns a list of scenarios and the corresponding active attributes. |
77
|
|
|
* @return array |
78
|
|
|
*/ |
79
|
|
|
public function scenarios() |
80
|
|
|
{ |
81
|
|
|
$scenarios = parent::scenarios(); |
82
|
|
|
return $scenarios; |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
/** |
86
|
|
|
* Create user |
87
|
|
|
* @return UserModel|null the saved model or null if saving fails |
88
|
|
|
*/ |
89
|
|
|
public function createUser() |
90
|
|
|
{ |
91
|
|
|
if ($this->validate()) { |
92
|
|
|
$this->setPassword($this->password); |
93
|
|
|
$this->generateAuthKey(); |
94
|
|
|
if ($this->save()) { |
95
|
|
|
$userDetailsModels = new BaseUserDetailsModel(); |
96
|
|
|
$userDetailsModels->userId = $this->primaryKey; |
97
|
|
|
$userDetailsModels->save(); |
98
|
|
|
} |
99
|
|
|
return $this; |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
return false; |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
} |
106
|
|
|
|