Skip to content
This repository was archived by the owner on Oct 29, 2020. It is now read-only.

Commit 858072a

Browse files
committed
Merge pull request #1371 from desmondmorris/stub-legacy-auth
Adds legacy auth validation to stubbed function
2 parents 18c7287 + da9bc10 commit 858072a

File tree

1 file changed

+45
-5
lines changed

1 file changed

+45
-5
lines changed

lib/modules/dosomething/dosomething_user/dosomething_user.module

Lines changed: 45 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -320,16 +320,56 @@ function dosomething_user_login_validate($form, &$form_state) {
320320
* @see dosomething_user_form_alter()
321321
*/
322322
function dosomething_user_login_legacy_validate($form, &$form_state) {
323-
// Lets check user against endpoint
324-
// http://mcommons.dosomething.org/rest/user/login.json?1234
325-
// pass json object {username: 'user', 'password': '12345'}
326323

327-
// If failure, form_set_error
324+
$data = array(
325+
"username" => $form_state['values']['name'],
326+
"password" => $form_state['values']['pass'],
327+
);
328+
$data = json_encode($data);
329+
330+
$url = variable_get(
331+
'DS_LEGACY_AUTH_ENDPOINT',
332+
'http://mcommons.dosomething.org/rest/user/login.json'
333+
);
334+
335+
$url .= '?' . time();
336+
337+
$ch = curl_init($url);
338+
339+
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
340+
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
341+
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
342+
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
343+
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
344+
'Content-Type: application/json',
345+
'Content-Length: ' . strlen($data))
346+
);
347+
348+
$result = curl_exec($ch);
349+
$status = curl_getinfo($ch, CURLINFO_HTTP_CODE);
328350

329-
// If authentication works, create a new user from the JSON object returned
351+
if ($status !== 200) {
352+
// @TODO: We must make this validation smarter
353+
form_set_error($form_state['values']['name'], t('Invalid login'));
354+
return;
355+
}
330356

357+
$result = json_decode($result);
331358

359+
if (function_exists('dosomething_user_create_user_from_legacy_json')){
360+
$user = dosomething_user_create_user_from_legacy_json($result, $form_state);
361+
if ($user === FALSE) {
362+
form_set_error(
363+
$form_state['values']['name'],
364+
t('User account could not be created')
365+
);
366+
return;
367+
}
332368

369+
user_login_finalize(array(
370+
'name' => $user->name
371+
));
372+
}
333373
}
334374

335375
/**

0 commit comments

Comments
 (0)