MDL-57429 badges: handle curl problems nicely during connection creation

- Properly validate the displayer API response before sending the
verification email.
- Properly validate the displayer API response before creating the
saved connection.
This commit is contained in:
Jake Dallimore 2017-03-23 11:08:56 +08:00
parent b7cc51ca60
commit 29631e628f
3 changed files with 13 additions and 1 deletions

View File

@ -101,6 +101,8 @@ class edit_backpack_form extends moodleform {
$request = $bp->curl_request('user');
if (isset($request->status) && $request->status == 'missing') {
$errors['email'] = get_string('error:nosuchuser', 'badges');
} else if (!isset($request->status) || $request->status !== 'okay') {
$errors['email'] = get_string('backpackconnectionunexpectedresult', 'badges');
}
}
return $errors;

View File

@ -43,11 +43,20 @@ if (!is_null($storedsecret)) {
$data->email = $storedemail;
$bp = new OpenBadgesBackpackHandler($data);
// Make sure we have all the required information before trying to save the connection.
$backpackuser = $bp->curl_request('user');
if (isset($backpackuser->status) && $backpackuser->status === 'okay' && isset($backpackuser->userId)) {
$backpackuid = $backpackuser->userId;
} else {
redirect(new moodle_url($redirect), get_string('backpackconnectionunexpectedresult', 'badges'),
null, \core\output\notification::NOTIFY_ERROR);
}
$obj = new stdClass();
$obj->userid = $USER->id;
$obj->email = $data->email;
$obj->backpackurl = $data->backpackurl;
$obj->backpackuid = $bp->curl_request('user')->userId;
$obj->backpackuid = $backpackuid;
$obj->autosync = 0;
$obj->password = '';
$DB->insert_record('badge_backpack', $obj);

View File

@ -87,6 +87,7 @@ Currently, only <a href="http://backpack.openbadges.org">Mozilla OpenBadges Back
$string['backpackconnectioncancelattempt'] = 'Connect using a different email address';
$string['backpackconnectionconnect'] = 'Connect to Backpack';
$string['backpackconnectionresendemail'] = 'Resend verification email';
$string['backpackconnectionunexpectedresult'] = 'There was a problem contacting the Backpack. Please try again.<br><br>If this problem persists, please contact your system administrator.';
$string['backpackdetails'] = 'Backpack settings';
$string['backpackemail'] = 'Email address';
$string['backpackemail_help'] = 'The email address associated with your backpack. While you are connected, any badges earned on this site will be associated with this email address.';