1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace yrc\forms; |
4
|
|
|
|
5
|
|
|
use Base32\Base32; |
6
|
|
|
use Yii; |
7
|
|
|
use yrc\models\redis\Code; |
8
|
|
|
|
9
|
|
|
/** |
10
|
|
|
* @class Registration |
11
|
|
|
* Registration form |
12
|
|
|
*/ |
13
|
|
|
abstract class Registration extends \yii\base\Model |
14
|
|
|
{ |
15
|
|
|
const ACTIVATION_TOKEN_TIME = '+2 days'; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* The email |
19
|
|
|
* @var string $email |
20
|
|
|
*/ |
21
|
|
|
public $email; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* The username |
25
|
|
|
* @var string $username |
26
|
|
|
*/ |
27
|
|
|
public $username; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* The password |
31
|
|
|
* @var string $password |
32
|
|
|
*/ |
33
|
|
|
public $password; |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* The password repeated |
37
|
|
|
* @var string $password_verify |
38
|
|
|
*/ |
39
|
|
|
public $password_verify; |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* Validation rules |
43
|
|
|
* @return array |
44
|
|
|
*/ |
45
|
|
|
public function rules() |
46
|
|
|
{ |
47
|
|
|
return [ |
48
|
|
|
[['email', 'password', 'password_verify', 'username'], 'required'], |
49
|
|
|
[['email'], 'email'], |
50
|
|
|
[['email'], 'verifyUsernameOrEmail'], |
51
|
|
|
[['username'], 'verifyUsernameOrEmail'], |
52
|
|
|
[['password', 'password_verify'], 'string', 'length' => [8, 100]], |
53
|
|
|
[['username'], 'string', 'length' => [1, 100]], |
54
|
|
|
[['password_verify'], 'compare', 'compareAttribute' => 'password'] |
55
|
|
|
]; |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* Verifies the username |
60
|
|
|
* @param string $attribute |
61
|
|
|
* @param array $params |
62
|
|
|
*/ |
63
|
|
|
public function verifyUsernameOrEmail($attribute, $params) |
|
|
|
|
64
|
|
|
{ |
65
|
|
|
if (!$this->hasErrors()) { |
66
|
|
|
$user = new Yii::$app->user->identityClass; |
67
|
|
|
$user->$attribute = $this->$attribute; |
68
|
|
|
|
69
|
|
|
$user->validate([$attribute]); |
70
|
|
|
|
71
|
|
|
if ($user->hasErrors($attribute)) { |
72
|
|
|
$this->addError($attribute, $user->getErrors($attribute)); |
73
|
|
|
} |
74
|
|
|
} |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* Registers a new user |
79
|
|
|
* @return boolean |
80
|
|
|
*/ |
81
|
|
|
public function register() |
82
|
|
|
{ |
83
|
|
|
if ($this->validate()) { |
84
|
|
|
$user = new Yii::$app->user->identityClass; |
85
|
|
|
|
86
|
|
|
$user->attributes = [ |
87
|
|
|
'email' => $this->email, |
88
|
|
|
'username' => $this->username, |
89
|
|
|
'password' => $this->password, |
90
|
|
|
'otp_enabled' => 0, |
91
|
|
|
'otp_secret' => '', |
92
|
|
|
]; |
93
|
|
|
|
94
|
|
|
if ($user->save()) { |
95
|
|
|
$token = \str_replace('=', '', Base32::encode(\random_bytes(64))); |
96
|
|
|
|
97
|
|
|
$code = new Code; |
98
|
|
|
$code->hash = hash('sha256', $token . '_activate'); |
99
|
|
|
$code->user_id = $user->id; |
100
|
|
|
$code->type = 'activate_email'; |
101
|
|
|
$code->attributes = [ |
102
|
|
|
'token' => $token, |
103
|
|
|
'email' => $user->email |
104
|
|
|
]; |
105
|
|
|
$code->expires_at = strtotime(static::ACTIVATION_TOKEN_TIME); |
106
|
|
|
|
107
|
|
|
$job = Yii::$app->rpq->getQueue()->push( |
|
|
|
|
108
|
|
|
'\yrc\jobs\notifications\email\ActivationNotification', |
109
|
|
|
[ |
110
|
|
|
'email' => $user->email, |
111
|
|
|
'token' => $token, |
112
|
|
|
'user_id' => $user->id |
113
|
|
|
], |
114
|
|
|
true |
115
|
|
|
); |
116
|
|
|
|
117
|
|
|
Yii::info([ |
118
|
|
|
'message' => '[Email] Account activation notification scheduled', |
119
|
|
|
'user_id' => $user->id, |
120
|
|
|
'data' => [ |
121
|
|
|
'email' => $user->email |
122
|
|
|
], |
123
|
|
|
'job_id' => $job->getId() |
124
|
|
|
], 'yrc/forms/Registration:register'); |
125
|
|
|
|
126
|
|
|
return true; |
127
|
|
|
} |
128
|
|
|
} |
129
|
|
|
|
130
|
|
|
return false; |
131
|
|
|
} |
132
|
|
|
} |
133
|
|
|
|
This check looks for parameters that have been defined for a function or method, but which are not used in the method body.