-
Notifications
You must be signed in to change notification settings - Fork 160
Expand file tree
/
Copy pathRegistrationForm.php
More file actions
194 lines (162 loc) · 4.55 KB
/
RegistrationForm.php
File metadata and controls
194 lines (162 loc) · 4.55 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
<?php
namespace webvimark\modules\UserManagement\models\forms;
use webvimark\modules\UserManagement\models\User;
use webvimark\modules\UserManagement\UserManagementModule;
use yii\base\Model;
use Yii;
use yii\helpers\Html;
class RegistrationForm extends Model
{
public $username;
public $password;
public $repeat_password;
public $captcha;
/**
* @inheritdoc
*/
public function rules()
{
$rules = [
['captcha', 'captcha', 'captchaAction'=>'/user-management/auth/captcha'],
[['username', 'password', 'repeat_password', 'captcha'], 'required'],
[['username', 'password', 'repeat_password'], 'trim'],
['username', 'unique',
'targetClass' => 'webvimark\modules\UserManagement\models\User',
'targetAttribute' => 'username',
],
['username', 'purgeXSS'],
['password', 'string', 'max' => 255],
['password', 'match', 'pattern' => Yii::$app->getModule('user-management')->passwordRegexp],
['repeat_password', 'compare', 'compareAttribute'=>'password'],
];
if ( Yii::$app->getModule('user-management')->useEmailAsLogin )
{
$rules[] = ['username', 'email'];
}
else
{
$rules[] = ['username', 'string', 'max' => 50];
$rules[] = ['username', 'match', 'pattern'=>Yii::$app->getModule('user-management')->registrationRegexp];
$rules[] = ['username', 'match', 'not'=>true, 'pattern'=>Yii::$app->getModule('user-management')->registrationBlackRegexp];
}
return $rules;
}
/**
* Remove possible XSS stuff
*
* @param $attribute
*/
public function purgeXSS($attribute)
{
$this->$attribute = Html::encode($this->$attribute);
}
/**
* @return array
*/
public function attributeLabels()
{
return [
'username' => Yii::$app->getModule('user-management')->useEmailAsLogin ? 'E-mail' : UserManagementModule::t('front', 'Login'),
'password' => UserManagementModule::t('front', 'Password'),
'repeat_password' => UserManagementModule::t('front', 'Repeat password'),
'captcha' => UserManagementModule::t('front', 'Captcha'),
];
}
/**
* @param bool $performValidation
*
* @return bool|User
*/
public function registerUser($performValidation = true)
{
if ( $performValidation AND !$this->validate() )
{
return false;
}
$user = new User();
$user->username = $this->username;
$user->password = $this->password;
if ( Yii::$app->getModule('user-management')->useEmailAsLogin )
{
$user->email = $this->username;
// If email confirmation required then we save user with "inactive" status
// and without username (username will be filled with email value after confirmation)
if ( Yii::$app->getModule('user-management')->emailConfirmationRequired )
{
$user->status = User::STATUS_INACTIVE;
$user->generateConfirmationToken();
$user->save(false);
$this->saveProfile($user);
if ( $this->sendConfirmationEmail($user) )
{
return $user;
}
else
{
$this->addError('username', UserManagementModule::t('front', 'Could not send confirmation email'));
}
}
else
{
$user->username = $this->username;
}
}
if ( $user->save() )
{
$this->saveProfile($user);
return $user;
}
else
{
$this->addError('username', UserManagementModule::t('front', 'Login has been taken'));
}
}
/**
* Implement your own logic if you have user profile and save some there after registration
*
* @param User $user
*/
protected function saveProfile($user)
{
}
/**
* @param User $user
*
* @return bool
*/
protected function sendConfirmationEmail($user)
{
return Yii::$app->mailer->compose(Yii::$app->getModule('user-management')->mailerOptions['registrationFormViewFile'], ['user' => $user])
->setFrom(Yii::$app->getModule('user-management')->mailerOptions['from'])
->setTo($user->email)
->setSubject(UserManagementModule::t('front', 'E-mail confirmation for') . ' ' . Yii::$app->name)
->send();
}
/**
* Check received confirmation token and if user found - activate it, set username, roles and log him in
*
* @param string $token
*
* @return bool|User
*/
public function checkConfirmationToken($token)
{
$user = User::findInactiveByConfirmationToken($token);
if ( $user )
{
$user->username = $user->email;
$user->status = User::STATUS_ACTIVE;
$user->email_confirmed = 1;
$user->removeConfirmationToken();
$user->save(false);
$roles = (array)Yii::$app->getModule('user-management')->rolesAfterRegistration;
foreach ($roles as $role)
{
User::assignRole($user->id, $role);
}
Yii::$app->user->login($user);
return $user;
}
return false;
}
}