1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace app\modules\auth\models\forms; |
4
|
|
|
|
5
|
|
|
use Yii; |
6
|
|
|
use yii\base\{Exception, UserException}; |
7
|
|
|
use app\models\entity\User; |
8
|
|
|
use app\modules\auth\services\Tokenizer; |
9
|
|
|
|
10
|
|
|
class SignupForm extends \yii\base\Model |
11
|
|
|
{ |
12
|
|
|
/** |
13
|
|
|
* @var string |
14
|
|
|
*/ |
15
|
|
|
public $email; |
16
|
|
|
/** |
17
|
|
|
* @var string |
18
|
|
|
*/ |
19
|
|
|
public $password; |
20
|
|
|
/** |
21
|
|
|
* @var string |
22
|
|
|
*/ |
23
|
|
|
public $fullName; |
24
|
|
|
/** |
25
|
|
|
* @var \app\models\entity\User |
26
|
|
|
*/ |
27
|
|
|
private $user; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* @inheritdoc |
31
|
|
|
*/ |
32
|
|
|
public function rules() |
33
|
|
|
{ |
34
|
|
|
return [ |
35
|
|
|
['fullName', 'required'], |
36
|
|
|
['fullName', 'string', 'max' => 40], |
37
|
|
|
|
38
|
|
|
['password', 'required'], |
39
|
|
|
['password', 'string', 'min' => 6, 'max' => 255], |
40
|
|
|
|
41
|
|
|
['email', 'filter', 'filter' => 'trim'], |
42
|
|
|
['email', 'required'], |
43
|
|
|
['email', 'string', 'max' => 255], |
44
|
|
|
['email', 'email'], |
45
|
|
|
['email', 'unique', |
46
|
|
|
'targetClass' => '\app\models\entity\User', |
47
|
|
|
'message' => Yii::t('app.msg', 'This email address has already been taken') |
48
|
|
|
], |
49
|
|
|
]; |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* @inheritdoc |
54
|
|
|
*/ |
55
|
|
|
public function attributeLabels() |
56
|
|
|
{ |
57
|
|
|
return [ |
58
|
|
|
'email' => Yii::t('app', 'Email'), |
59
|
|
|
'password' => Yii::t('app', 'Password'), |
60
|
|
|
'fullName' => Yii::t('app', 'Your name'), |
61
|
|
|
]; |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* Signs user up |
66
|
|
|
* |
67
|
|
|
* @throws Exception |
68
|
|
|
* @return \app\models\entity\User |
69
|
|
|
*/ |
70
|
|
|
public function signup(): User |
71
|
|
|
{ |
72
|
|
|
$user = new User(); |
73
|
|
|
$user->email = $this->email; |
74
|
|
|
$user->setPassword($this->password); |
75
|
|
|
$user->setProfile([ |
76
|
|
|
'full_name' => $this->fullName, |
77
|
|
|
]); |
78
|
|
|
$user->status = User::STATUS_ACTIVE; |
79
|
|
|
|
80
|
|
|
if (!$user->save()) { |
81
|
|
|
throw new Exception(Yii::t('app.msg', 'An error occurred while saving user')); |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
$this->user = $user; |
85
|
|
|
|
86
|
|
|
$user->updateDateLogin(); |
87
|
|
|
Yii::$app->user->login($user, 3600 * 24 * 30); |
88
|
|
|
|
89
|
|
|
$this->sendEmail(); |
90
|
|
|
|
91
|
|
|
return $this->user; |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
/** |
95
|
|
|
* Sends an email with a link, for confirm the email |
96
|
|
|
* |
97
|
|
|
* @throws UserException |
98
|
|
|
*/ |
99
|
|
|
private function sendEmail(): void |
100
|
|
|
{ |
101
|
|
|
$tokenizer = new Tokenizer(); |
102
|
|
|
if (!$tokenizer->validate($this->user->email_confirm_token)) { |
103
|
|
|
$this->user->setEmailConfirmToken($tokenizer->generate()); |
104
|
|
|
$this->user->updateAttributes([ |
105
|
|
|
'email_confirm_token' => $this->user->email_confirm_token, |
106
|
|
|
'date_confirm' => $this->user->date_confirm, |
107
|
|
|
]); |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
$sent = Yii::$app->notify->sendMessage( |
111
|
|
|
$this->email, |
112
|
|
|
Yii::t('app', 'Registration'), |
113
|
|
|
'registration', |
114
|
|
|
['user' => $this->user] |
115
|
|
|
); |
116
|
|
|
|
117
|
|
|
if (!$sent) { |
118
|
|
|
throw new UserException(Yii::t('app.msg', 'An error occurred while sending a message to activate account')); |
119
|
|
|
} |
120
|
|
|
} |
121
|
|
|
} |
122
|
|
|
|