1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* Base controller class for users to register. Provides extension hooks to |
5
|
|
|
* allow third party overwriting of both index and register form actions |
6
|
|
|
* |
7
|
|
|
* This controller is also used to allow registered accounts to "verify" |
8
|
|
|
* their details via email. |
9
|
|
|
* |
10
|
|
|
* This is done by adding verified users to the groups stipulated by the |
11
|
|
|
* $verification_groups config variable |
12
|
|
|
* |
13
|
|
|
*/ |
14
|
|
|
class Users_Register_Controller extends Controller |
|
|
|
|
15
|
|
|
{ |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* URL That you can access this from |
19
|
|
|
* |
20
|
|
|
* @config |
21
|
|
|
*/ |
22
|
|
|
private static $url_segment = "users/register"; |
|
|
|
|
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* Current actions available to this controller |
26
|
|
|
* |
27
|
|
|
* @var array |
28
|
|
|
*/ |
29
|
|
|
private static $allowed_actions = array( |
|
|
|
|
30
|
|
|
"index", |
31
|
|
|
"sendverification", |
32
|
|
|
"verify", |
33
|
|
|
"RegisterForm" |
34
|
|
|
); |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* Internal function designed to allow us to send a verification |
38
|
|
|
* email from multiple locations |
39
|
|
|
* |
40
|
|
|
* @param $member Member object to send email to |
41
|
|
|
* @return boolean |
42
|
|
|
*/ |
43
|
|
|
protected function send_verification_email(Member $member) |
44
|
|
|
{ |
45
|
|
|
if ($member) { |
46
|
|
|
$subject = _t("Users.PleaseVerify", "Please verify your account"); |
47
|
|
|
if (Users::config()->send_email_from) { |
48
|
|
|
$from = Users::config()->send_email_from; |
49
|
|
|
} else { |
50
|
|
|
$from = Email::config()->admin_email; |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
$body = $this->renderWith( |
54
|
|
|
'UsersAccountVerification', |
55
|
|
|
array( |
56
|
|
|
"Link" => Controller::join_links( |
57
|
|
|
Director::absoluteBaseURL(), |
58
|
|
|
$this->config()->url_segment, |
59
|
|
|
"verify", |
60
|
|
|
$member->ID, |
61
|
|
|
$member->VerificationCode |
62
|
|
|
) |
63
|
|
|
) |
64
|
|
|
); |
65
|
|
|
|
66
|
|
|
$email = new Email($from, $member->Email, $subject, $body); |
67
|
|
|
$email->sendPlain(); |
68
|
|
|
|
69
|
|
|
return true; |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
return false; |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* Get the link to this controller |
77
|
|
|
* |
78
|
|
|
* @param string $action |
|
|
|
|
79
|
|
|
* @return string |
80
|
|
|
*/ |
81
|
|
|
public function Link($action = null) |
82
|
|
|
{ |
83
|
|
|
return Controller::join_links( |
84
|
|
|
$this->config()->url_segment, |
85
|
|
|
$action |
86
|
|
|
); |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
/** |
90
|
|
|
* Get an absolute link to this controller |
91
|
|
|
* |
92
|
|
|
* @param string $action |
|
|
|
|
93
|
|
|
* @return string |
|
|
|
|
94
|
|
|
*/ |
95
|
|
|
public function AbsoluteLink($action = null) |
96
|
|
|
{ |
97
|
|
|
return Director::absoluteURL($this->Link($action)); |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
/** |
101
|
|
|
* Get a relative (to the root url of the site) link to this |
102
|
|
|
* controller |
103
|
|
|
* |
104
|
|
|
* @param string $action |
|
|
|
|
105
|
|
|
* @return string |
106
|
|
|
*/ |
107
|
|
|
public function RelativeLink($action = null) |
108
|
|
|
{ |
109
|
|
|
return Controller::join_links( |
110
|
|
|
$this->Link($action) |
111
|
|
|
); |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
public function index(SS_HTTPRequest $request) |
|
|
|
|
115
|
|
|
{ |
116
|
|
|
$this->customise(array( |
117
|
|
|
'Title' => "Register", |
118
|
|
|
'ClassName' => 'RegisterPage', |
119
|
|
|
'Content' => '', |
120
|
|
|
'Form' => $this->RegisterForm(), |
121
|
|
|
)); |
122
|
|
|
|
123
|
|
|
$this->extend("updateIndexAction"); |
124
|
|
|
|
125
|
|
|
return $this->renderWith(array( |
126
|
|
|
"Users_Register", |
127
|
|
|
"Users", |
128
|
|
|
"Page" |
129
|
|
|
)); |
130
|
|
|
} |
131
|
|
|
|
132
|
|
|
|
133
|
|
|
/** |
134
|
|
|
* Send a verification email to the user provided (if verification |
135
|
|
|
* emails are enabled and account is not already verified) |
136
|
|
|
* |
137
|
|
|
*/ |
138
|
|
|
public function sendverification() |
139
|
|
|
{ |
140
|
|
|
$sent = false; |
|
|
|
|
141
|
|
|
|
142
|
|
|
if (Member::currentUserID()) { |
143
|
|
|
$member = Member::currentUser(); |
144
|
|
|
} else { |
145
|
|
|
$member = Member::get()->byID($this->request->param("ID")); |
146
|
|
|
} |
147
|
|
|
|
148
|
|
|
if ($member && !$member->isVerified() && Users::config()->send_verification_email) { |
149
|
|
|
$sent = $this->send_verification_email($member); |
150
|
|
|
} else { |
151
|
|
|
$sent = false; |
152
|
|
|
} |
153
|
|
|
|
154
|
|
|
$this->customise(array( |
155
|
|
|
"ClassName" => "RegisterPage", |
156
|
|
|
"Sent" => $sent |
157
|
|
|
)); |
158
|
|
|
|
159
|
|
|
return $this->renderWith(array( |
160
|
|
|
"Users_Register_sendverification", |
161
|
|
|
"Users", |
162
|
|
|
"Page" |
163
|
|
|
)); |
164
|
|
|
} |
165
|
|
|
|
166
|
|
|
|
167
|
|
|
/** |
168
|
|
|
* Verify the provided user (ID) using the verification code (Other |
169
|
|
|
* ID) provided |
170
|
|
|
* |
171
|
|
|
*/ |
172
|
|
|
public function verify() |
173
|
|
|
{ |
174
|
|
|
$member = Member::get()->byID($this->request->param("ID")); |
175
|
|
|
$code = $this->request->param("OtherID"); |
176
|
|
|
$verify = false; |
177
|
|
|
|
178
|
|
|
// Check verification group exists, if not, make it |
179
|
|
|
// Add a verified users group (only used if we turn on |
180
|
|
|
// verification) |
181
|
|
|
$verify_groups = Group::get() |
182
|
|
|
->filter("Code", Users::config()->verification_groups); |
183
|
|
|
|
184
|
|
|
$this->extend("onBeforeVerify", $member); |
185
|
|
|
|
186
|
|
|
if (($member && $code) && $code == $member->VerificationCode) { |
187
|
|
|
foreach ($verify_groups as $group) { |
188
|
|
|
$group->Members()->add($member); |
189
|
|
|
$verify = true; |
190
|
|
|
} |
191
|
|
|
} |
192
|
|
|
|
193
|
|
|
$this->customise(array( |
194
|
|
|
"ClassName" => "RegisterPage", |
195
|
|
|
"Verify" => $verify |
196
|
|
|
)); |
197
|
|
|
|
198
|
|
|
$this->extend("onAfterVerify", $member); |
199
|
|
|
|
200
|
|
|
return $this->renderWith(array( |
201
|
|
|
"Users_Register_verify", |
202
|
|
|
"Users", |
203
|
|
|
"Page" |
204
|
|
|
)); |
205
|
|
|
} |
206
|
|
|
|
207
|
|
|
/** |
208
|
|
|
* Registration form |
209
|
|
|
* |
210
|
|
|
* @return Form |
211
|
|
|
*/ |
212
|
|
|
public function RegisterForm() |
|
|
|
|
213
|
|
|
{ |
214
|
|
|
|
215
|
|
|
// If back URL set, push to session |
216
|
|
|
if (isset($_REQUEST['BackURL'])) { |
217
|
|
|
Session::set('BackURL', $_REQUEST['BackURL']); |
218
|
|
|
} |
219
|
|
|
|
220
|
|
|
// Setup form fields |
221
|
|
|
$fields = FieldList::create( |
222
|
|
|
TextField::create("FirstName"), |
223
|
|
|
TextField::create("Surname"), |
224
|
|
|
EmailField::create("Email"), |
225
|
|
|
ConfirmedPasswordField::create("Password") |
226
|
|
|
); |
227
|
|
|
|
228
|
|
|
// Setup form actions |
229
|
|
|
$actions = new FieldList( |
230
|
|
|
FormAction::create("doRegister", "Register") |
231
|
|
|
->addExtraClass("btn") |
232
|
|
|
->addExtraClass("btn-green") |
233
|
|
|
); |
234
|
|
|
|
235
|
|
|
// Setup required fields |
236
|
|
|
$required = new RequiredFields(array( |
237
|
|
|
"FirstName", |
238
|
|
|
"Surname", |
239
|
|
|
"Email", |
240
|
|
|
"Password" |
241
|
|
|
)); |
242
|
|
|
|
243
|
|
|
$form = Form::create($this, "RegisterForm", $fields, $actions, $required) |
244
|
|
|
->addExtraClass("forms") |
245
|
|
|
->addExtraClass("forms-columnar"); |
246
|
|
|
|
247
|
|
|
$this->extend("updateRegisterForm", $form); |
248
|
|
|
|
249
|
|
|
$session_data = Session::get("Form.{$form->FormName()}.data"); |
250
|
|
|
|
251
|
|
|
if ($session_data && is_array($session_data)) { |
252
|
|
|
$form->loadDataFrom($session_data); |
253
|
|
|
Session::clear("Form.{$form->FormName()}.data"); |
254
|
|
|
} |
255
|
|
|
|
256
|
|
|
return $form; |
257
|
|
|
} |
258
|
|
|
|
259
|
|
|
/** |
260
|
|
|
* Register a new member. This action is deigned to be intercepted at 2 |
261
|
|
|
* points: |
262
|
|
|
* |
263
|
|
|
* - Modify the initial member filter (so that you can perfom bespoke |
264
|
|
|
* member filtering |
265
|
|
|
* |
266
|
|
|
* - Modify the member user before saving (so we can add extra permissions |
267
|
|
|
* etc) |
268
|
|
|
* |
269
|
|
|
* @param array $data User submitted data |
270
|
|
|
* @param Form $form Registration form |
271
|
|
|
*/ |
272
|
|
|
public function doRegister($data, $form) |
273
|
|
|
{ |
274
|
|
|
$filter = array(); |
275
|
|
|
|
276
|
|
|
if (isset($data['Email'])) { |
277
|
|
|
$filter['Email'] = $data['Email']; |
278
|
|
|
} |
279
|
|
|
|
280
|
|
|
$this->extend("updateMemberFilter", $filter); |
281
|
|
|
|
282
|
|
|
// Check if a user already exists |
283
|
|
|
if ($member = Member::get()->filter($filter)->first()) { |
284
|
|
|
if ($member) { |
285
|
|
|
$form->addErrorMessage( |
286
|
|
|
"Blurb", |
287
|
|
|
"Sorry, an account already exists with those details.", |
288
|
|
|
"bad" |
289
|
|
|
); |
290
|
|
|
|
291
|
|
|
// Load errors into session and post back |
292
|
|
|
unset($data["Password"]); |
293
|
|
|
Session::set("Form.{$form->FormName()}.data", $data); |
|
|
|
|
294
|
|
|
|
295
|
|
|
return $this->redirectBack(); |
296
|
|
|
} |
297
|
|
|
} |
298
|
|
|
|
299
|
|
|
$member = Member::create(); |
300
|
|
|
$form->saveInto($member); |
301
|
|
|
|
302
|
|
|
// Set verification code for this user |
303
|
|
|
$member->VerificationCode = sha1(mt_rand() . mt_rand()); |
304
|
|
|
$member->write(); |
305
|
|
|
|
306
|
|
|
$this->extend("updateNewMember", $member, $data); |
307
|
|
|
|
308
|
|
|
// Add member to any groups that have been specified |
309
|
|
|
if (count(Users::config()->new_user_groups)) { |
310
|
|
|
$groups = Group::get()->filter(array( |
311
|
|
|
"Code" => Users::config()->new_user_groups |
312
|
|
|
)); |
313
|
|
|
|
314
|
|
|
foreach ($groups as $group) { |
315
|
|
|
$group->Members()->add($member); |
316
|
|
|
$group->write(); |
317
|
|
|
} |
318
|
|
|
} |
319
|
|
|
|
320
|
|
|
// Send a verification email, if needed |
321
|
|
|
if (Users::config()->send_verification_email) { |
322
|
|
|
$sent = $this->send_verification_email($member); |
|
|
|
|
323
|
|
|
} else { |
324
|
|
|
$sent = false; |
|
|
|
|
325
|
|
|
} |
326
|
|
|
|
327
|
|
|
// Login (if enabled) |
|
|
|
|
328
|
|
|
if (Users::config()->login_after_register) { |
329
|
|
|
$member->LogIn(isset($data['Remember'])); |
330
|
|
|
} |
331
|
|
|
|
332
|
|
|
// If a back URL is used in session. |
333
|
|
|
if (Session::get("BackURL")) { |
334
|
|
|
$redirect_url = Session::get("BackURL"); |
335
|
|
|
} else { |
336
|
|
|
$redirect_url = Controller::join_links( |
337
|
|
|
BASE_URL, |
338
|
|
|
Users_Account_Controller::config()->url_segment |
339
|
|
|
); |
340
|
|
|
} |
341
|
|
|
|
342
|
|
|
return $this->redirect($redirect_url); |
343
|
|
|
} |
344
|
|
|
} |
345
|
|
|
|
You can fix this by adding a namespace to your class:
When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.