|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* Log-in form for the "member" authentication method |
|
4
|
|
|
* @package sapphire |
|
5
|
|
|
* @subpackage security |
|
6
|
|
|
*/ |
|
7
|
|
|
class MemberLoginFormWithSignup extends LoginForm |
|
|
|
|
|
|
8
|
|
|
{ |
|
9
|
|
|
|
|
10
|
|
|
/** |
|
11
|
|
|
* This field is used in the "You are logged in as %s" message |
|
12
|
|
|
* @var string |
|
13
|
|
|
*/ |
|
14
|
|
|
public $loggedInAsField = 'FirstName'; |
|
15
|
|
|
|
|
16
|
|
|
protected $authenticator_class = 'MemberAuthenticatorWithSignup'; |
|
17
|
|
|
|
|
18
|
|
|
/** |
|
19
|
|
|
* Constructor |
|
20
|
|
|
* |
|
21
|
|
|
* @param Controller $controller The parent controller, necessary to |
|
22
|
|
|
* create the appropriate form action tag. |
|
23
|
|
|
* @param string $name The method on the controller that will return this |
|
24
|
|
|
* form object. |
|
25
|
|
|
* @param FieldSet|FormField $fields All of the fields in the form - a |
|
|
|
|
|
|
26
|
|
|
* {@link FieldSet} of {@link FormField} |
|
27
|
|
|
* objects. |
|
28
|
|
|
* @param FieldSet|FormAction $actions All of the action buttons in the |
|
|
|
|
|
|
29
|
|
|
* form - a {@link FieldSet} of |
|
30
|
|
|
* {@link FormAction} objects |
|
31
|
|
|
* @param bool $checkCurrentUser If set to TRUE, it will be checked if a |
|
32
|
|
|
* the user is currently logged in, and if |
|
33
|
|
|
* so, only a logout button will be rendered |
|
34
|
|
|
* @param string $authenticatorClassName Name of the authenticator class that this form uses. |
|
|
|
|
|
|
35
|
|
|
*/ |
|
36
|
|
|
public function __construct($controller, $name, $fields = null, $actions = null, $checkCurrentUser = true) |
|
|
|
|
|
|
37
|
|
|
{ |
|
38
|
|
|
|
|
39
|
|
|
// This is now set on the class directly to make it easier to create subclasses |
|
40
|
|
|
// $this->authenticator_class = $authenticatorClassName; |
|
|
|
|
|
|
41
|
|
|
|
|
42
|
|
|
if (isset($_REQUEST['BackURL'])) { |
|
43
|
|
|
$backURL = $_REQUEST['BackURL']; |
|
44
|
|
|
} else { |
|
45
|
|
|
$backURL = Session::get('BackURL'); |
|
46
|
|
|
} |
|
47
|
|
|
$member = Member::currentUser(); |
|
48
|
|
|
$label = singleton('Member')->fieldLabel(Member::get_unique_identifier_field()); |
|
49
|
|
|
|
|
50
|
|
|
if ($checkCurrentUser && $member) { |
|
51
|
|
|
$fields = new FieldSet( |
|
52
|
|
|
new HiddenField("AuthenticationMethod", null, $this->authenticator_class, $this), |
|
|
|
|
|
|
53
|
|
|
new TextField("FirstNameSignup", "Voornaam", $member->FirstName), |
|
54
|
|
|
new TextField("SurnameSignup", "Achternaam", $member->Surname), |
|
55
|
|
|
new TextField("EmailSignup", $label, $member->Email), |
|
56
|
|
|
new PasswordField("PasswordSignup", _t('Member.PASSWORD', 'Password')) |
|
57
|
|
|
); |
|
58
|
|
|
$actions = new FieldSet( |
|
59
|
|
|
new FormAction("createorupdateaccount", _t('Member.UPDATEDETAILS', "Update your details")), |
|
|
|
|
|
|
60
|
|
|
new FormAction("logout", _t('Member.BUTTONLOGINOTHER', "Log in as someone else")) |
|
|
|
|
|
|
61
|
|
|
); |
|
62
|
|
|
} else { |
|
63
|
|
|
if (!$fields) { |
|
64
|
|
|
$fields = new FieldSet( |
|
65
|
|
|
new HiddenField("AuthenticationMethod", null, $this->authenticator_class, $this), |
|
|
|
|
|
|
66
|
|
|
new TextField("FirstNameSignup", "Voornaam", Session::get('SessionForms.MemberLoginFormWithSignup.FirstNameSignup'), null, $this), |
|
67
|
|
|
new TextField("SurnameSignup", "Achternaam", Session::get('SessionForms.MemberLoginFormWithSignup.SurnameSignup'), null, $this), |
|
68
|
|
|
new TextField("EmailSignup", $label, Session::get('SessionForms.MemberLoginFormWithSignup.EmailSignup'), null, $this), |
|
69
|
|
|
new PasswordField("PasswordSignup", _t('Member.PASSWORD', 'Password')) |
|
70
|
|
|
); |
|
71
|
|
|
if (Security::$autologin_enabled) { |
|
72
|
|
|
$fields->push( |
|
73
|
|
|
new CheckboxField( |
|
74
|
|
|
"RememberSignup", |
|
|
|
|
|
|
75
|
|
|
_t('Member.REMEMBERME', "Remember me next time?") |
|
|
|
|
|
|
76
|
|
|
) |
|
77
|
|
|
); |
|
78
|
|
|
} |
|
79
|
|
|
} |
|
80
|
|
|
if (!$actions) { |
|
81
|
|
|
$actions = new FieldSet( |
|
82
|
|
|
new FormAction('createorupdateaccount', _t('Member.BUTTONCREATEACCOUNT', "Create account")) |
|
|
|
|
|
|
83
|
|
|
); |
|
84
|
|
|
} |
|
85
|
|
|
} |
|
86
|
|
|
|
|
87
|
|
|
if (isset($backURL)) { |
|
88
|
|
|
$fields->push(new HiddenField('BackURL', 'BackURL', $backURL)); |
|
|
|
|
|
|
89
|
|
|
} |
|
90
|
|
|
$requiredFields = |
|
|
|
|
|
|
91
|
|
|
parent::__construct($controller, $name, $fields, $actions); |
|
92
|
|
|
$validator = new RequiredFields(array("EmailSignup", "FirstNameSignup", "SurnameSignup", "PasswordSignup")); |
|
93
|
|
|
$validator->setForm($this); |
|
94
|
|
|
$this->validator = $validator; |
|
95
|
|
|
|
|
96
|
|
|
// Focus on the email input when the page is loaded |
|
97
|
|
|
// Only include this if other form JS validation is enabled |
|
98
|
|
|
if ($this->getValidator()->getJavascriptValidationHandler() != 'none') { |
|
99
|
|
|
Requirements::customScript(<<<JS |
|
|
|
|
|
|
100
|
|
|
(function() { |
|
101
|
|
|
var el = document.getElementById("MemberLoginForm_LoginForm_EmailSignup"); |
|
102
|
|
|
if(el && el.focus) el.focus(); |
|
103
|
|
|
})(); |
|
104
|
|
|
JS |
|
105
|
|
|
); |
|
106
|
|
|
} |
|
107
|
|
|
} |
|
108
|
|
|
|
|
109
|
|
|
/** |
|
110
|
|
|
* Get message from session |
|
111
|
|
|
*/ |
|
112
|
|
|
protected function getMessageFromSession() |
|
113
|
|
|
{ |
|
114
|
|
|
parent::getMessageFromSession(); |
|
115
|
|
|
Session::set('MemberLoginFormWithSignup.force_message', false); |
|
|
|
|
|
|
116
|
|
|
} |
|
117
|
|
|
|
|
118
|
|
|
|
|
119
|
|
|
/** |
|
120
|
|
|
* Login form handler method |
|
121
|
|
|
* |
|
122
|
|
|
* This method is called when the user clicks on "Log in" |
|
123
|
|
|
* |
|
124
|
|
|
* @param array $data Submitted data |
|
125
|
|
|
*/ |
|
126
|
|
|
public function createorupdateaccount($data, $form) |
|
|
|
|
|
|
127
|
|
|
{ |
|
128
|
|
|
$passwordOK = true; |
|
129
|
|
|
if (!$passwordOK) { |
|
130
|
|
|
Session::set('Security.Message.message', |
|
131
|
|
|
_t('Member.PASSWORDINVALID', "Your password is not valid.") |
|
132
|
|
|
); |
|
133
|
|
|
$loginLink = Director::absoluteURL(Security::Link("login")); |
|
134
|
|
|
if ($backURL) { |
|
|
|
|
|
|
135
|
|
|
$loginLink .= '?BackURL=' . urlencode($backURL); |
|
|
|
|
|
|
136
|
|
|
} |
|
137
|
|
|
Director::redirect($loginLink . '#' . $this->FormName() .'_tab'); |
|
138
|
|
|
} |
|
139
|
|
|
if ($this->createOrUpdateUser($data, $form)) { |
|
140
|
|
|
Session::clear('SessionForms.MemberLoginForm.EmailSignup'); |
|
141
|
|
|
Session::clear('SessionForms.MemberLoginForm.FirstNameSignup'); |
|
142
|
|
|
Session::clear('SessionForms.MemberLoginForm.SurnameSignup'); |
|
143
|
|
|
Session::clear('SessionForms.MemberLoginForm.RememberSignup'); |
|
144
|
|
|
if (!isset($_REQUEST['BackURL'])) { |
|
145
|
|
|
if (Session::get("BackURL")) { |
|
146
|
|
|
$_REQUEST['BackURL'] = Session::get("BackURL"); |
|
147
|
|
|
} |
|
148
|
|
|
} |
|
149
|
|
|
Session::clear("BackURL"); |
|
150
|
|
|
if (isset($_REQUEST['BackURL']) && $_REQUEST['BackURL'] && Director::is_site_url($_REQUEST['BackURL'])) { |
|
151
|
|
|
Director::redirect($_REQUEST['BackURL']); |
|
152
|
|
|
} elseif (Security::default_login_dest()) { |
|
153
|
|
|
Director::redirect(Director::absoluteBaseURL() . Security::default_login_dest()); |
|
154
|
|
|
} else { |
|
155
|
|
|
$member = Member::currentUser(); |
|
156
|
|
|
if ($member) { |
|
157
|
|
|
$firstname = Convert::raw2xml($member->FirstName); |
|
158
|
|
|
if (!empty($data['RememberSignup'])) { |
|
159
|
|
|
Session::set('SessionForms.MemberLoginForm.RememberSignup', '1'); |
|
160
|
|
|
$member->logIn(true); |
|
161
|
|
|
} else { |
|
162
|
|
|
$member->logIn(); |
|
163
|
|
|
} |
|
164
|
|
|
Session::set('Security.Message.message', |
|
165
|
|
|
sprintf(_t('Member.THANKYOUFORCREATINGACCOUNT', "Thank you for creating an account, %s"), $firstname) |
|
166
|
|
|
); |
|
167
|
|
|
Session::set("Security.Message.type", "good"); |
|
168
|
|
|
} |
|
169
|
|
|
Director::redirectBack(); |
|
|
|
|
|
|
170
|
|
|
} |
|
171
|
|
|
} else { |
|
172
|
|
|
Session::set('Security.Message.message', |
|
173
|
|
|
_t('Member.MEMBERALREADYEXISTS', "A member with this email already exists.") |
|
174
|
|
|
); |
|
175
|
|
|
Session::set("Security.Message.type", "error"); |
|
176
|
|
|
Session::set('SessionForms.MemberLoginFormWithSignup.EmailSignupSignup', $data['EmailSignup']); |
|
177
|
|
|
Session::set('SessionForms.MemberLoginFormWithSignup.FirstNameSignup', $data['FirstNameSignup']); |
|
178
|
|
|
Session::set('SessionForms.MemberLoginFormWithSignup.SurnameSignup', $data['SurnameSignup']); |
|
179
|
|
|
Session::set('SessionForms.MemberLoginFormWithSignup.RememberSignup', isset($data['RememberSignup'])); |
|
|
|
|
|
|
180
|
|
|
if (isset($_REQUEST['BackURL'])) { |
|
181
|
|
|
$backURL = $_REQUEST['BackURL']; |
|
182
|
|
|
} else { |
|
183
|
|
|
$backURL = null; |
|
184
|
|
|
} |
|
185
|
|
|
if ($backURL) { |
|
186
|
|
|
Session::set('BackURL', $backURL); |
|
187
|
|
|
} |
|
188
|
|
|
if ($badLoginURL = Session::get("BadLoginURL")) { |
|
189
|
|
|
Director::redirect($badLoginURL); |
|
190
|
|
|
} else { |
|
191
|
|
|
// Show the right tab on failed login |
|
192
|
|
|
$loginLink = Director::absoluteURL(Security::Link("login")); |
|
193
|
|
|
if ($backURL) { |
|
194
|
|
|
$loginLink .= '?BackURL=' . urlencode($backURL); |
|
195
|
|
|
} |
|
196
|
|
|
Director::redirect($loginLink . '#' . $this->FormName() .'_tab'); |
|
197
|
|
|
} |
|
198
|
|
|
} |
|
199
|
|
|
} |
|
200
|
|
|
|
|
201
|
|
|
|
|
202
|
|
|
/** |
|
203
|
|
|
* Log out form handler method |
|
204
|
|
|
* |
|
205
|
|
|
* This method is called when the user clicks on "logout" on the form |
|
206
|
|
|
* created when the parameter <i>$checkCurrentUser</i> of the |
|
207
|
|
|
* {@link __construct constructor} was set to TRUE and the user was |
|
208
|
|
|
* currently logged in. |
|
209
|
|
|
*/ |
|
210
|
|
|
public function logout() |
|
211
|
|
|
{ |
|
212
|
|
|
$s = new Security(); |
|
213
|
|
|
$s->logout(); |
|
214
|
|
|
} |
|
215
|
|
|
|
|
216
|
|
|
|
|
217
|
|
|
/** |
|
218
|
|
|
* Try to authenticate the user |
|
219
|
|
|
* |
|
220
|
|
|
* @param array Submitted data |
|
221
|
|
|
* @return Member Returns the member object on successful authentication |
|
|
|
|
|
|
222
|
|
|
* or NULL on failure. |
|
223
|
|
|
*/ |
|
224
|
|
|
public function createOrUpdateUser($data, $form) |
|
|
|
|
|
|
225
|
|
|
{ |
|
226
|
|
|
$currentUserID = intval(Member::currentUserID()) - 0; |
|
227
|
|
|
$existingMember = DataObject::get_one("Member", "\"".Member::get_unique_identifier_field()."\" = '".Convert::raw2sql($data[Member::get_unique_identifier_field()."Signup"])."' AND \"Member\".\"ID\" <> ".$currentUserID); |
|
228
|
|
|
$loginMemberAfterCreation = true; |
|
229
|
|
|
$loggedInUser = $member = Member::currentUser(); |
|
230
|
|
|
if ($existingMember && $loggedInUser) { |
|
231
|
|
|
$this->extend('authenticationFailed', $data); |
|
232
|
|
|
return null; |
|
233
|
|
|
} elseif ($existingMember && !$loggedInUser) { |
|
234
|
|
|
$member = $existingMember; |
|
235
|
|
|
} elseif ($loggedInUser) { |
|
236
|
|
|
$loginMemberAfterCreation = false; |
|
237
|
|
|
} else { |
|
238
|
|
|
$member = new Member(); |
|
239
|
|
|
} |
|
240
|
|
|
$member->FirstName = trim(Convert::raw2sql($data["FirstNameSignup"])); |
|
241
|
|
|
$member->Surname = trim(Convert::raw2sql($data["SurnameSignup"])); |
|
242
|
|
|
$member->Email = trim(Convert::raw2sql($data["EmailSignup"])); |
|
243
|
|
|
$member->Password = trim(Convert::raw2sql($data["PasswordSignup"])); |
|
244
|
|
|
$member->write(); |
|
245
|
|
|
if ($loginMemberAfterCreation) { |
|
246
|
|
|
$member->LogIn(isset($data['RememberSignup'])); |
|
247
|
|
|
} |
|
248
|
|
|
return $member; |
|
249
|
|
|
} |
|
250
|
|
|
} |
|
251
|
|
|
|
|
252
|
|
|
|
|
253
|
|
|
|
|
254
|
|
|
|
|
255
|
|
|
class MemberLoginFormWithSignup_Validator extends RequiredFields |
|
|
|
|
|
|
256
|
|
|
{ |
|
257
|
|
|
|
|
258
|
|
|
/** |
|
259
|
|
|
* Ensures member unique id stays unique and other basic stuff... |
|
260
|
|
|
* @param $data = array Form Field Data |
|
261
|
|
|
* @return Boolean |
|
262
|
|
|
**/ |
|
263
|
|
|
public function php($data) |
|
264
|
|
|
{ |
|
265
|
|
|
$valid = parent::php($data); |
|
266
|
|
|
$uniqueFieldNameForMember = Member::get_unique_identifier_field(); |
|
267
|
|
|
$uniqueFieldNameForForm = $uniqueFieldNameForMember."Signup"; |
|
268
|
|
|
$loggedInMember = Member::currentUser(); |
|
269
|
|
|
if (isset($data[$uniqueFieldNameForForm]) && $loggedInMember && $data[$uniqueFieldNameForForm]) { |
|
270
|
|
|
if (!$loggedInMember->IsShopAdmin()) { |
|
271
|
|
|
$uniqueFieldValue = Convert::raw2sql($data[$uniqueFieldNameForForm]); |
|
272
|
|
|
$anotherMember = DataObject::get_one('Member', "\"$uniqueFieldNameForMember\" = '$uniqueFieldValue' AND \"Member\".\"ID\" <> ".$loggedInMember->ID); |
|
273
|
|
|
//can't be taken |
|
274
|
|
|
if ($anotherMember->Password) { |
|
275
|
|
|
$message = sprintf( |
|
276
|
|
|
_t("Account.ALREADYTAKEN", '%1$s is already taken by another member. Please log in or use another %2$s'), |
|
277
|
|
|
$uniqueFieldValue, |
|
278
|
|
|
$uniqueFieldNameForForm |
|
279
|
|
|
); |
|
280
|
|
|
$this->validationError( |
|
281
|
|
|
$uniqueFieldNameForForm, |
|
282
|
|
|
$message, |
|
283
|
|
|
"required" |
|
284
|
|
|
); |
|
285
|
|
|
$valid = false; |
|
286
|
|
|
} |
|
287
|
|
|
} |
|
288
|
|
|
} |
|
289
|
|
|
/* |
|
|
|
|
|
|
290
|
|
|
// check password fields are the same before saving |
|
291
|
|
|
if(isset($data["Password"]["_Password"]) && isset($data["Password"]["_ConfirmPassword"])) { |
|
292
|
|
|
if($data["Password"]["_Password"] != $data["Password"]["_ConfirmPassword"]) { |
|
293
|
|
|
$this->validationError( |
|
294
|
|
|
"Password", |
|
295
|
|
|
_t('Account.PASSWORDSERROR', 'Passwords do not match.'), |
|
296
|
|
|
"required" |
|
297
|
|
|
); |
|
298
|
|
|
$valid = false; |
|
299
|
|
|
} |
|
300
|
|
|
if(!$loggedInMember && !$data["Password"]["_Password"]) { |
|
301
|
|
|
$this->validationError( |
|
302
|
|
|
"Password", |
|
303
|
|
|
_t('Account.SELECTPASSWORD', 'Please select a password.'), |
|
304
|
|
|
"required" |
|
305
|
|
|
); |
|
306
|
|
|
$valid = false; |
|
307
|
|
|
} |
|
308
|
|
|
} |
|
309
|
|
|
* */ |
|
310
|
|
|
if (!$valid) { |
|
311
|
|
|
$this->form->sessionMessage(_t('Account.ERRORINFORM', 'We could not save your details, please check your errors below.'), "bad"); |
|
|
|
|
|
|
312
|
|
|
} |
|
313
|
|
|
return $valid; |
|
314
|
|
|
} |
|
315
|
|
|
} |
|
316
|
|
|
|
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.