1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace yii2mod\user\models; |
4
|
|
|
|
5
|
|
|
use Yii; |
6
|
|
|
use yii\base\Model; |
7
|
|
|
|
8
|
|
|
/** |
9
|
|
|
* Class SignupForm |
10
|
|
|
* |
11
|
|
|
* @package yii2mod\user\models |
12
|
|
|
*/ |
13
|
|
|
class SignupForm extends Model |
14
|
|
|
{ |
15
|
|
|
/** |
16
|
|
|
* @var string username |
17
|
|
|
*/ |
18
|
|
|
public $username; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* @var string email |
22
|
|
|
*/ |
23
|
|
|
public $email; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* @var string password |
27
|
|
|
*/ |
28
|
|
|
public $password; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* @var UserModel |
32
|
|
|
*/ |
33
|
|
|
protected $user; |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* @inheritdoc |
37
|
|
|
*/ |
38
|
|
View Code Duplication |
public function rules() |
|
|
|
|
39
|
|
|
{ |
40
|
|
|
return [ |
41
|
|
|
['username', 'trim'], |
42
|
|
|
['username', 'required'], |
43
|
|
|
['username', 'unique', 'targetClass' => UserModel::class, 'message' => Yii::t('yii2mod.user', 'This username has already been taken.')], |
44
|
|
|
['username', 'string', 'min' => 2, 'max' => 255], |
45
|
|
|
|
46
|
|
|
['email', 'trim'], |
47
|
|
|
['email', 'required'], |
48
|
|
|
['email', 'email'], |
49
|
|
|
['email', 'string', 'max' => 255], |
50
|
|
|
['email', 'unique', 'targetClass' => UserModel::class, 'message' => Yii::t('yii2mod.user', 'This email address has already been taken.')], |
51
|
|
|
|
52
|
|
|
['password', 'required'], |
53
|
|
|
['password', 'string', 'min' => 6], |
54
|
|
|
]; |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* @inheritdoc |
59
|
|
|
*/ |
60
|
|
View Code Duplication |
public function attributeLabels() |
|
|
|
|
61
|
|
|
{ |
62
|
|
|
return [ |
63
|
|
|
'username' => Yii::t('yii2mod.user', 'Username'), |
64
|
|
|
'email' => Yii::t('yii2mod.user', 'Email'), |
65
|
|
|
'password' => Yii::t('yii2mod.user', 'Password'), |
66
|
|
|
]; |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* Signs user up. |
71
|
|
|
* |
72
|
|
|
* @return UserModel|null the saved model or null if saving fails |
73
|
|
|
*/ |
74
|
|
|
public function signup() |
75
|
|
|
{ |
76
|
|
|
if (!$this->validate()) { |
77
|
|
|
return null; |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
$this->user = new UserModel(); |
81
|
|
|
$this->user->setAttributes($this->attributes); |
82
|
|
|
$this->user->setPassword($this->password); |
83
|
|
|
$this->user->setLastLogin(time()); |
84
|
|
|
$this->user->generateAuthKey(); |
85
|
|
|
|
86
|
|
|
return $this->user->save() ? $this->user : null; |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
/** |
90
|
|
|
* @return UserModel|null |
91
|
|
|
*/ |
92
|
|
|
public function getUser() |
93
|
|
|
{ |
94
|
|
|
return $this->user; |
95
|
|
|
} |
96
|
|
|
} |
97
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.