MDL-61424 admin: reset registration if token is rejected

This commit is contained in:
Marina Glancy 2018-06-06 12:54:50 +08:00
parent 6e8235c7d3
commit 9991670ff9
5 changed files with 56 additions and 6 deletions

View File

@ -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'));
}
}

View File

@ -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.';

View File

@ -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]]);
}
/**

View File

@ -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
*

View File

@ -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);
}
}