1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace modules\users\models; |
4
|
|
|
|
5
|
|
|
use Yii; |
6
|
|
|
use yii\base\Model; |
7
|
|
|
use modules\users\Module; |
8
|
|
|
|
9
|
|
|
/** |
10
|
|
|
* Class SignupForm |
11
|
|
|
* @package modules\users\models |
12
|
|
|
* |
13
|
|
|
* @property string $username Username |
14
|
|
|
* @property string $email Email |
15
|
|
|
* @property string $password Password |
16
|
|
|
*/ |
17
|
|
|
class SignupForm extends Model |
18
|
|
|
{ |
19
|
|
|
public $username; |
20
|
|
|
public $email; |
21
|
|
|
public $password; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* @inheritdoc |
25
|
|
|
* @return array |
26
|
|
|
*/ |
27
|
|
|
public function rules() |
28
|
|
|
{ |
29
|
|
|
return [ |
30
|
|
|
['username', 'trim'], |
31
|
|
|
['username', 'required'], |
32
|
|
|
['username', 'match', 'pattern' => '#^[\w_-]+$#i'], |
33
|
|
|
['username', 'unique', 'targetClass' => '\modules\users\models\User', 'message' => Module::t('module', 'This username already exists.')], |
34
|
|
|
['username', 'string', 'min' => 2, 'max' => 255], |
35
|
|
|
|
36
|
|
|
['email', 'trim'], |
37
|
|
|
['email', 'required'], |
38
|
|
|
['email', 'email'], |
39
|
|
|
['email', 'string', 'max' => 255], |
40
|
|
|
['email', 'unique', 'targetClass' => '\modules\users\models\User', 'message' => Module::t('module', 'This email already exists.')], |
41
|
|
|
|
42
|
|
|
['password', 'required'], |
43
|
|
|
['password', 'string', 'min' => User::LENGTH_STRING_PASSWORD_MIN, 'max' => User::LENGTH_STRING_PASSWORD_MAX], |
44
|
|
|
]; |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* @inheritdoc |
49
|
|
|
* @return array |
50
|
|
|
*/ |
51
|
|
|
public function attributeLabels() |
52
|
|
|
{ |
53
|
|
|
return [ |
54
|
|
|
'username' => Module::t('module', 'Username'), |
55
|
|
|
'email' => Module::t('module', 'Email'), |
56
|
|
|
'password' => Module::t('module', 'Password'), |
57
|
|
|
]; |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* Signs users up. |
62
|
|
|
* |
63
|
|
|
* @return User|null the saved model or null if saving fails |
64
|
|
|
* @throws \yii\base\Exception |
65
|
|
|
*/ |
66
|
|
|
public function signup() |
67
|
|
|
{ |
68
|
|
|
if ($this->validate()) { |
69
|
|
|
$user = $this->processLoadModel(); |
70
|
|
|
if ($user->save()) { |
71
|
|
|
Yii::$app->mailer->compose( |
72
|
|
|
['html' => '@modules/users/mail/emailConfirm-html', 'text' => '@modules/users/mail/emailConfirm-text'], |
73
|
|
|
['user' => $user]) |
74
|
|
|
->setFrom([Yii::$app->params['supportEmail'] => Yii::$app->name . ' robot']) |
75
|
|
|
->setTo($this->email) |
76
|
|
|
->setSubject(Yii::$app->name . ' | ' . Module::t('module', 'Account activation')) |
77
|
|
|
->send(); |
78
|
|
|
|
79
|
|
|
return $user; |
80
|
|
|
} |
81
|
|
|
} |
82
|
|
|
return null; |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
/** |
86
|
|
|
* @return User |
87
|
|
|
* @throws \yii\base\Exception |
88
|
|
|
*/ |
89
|
|
|
protected function processLoadModel() |
90
|
|
|
{ |
91
|
|
|
$user = new User(); |
92
|
|
|
$user->username = $this->username; |
93
|
|
|
$user->email = $this->email; |
94
|
|
|
$user->setPassword($this->password); |
95
|
|
|
$user->status = User::STATUS_WAIT; |
96
|
|
|
$user->generateAuthKey(); |
97
|
|
|
$user->generateEmailConfirmToken(); |
98
|
|
|
return $user; |
99
|
|
|
} |
100
|
|
|
} |
101
|
|
|
|