Merge branch 'MDL-65518-master' of git://github.com/damyon/moodle

This commit is contained in:
Sara Arjona 2019-05-09 16:19:05 +02:00
commit 8b0c8764b4
6 changed files with 72 additions and 4 deletions

View File

@ -105,6 +105,13 @@ if (($hassiteconfig || has_any_capability(array(
new lang_string('sitebackpack_help', 'badges'),
1, $choices));
$warning = badges_verify_site_backpack();
if (!empty($warning)) {
$backpacksettings->add(new admin_setting_description('badges_site_backpack_verify',
new lang_string('sitebackpackverify', 'badges'),
$warning));
}
$ADMIN->add('badges', $backpacksettings);
$ADMIN->add('badges',

View File

@ -473,6 +473,21 @@ class backpack_api {
return $this->curl_request('issuers', null, null, $data);
}
/**
* Delete any user access tokens in the session so we will attempt to get new ones.
*
* @return void
*/
public function clear_system_user_session() {
global $SESSION;
$useridkey = $this->get_token_key(BADGE_USER_ID_TOKEN);
unset($SESSION->$useridkey);
$expireskey = $this->get_token_key(BADGE_EXPIRES_TOKEN);
unset($SESSION->$expireskey);
}
/**
* Authenticate using the stored email and password and save the valid access tokens.
*

View File

@ -70,6 +70,7 @@ class external_backpacks_page implements \renderable {
}
$data->backpacks[] = $backpack;
}
$data->warning = badges_verify_site_backpack();
return $data;
}

View File

@ -26,7 +26,8 @@
{
"backpacks": [
{"backpackweburl": "http://localhost/", "sitebackpack": true, "canedit": false}
]
],
"warning": "<span class='text-warning'>Could not login</span>"
}
}}
<table class="generaltable fullwidth">
@ -54,3 +55,4 @@
{{/backpacks}}
</tbody>
</table>
{{{warning}}}

View File

@ -502,8 +502,10 @@ $string['selectgroup_end'] = 'Only public collections are shown, <a href="{$a}">
$string['selectgroup_start'] = 'Select collections from your backpack to display on this site:';
$string['selecting'] = 'With selected badges...';
$string['setup'] = 'Set up connection';
$string['sitebackpack'] = 'Site backpack';
$string['sitebackpack_help'] = 'The external backpack to allow users to share their badges with. Changing this setting after users have connected their backpacks will require each user to update their backpack settings.';
$string['sitebackpack'] = 'Active external backpack';
$string['sitebackpack_help'] = 'An external backpack allows users to share their badges. Only one external backpack can be active for the site. Changing this setting after users have connected their backpacks will require each user to disconnect and reconnect from their backpack settings page.';
$string['sitebackpackverify'] = 'Backpack connection';
$string['sitebackpackwarning'] = 'Could not connect to backpack. <br/><br/>Check that the "Badge issuer email address" admin setting is the valid email for an account on the backpack website. <br/><br/>Check that the "Badge issuer password" on the <a href="{$a->url}">site backpack settings page</a>, is the correct password for the account on the backpack website. <br/><br/>The backpack returned: "{$a->warning}"';
$string['sitebadges'] = 'Site badges';
$string['sitebadges_help'] = 'Site badges can only be awarded to users for site-related activities. These include completing a set of courses or parts of user profiles. Site badges can also be issued manually by one user to another.

View File

@ -901,11 +901,14 @@ function badges_install_default_backpacks() {
* @return array
*/
function badges_get_default_issuer() {
global $CFG;
global $CFG, $SITE;
$issuer = array();
$issuerurl = new moodle_url('/badges/issuer.php');
$issuer['name'] = $CFG->badges_defaultissuername;
if (empty($issuer['name'])) {
$issuer['name'] = $SITE->fullname ? $SITE->fullname : $SITE->shortname;
}
$issuer['url'] = $issuerurl->out(false);
$issuer['email'] = $CFG->badges_defaultissuercontact;
$issuer['@context'] = OPEN_BADGES_V2_CONTEXT;
@ -1167,3 +1170,41 @@ function badge_assemble_notification(stdClass $badge) {
message_send($eventdata);
}
}
/**
* Attempt to authenticate with the site backpack credentials and return an error
* if the authentication fails. If external backpacks are not enabled, this will
* not perform any test.
*
* @return string
*/
function badges_verify_site_backpack() {
global $OUTPUT, $CFG;
if (empty($CFG->badges_allowexternalbackpack)) {
return '';
}
$backpack = badges_get_site_backpack($CFG->badges_site_backpack);
if (empty($backpack->apiversion) || ($backpack->apiversion == OPEN_BADGES_V2)) {
$backpackapi = new \core_badges\backpack_api($backpack);
// Clear any cached access tokens in the session.
$backpackapi->clear_system_user_session();
// Now attempt a login with these credentials.
$result = $backpackapi->authenticate();
if ($result === false || !empty($result->error)) {
$warning = $backpackapi->get_authentication_error();
$params = ['id' => $backpack->id, 'action' => 'edit'];
$backpackurl = (new moodle_url('/badges/backpacks.php', $params))->out(false);
$message = get_string('sitebackpackwarning', 'badges', ['url' => $backpackurl, 'warning' => $warning]);
$icon = $OUTPUT->pix_icon('i/warning', get_string('warning', 'moodle'));
return $OUTPUT->container($icon . $message, 'text-error');
}
}
return '';
}