From 657f0381d849dd048a778d0751ffcde887677148 Mon Sep 17 00:00:00 2001 From: Sara Arjona Date: Wed, 10 Jun 2020 12:05:40 +0200 Subject: [PATCH 1/3] MDL-69002 core_badges: remove site backpack verification from settings This site backpack verification is not required so, instead of running it always here, it will be moved to a separate page to let admins decide when to check it. --- admin/settings/badges.php | 7 ------- 1 file changed, 7 deletions(-) diff --git a/admin/settings/badges.php b/admin/settings/badges.php index 0ea51937c78..24333ba7260 100644 --- a/admin/settings/badges.php +++ b/admin/settings/badges.php @@ -105,13 +105,6 @@ 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', From fa1eab64476b4ab9ce0ce8c250a528236a79865e Mon Sep 17 00:00:00 2001 From: Sara Arjona Date: Wed, 10 Jun 2020 12:23:26 +0200 Subject: [PATCH 2/3] MDL-69002 core_badges: add methods to support backpack validation A more generic method has been added to the API to validate the backpack connection (for now, there was only one method for validating current backpack). Besides, a renderer has been added to display this information depending on the backpackid. --- badges/renderer.php | 31 +++++++++++++++++++++++++++++++ lang/en/badges.php | 3 +++ lib/badgeslib.php | 19 +++++++++++++++++-- 3 files changed, 51 insertions(+), 2 deletions(-) diff --git a/badges/renderer.php b/badges/renderer.php index 341ef6dbf34..c88b1d81b6a 100644 --- a/badges/renderer.php +++ b/badges/renderer.php @@ -1362,4 +1362,35 @@ class core_badges_renderer extends plugin_renderer_base { $data = $page->export_for_template($this); return parent::render_from_template('core_badges/external_backpacks_page', $data); } + + /** + * Get the result of a backpack validation with its settings. It returns: + * - A informative message if the backpack version is different from OBv2. + * - A warning with the error if it's not possible to connect to this backpack. + * - A successful message if the connection has worked. + * + * @param int $backpackid The backpack identifier. + * @return string A message with the validation result. + */ + public function render_test_backpack_result(int $backpackid): string { + // Get the backpack. + $backpack = badges_get_site_backpack($backpackid); + + // Add the header to the result. + $result = $this->heading(get_string('testbackpack', 'badges', $backpack->backpackweburl)); + + if ($backpack->apiversion != OPEN_BADGES_V2) { + // Only OBv2 supports this validation. + $result .= get_string('backpackconnectionnottested', 'badges'); + } else { + $message = badges_verify_backpack($backpackid); + if (empty($message)) { + $result .= get_string('backpackconnectionok', 'badges'); + } else { + $result .= $message; + } + } + + return $result; + } } diff --git a/lang/en/badges.php b/lang/en/badges.php index b554a2f5604..3a638c24e28 100644 --- a/lang/en/badges.php +++ b/lang/en/badges.php @@ -81,6 +81,8 @@ $string['awardedtoyou'] = 'Issued to me'; $string['awardoncron'] = 'Access to the badges was successfully enabled. Too many users can instantly earn this badge. To ensure site performance, this action will take some time to process.'; $string['awards'] = 'Recipients'; $string['backpackavailability'] = 'External badge verification'; +$string['backpackconnectionok'] = 'Backpack connection successfully established'; +$string['backpackconnectionnottested'] = 'Connection can not be tested for this backpack because only OBv2.0 backpacks support it.'; $string['backpackneedsupdate'] = 'The backpack connected to this profile does not match the backpack for the site. You need to disconnect and reconnect the backpack.'; $string['backpackavailability_help'] = 'For badge recipients to be able to prove they earned their badges from you, an external backpack service should be able to access your site and verify badges issued from it. Your site does not currently appear to be accessible, which means that badges you have already issued or will issue in the future cannot be verified. @@ -536,6 +538,7 @@ $string['targetframework'] = 'Framework'; $string['targetframework_help'] = 'The name of the external skill or standard framework.'; $string['targetcode'] = 'Code'; $string['targetcode_help'] = 'A unique string identifier for referencing the external skill or standard within its framework.'; +$string['testbackpack'] = 'Test backpack \'{$a}\''; $string['type'] = 'Type'; $string['variablesubstitution'] = 'Variable substitution in messages.'; $string['variablesubstitution_help'] = 'In a badge message, certain variables can be inserted into the subject and/or body of a message so that they will be replaced with real values when the message is sent. The variables should be inserted into the text exactly as they are shown below. The following variables can be used: diff --git a/lib/badgeslib.php b/lib/badgeslib.php index 6d58efafd10..8a40f8e2de7 100644 --- a/lib/badgeslib.php +++ b/lib/badgeslib.php @@ -1147,14 +1147,28 @@ function badge_assemble_notification(stdClass $badge) { * @return string */ function badges_verify_site_backpack() { + global $CFG; + + return badges_verify_backpack($CFG->badges_site_backpack); +} + +/** + * Attempt to authenticate with a backpack credentials and return an error + * if the authentication fails. + * If external backpacks are not enabled or the backpack version is different + * from OBv2, this will not perform any test. + * + * @param int $backpackid Backpack identifier to verify. + * @return string The result of the verification process. + */ +function badges_verify_backpack(int $backpackid) { global $OUTPUT, $CFG; if (empty($CFG->badges_allowexternalbackpack)) { return ''; } - $backpack = badges_get_site_backpack($CFG->badges_site_backpack); - + $backpack = badges_get_site_backpack($backpackid); if (empty($backpack->apiversion) || ($backpack->apiversion == OPEN_BADGES_V2)) { $backpackapi = new \core_badges\backpack_api($backpack); @@ -1174,5 +1188,6 @@ function badges_verify_site_backpack() { return $OUTPUT->container($icon . $message, 'text-error'); } } + return ''; } From 048d7eec369d8d06c686263c9b26d0199e1119ee Mon Sep 17 00:00:00 2001 From: Sara Arjona Date: Wed, 10 Jun 2020 12:27:40 +0200 Subject: [PATCH 3/3] MDL-69002 core_badges: move backpack validation to a separate page Instead of running the site backpack validation every time badges/backpacks.php page is loaded, an action button has been added to the backpacks with OB set to 2.0 to let admins running manually this verification when needed. --- badges/backpacks.php | 8 ++++++++ badges/classes/output/external_backpacks_page.php | 2 +- badges/templates/external_backpacks_page.mustache | 15 ++++++++------- lang/en/badges.php | 1 + 4 files changed, 18 insertions(+), 8 deletions(-) diff --git a/badges/backpacks.php b/badges/backpacks.php index 341d511f47b..fea1d0e6d85 100644 --- a/badges/backpacks.php +++ b/badges/backpacks.php @@ -67,6 +67,14 @@ if ($action == 'edit') { echo $output->heading(get_string('managebackpacks', 'badges')); $form->display(); +} else if ($action == 'test') { + // If no backpack has been selected, there isn't anything to test. + if (empty($id)) { + redirect($url); + } + + echo $OUTPUT->header(); + echo $output->render_test_backpack_result($id); } else { echo $OUTPUT->header(); echo $output->heading(get_string('managebackpacks', 'badges')); diff --git a/badges/classes/output/external_backpacks_page.php b/badges/classes/output/external_backpacks_page.php index aa9592fda20..2c59a5e1507 100644 --- a/badges/classes/output/external_backpacks_page.php +++ b/badges/classes/output/external_backpacks_page.php @@ -68,9 +68,9 @@ class external_backpacks_page implements \renderable { } else { $backpack->canedit = false; } + $backpack->cantest = ($backpack->apiversion == OPEN_BADGES_V2); $data->backpacks[] = $backpack; } - $data->warning = badges_verify_site_backpack(); return $data; } diff --git a/badges/templates/external_backpacks_page.mustache b/badges/templates/external_backpacks_page.mustache index 2d84733279b..ecb7135ad6f 100644 --- a/badges/templates/external_backpacks_page.mustache +++ b/badges/templates/external_backpacks_page.mustache @@ -25,9 +25,8 @@ Example context (json): { "backpacks": [ - {"backpackweburl": "http://localhost/", "sitebackpack": true, "canedit": false} - ], - "warning": "Could not login" + {"backpackweburl": "http://localhost/", "sitebackpack": true, "canedit": false, "cantest": true} + ] } }} @@ -46,13 +45,15 @@ {{/backpacks}}
{{#sitebackpack}}Yes{{/sitebackpack}} {{#canedit}} - - {{#str}}editsettings, core_badges{{/str}} - + {{#pix}}t/edit, core,{{#str}}editsettings{{/str}}{{/pix}} {{/canedit}} + {{#cantest}} + + {{#pix}}t/check, core,{{#str}}testsettings, core_badges{{/str}}{{/pix}} + + {{/cantest}}
-{{{warning}}} diff --git a/lang/en/badges.php b/lang/en/badges.php index 3a638c24e28..1033b6906bc 100644 --- a/lang/en/badges.php +++ b/lang/en/badges.php @@ -539,6 +539,7 @@ $string['targetframework_help'] = 'The name of the external skill or standard fr $string['targetcode'] = 'Code'; $string['targetcode_help'] = 'A unique string identifier for referencing the external skill or standard within its framework.'; $string['testbackpack'] = 'Test backpack \'{$a}\''; +$string['testsettings'] = 'Test settings'; $string['type'] = 'Type'; $string['variablesubstitution'] = 'Variable substitution in messages.'; $string['variablesubstitution_help'] = 'In a badge message, certain variables can be inserted into the subject and/or body of a message so that they will be replaced with real values when the message is sent. The variables should be inserted into the text exactly as they are shown below. The following variables can be used: