This commit is contained in:
Michael Hawkins
2025-05-29 12:18:17 +08:00
6 changed files with 63 additions and 11 deletions
@@ -0,0 +1,8 @@
issueNumber: MDL-83448
notes:
core:
- message: >
- Added is_site_registered_in_hub method in lib/classes/hub/api.php to
check if the site is registered or not.
- Added get_secret method in lib/classes/hub/registration.php to get site's secret.
type: improved
+7 -5
View File
@@ -34,8 +34,10 @@ admin_externalpage_setup('registrationmoodleorg');
$unregistration = optional_param('unregistration', false, PARAM_BOOL);
$confirm = optional_param('confirm', false, PARAM_BOOL);
// Consider the site 'registered' if records exist locally and at the hub.
$siteisregistered = \core\hub\registration::is_registered() && core\hub\api::is_site_registered_in_hub();
if ($unregistration && \core\hub\registration::is_registered()) {
if ($unregistration && $siteisregistered) {
if ($confirm) {
require_sesskey();
\core\hub\registration::unregister(false, false);
@@ -60,7 +62,7 @@ if (!$returnurl = optional_param('returnurl', null, PARAM_LOCALURL)) {
$returnurl = $isinitialregistration ? '/admin/index.php' : '/admin/registration/index.php';
}
$siteregistrationform = new \core\hub\site_registration_form();
$siteregistrationform = new \core\hub\site_registration_form(null, ['registered' => $siteisregistered]);
$siteregistrationform->set_data(['returnurl' => $returnurl]);
if ($fromform = $siteregistrationform->get_data()) {
@@ -86,7 +88,7 @@ echo $OUTPUT->header();
// Current status of registration.
$notificationtype = \core\output\notification::NOTIFY_ERROR;
if (\core\hub\registration::is_registered()) {
if ($siteisregistered) {
$lastupdated = \core\hub\registration::get_last_updated();
if ($lastupdated == 0) {
$registrationmessage = get_string('pleaserefreshregistrationunknown', 'admin');
@@ -104,7 +106,7 @@ if (\core\hub\registration::is_registered()) {
}
// Heading.
if (\core\hub\registration::is_registered()) {
if ($siteisregistered) {
echo $OUTPUT->heading(get_string('registerwithmoodleorgupdate', 'core_hub'));
} else if ($isinitialregistration) {
echo $OUTPUT->heading(get_string('registerwithmoodleorgcomplete', 'core_hub'));
@@ -117,7 +119,7 @@ echo $renderer->moodleorg_registration_message();
$siteregistrationform->display();
if (\core\hub\registration::is_registered()) {
if ($siteisregistered) {
// Unregister link.
$unregisterhuburl = new moodle_url("/admin/registration/index.php", ['unregistration' => 1]);
echo html_writer::div(html_writer::link($unregisterhuburl, get_string('unregister', 'hub')), 'unregister mt-2');
+15 -1
View File
@@ -96,7 +96,7 @@ class api {
} else if (isset($curloutput['exception'])) {
// Exception occurred on the remote side.
self::process_curl_exception($token, $curloutput);
} else if ($info['http_code'] != 200) {
} else if (!empty($info['http_code']) && $info['http_code'] != 200) {
throw new moodle_exception('errorconnect', 'hub', '', $info['http_code']);
} else {
return $curloutput;
@@ -164,6 +164,20 @@ class api {
return $info;
}
/**
* Checks if current site is registered in hub.
*
* @return bool
*/
public static function is_site_registered_in_hub(): bool {
global $CFG;
return self::call('hub_site_is_registered', [
'siteurl' => $CFG->wwwroot,
'sitesecret' => registration::get_secret(),
]);
}
/**
* Calls WS function hub_get_courses
*
+27 -3
View File
@@ -144,6 +144,18 @@ class registration {
return $registration->token;
}
/**
* Returns registration secret.
*
* @return string
*/
public static function get_secret(): string {
if ($registration = self::get_registration()) {
return $registration->secret;
}
return '';
}
/**
* When was the registration last updated
*
@@ -381,6 +393,8 @@ class registration {
if (!$registration || $registration->token !== $token) {
throw new moodle_exception('wrongtoken', 'hub', new moodle_url('/admin/registration/index.php'));
}
// Update hub information of the site.
$record = ['id' => $registration->id];
$record['token'] = $newtoken;
$record['confirmed'] = 1;
@@ -461,16 +475,23 @@ class registration {
public static function register($returnurl) {
global $DB, $SESSION;
if (self::is_registered()) {
// We should also check if the url is registered in the hub.
if (self::is_registered() && api::is_site_registered_in_hub()) {
// Caller of this method must make sure that site is not registered.
throw new \coding_exception('Site already registered');
}
// Delete 'confirmed' registrations.
$DB->delete_records('registration_hubs', ['confirmed' => 1]);
// Get 'unconfirmed' registration.
$hub = self::get_registration(false);
if (empty($hub)) {
// Create a new record in 'registration_hubs'.
$hub = new stdClass();
$hub->token = get_site_identifier();
// Let's add date('Ymdhis') to make the token unique.
$hub->token = get_site_identifier() . date('Ymdhis');
// Secret is identical to token until registration confirmed (confirmregistration.php).
$hub->secret = $hub->token;
$hub->huburl = HUB_MOODLEORGHUBURL;
$hub->hubname = 'moodle';
@@ -653,7 +674,10 @@ class registration {
if (!has_capability('moodle/site:config', context_system::instance())) {
return;
}
if (self::show_after_install() || self::get_new_registration_fields()) {
if (
site_is_public() &&
(self::show_after_install() || self::get_new_registration_fields())
) {
$returnurl = new moodle_url($url);
redirect(new moodle_url('/admin/registration/index.php', ['returnurl' => $returnurl->out_as_local_url(false)]));
}
+2 -1
View File
@@ -53,6 +53,7 @@ class site_registration_form extends \moodleform {
$mform = & $this->_form;
$admin = get_admin();
$site = get_site();
$registered = $this->_customdata['registered'];
$siteinfo = registration::get_site_info([
'name' => format_string($site->fullname, true, array('context' => context_course::instance(SITEID))),
@@ -168,7 +169,7 @@ class site_registration_form extends \moodleform {
$mform->addElement('static', 'siteinfosummary', get_string('sendfollowinginfo', 'hub'), registration::get_stats_summary($siteinfo));
// Check if it's a first registration or update.
if (registration::is_registered()) {
if ($registered) {
$buttonlabel = get_string('updatesiteregistration', 'core_hub');
$mform->addElement('hidden', 'update', true);
$mform->setType('update', PARAM_BOOL);
+4 -1
View File
@@ -42,7 +42,10 @@ class registration_cron_task extends scheduled_task {
* Throw exceptions on errors (the job will be retried).
*/
public function execute() {
\core\hub\registration::update_cron();
// Only execute the task if site is public.
if (site_is_public()) {
\core\hub\registration::update_cron();
}
}
}