From 9991670ff9205c002d243c0ea46dae4f0062297c Mon Sep 17 00:00:00 2001 From: Marina Glancy Date: Wed, 6 Jun 2018 12:54:50 +0800 Subject: [PATCH] MDL-61424 admin: reset registration if token is rejected --- admin/registration/index.php | 5 +-- lang/en/hub.php | 2 ++ lib/classes/hub/api.php | 33 +++++++++++++++++--- lib/classes/hub/registration.php | 19 +++++++++++ lib/classes/hub/site_unregistration_form.php | 3 ++ 5 files changed, 56 insertions(+), 6 deletions(-) diff --git a/admin/registration/index.php b/admin/registration/index.php index 57d65a3f404..62f251a4b44 100644 --- a/admin/registration/index.php +++ b/admin/registration/index.php @@ -40,8 +40,9 @@ if ($unregistration && \core\hub\registration::is_registered()) { if ($siteunregistrationform->is_cancelled()) { redirect(new moodle_url('/admin/registration/index.php')); } else if ($data = $siteunregistrationform->get_data()) { - if (\core\hub\registration::unregister($data->unpublishalladvertisedcourses, - $data->unpublishalluploadedcourses)) { + \core\hub\registration::unregister($data->unpublishalladvertisedcourses, + $data->unpublishalluploadedcourses); + if (!\core\hub\registration::is_registered()) { redirect(new moodle_url('/admin/registration/index.php')); } } diff --git a/lang/en/hub.php b/lang/en/hub.php index 4a9937d657f..2e2bcf75e9d 100644 --- a/lang/en/hub.php +++ b/lang/en/hub.php @@ -80,6 +80,7 @@ $string['errorotherhubsnotsupported'] = 'This page can no longer be used for reg $string['errorregistration'] = 'An error occurred during registration, please try again later. ({$a})'; $string['errorunpublishcourses'] = 'Due to an unexpected error, the courses could not be deleted from Moodle.net. Try again later (recommended) or contact Moodle.net administrator.'; $string['errorws'] = '{$a}'; +$string['errorwstokenreset'] = '{$a}. Registration token on this site has been reset. You can now register your site again.'; $string['existingscreenshotnumber'] = '{$a} existing screenshots. You will be able to see these screenshots on this page, only once the Moodle.net administrator enables your course.'; $string['errorregistrationupdate'] = 'An error occurred during registration update ({$a})'; $string['existingscreenshots'] = 'Existing screenshots'; @@ -210,6 +211,7 @@ $string['update'] = 'Update'; $string['updatesite'] = 'Update registration on {$a}'; $string['updatestatus'] = 'Check it now.'; $string['usedifferentemail'] = 'Use different email'; +$string['unregisterexplained'] = 'If the site with URL {$a} is registered on Moodle.net its registration will be removed.'; $string['urlalreadyregistered'] = 'Your site seems to be already registered on Moodle.net, which means something has gone wrong. Please contact the Moodle.net administrator to reset your registration so you can try again.'; $string['usersnumber'] = 'Number of users ({$a})'; $string['wrongtoken'] = 'The registration failed for some unknown reason (network?). Please try again.'; diff --git a/lib/classes/hub/api.php b/lib/classes/hub/api.php index 2c66b5844f9..75a050883fe 100644 --- a/lib/classes/hub/api.php +++ b/lib/classes/hub/api.php @@ -91,10 +91,11 @@ class api { $curloutput = @json_decode($curl->get($serverurl, $params), true); $info = $curl->get_info(); if ($curl->get_errno()) { + // Connection error. throw new moodle_exception('errorconnect', 'hub', '', $curl->error); } else if (isset($curloutput['exception'])) { - // Error message returned by web service. - throw new moodle_exception('errorws', 'hub', '', $curloutput['message']); + // Exception occurred on moodle.net . + self::process_curl_exception($token, $curloutput); } else if ($info['http_code'] != 200) { throw new moodle_exception('errorconnect', 'hub', '', $info['http_code']); } else { @@ -102,6 +103,29 @@ class api { } } + /** + * Analyses exception received from moodle.net + * + * @param string $token token used for CURL request + * @param array $curloutput output from CURL request + * @throws moodle_exception + */ + protected static function process_curl_exception($token, $curloutput) { + if (!isset($curloutput['exception'])) { + return; + } + if ($token === registration::get_token()) { + // Check if registration token was rejected or there are other problems with registration. + if (($curloutput['exception'] === 'moodle_exception' && $curloutput['errorcode'] === 'invalidtoken') + || $curloutput['exception'] === 'registration_exception') { + // Force admin to repeat site registration process. + registration::reset_token(); + throw new moodle_exception('errorwstokenreset', 'hub', '', $curloutput['message']); + } + } + throw new moodle_exception('errorws', 'hub', '', $curloutput['message']); + } + /** * Update site registration on moodle.net * @@ -109,7 +133,7 @@ class api { * @throws moodle_exception */ public static function update_registration(array $siteinfo) { - $params = array('siteinfo' => $siteinfo); + $params = array('siteinfo' => $siteinfo, 'validateurl' => 1); self::call('hub_update_site_info', $params); } @@ -276,7 +300,8 @@ class api { * @throws moodle_exception */ public static function unregister_site() { - self::call('hub_unregister_site'); + global $CFG; + self::call('hub_unregister_site', ['url' => [$CFG->wwwroot]]); } /** diff --git a/lib/classes/hub/registration.php b/lib/classes/hub/registration.php index dc09ffbb6a3..823e47f92c5 100644 --- a/lib/classes/hub/registration.php +++ b/lib/classes/hub/registration.php @@ -275,6 +275,11 @@ class registration { try { api::update_registration($siteinfo); } catch (moodle_exception $e) { + if (!self::is_registered()) { + // Token was rejected during registration update and site and locally stored token was reset, + // proceed to site registration. This method will redirect away. + self::register(''); + } \core\notification::add(get_string('errorregistrationupdate', 'hub', $e->getMessage()), \core\output\notification::NOTIFY_ERROR); return false; @@ -428,6 +433,20 @@ class registration { return true; } + /** + * Resets the registration token without changing site identifier so site can be re-registered + * + * @return bool + */ + public static function reset_token() { + global $DB; + if (!$hub = self::get_registration()) { + return true; + } + $DB->delete_records('registration_hubs', array('id' => $hub->id)); + self::$registration = null; + } + /** * Generate a new token for the site that is not registered * diff --git a/lib/classes/hub/site_unregistration_form.php b/lib/classes/hub/site_unregistration_form.php index ed4116abc7d..70fc070ee94 100644 --- a/lib/classes/hub/site_unregistration_form.php +++ b/lib/classes/hub/site_unregistration_form.php @@ -42,6 +42,7 @@ class site_unregistration_form extends \moodleform { * Form definition */ public function definition() { + global $CFG; $mform = & $this->_form; $mform->addElement('header', 'site', get_string('unregister', 'hub')); @@ -56,6 +57,8 @@ class site_unregistration_form extends \moodleform { $mform->addElement('hidden', 'unregistration', 1); $mform->setType('unregistration', PARAM_INT); + $mform->addElement('static', 'explanation', '', get_string('unregisterexplained', 'hub', $CFG->wwwroot)); + $this->add_action_buttons(true, $unregisterlabel); } }