diff --git a/auth/cas/db/upgrade.php b/auth/cas/db/upgrade.php index dc0a265eb15..7c7c1c05430 100644 --- a/auth/cas/db/upgrade.php +++ b/auth/cas/db/upgrade.php @@ -62,6 +62,7 @@ function xmldb_auth_cas_upgrade($oldversion) { if ($oldversion < 2017020700) { // Convert info in config plugins from auth/cas to auth_cas. upgrade_fix_config_auth_plugin_names('cas'); + upgrade_fix_config_auth_plugin_defaults('cas'); upgrade_plugin_savepoint(true, 2017020700, 'auth', 'cas'); } diff --git a/auth/db/db/upgrade.php b/auth/db/db/upgrade.php index f40ea8665a9..00e18b9c69d 100644 --- a/auth/db/db/upgrade.php +++ b/auth/db/db/upgrade.php @@ -38,6 +38,7 @@ function xmldb_auth_db_upgrade($oldversion) { if ($oldversion < 2017032800) { // Convert info in config plugins from auth/db to auth_db upgrade_fix_config_auth_plugin_names('db'); + upgrade_fix_config_auth_plugin_defaults('db'); upgrade_plugin_savepoint(true, 2017032800, 'auth', 'db'); } diff --git a/auth/email/db/upgrade.php b/auth/email/db/upgrade.php index f1b9ca746ca..cd0ed73f928 100644 --- a/auth/email/db/upgrade.php +++ b/auth/email/db/upgrade.php @@ -38,6 +38,7 @@ function xmldb_auth_email_upgrade($oldversion) { if ($oldversion < 2017020700) { // Convert info in config plugins from auth/email to auth_email. upgrade_fix_config_auth_plugin_names('email'); + upgrade_fix_config_auth_plugin_defaults('email'); upgrade_plugin_savepoint(true, 2017020700, 'auth', 'email'); } diff --git a/auth/fc/db/upgrade.php b/auth/fc/db/upgrade.php index 4aefd465e74..61a9bab38fa 100644 --- a/auth/fc/db/upgrade.php +++ b/auth/fc/db/upgrade.php @@ -38,6 +38,7 @@ function xmldb_auth_fc_upgrade($oldversion) { if ($oldversion < 2017020700) { // Convert info in config plugins from auth/fc to auth_fc. upgrade_fix_config_auth_plugin_names('fc'); + upgrade_fix_config_auth_plugin_defaults('fc'); upgrade_plugin_savepoint(true, 2017020700, 'auth', 'fc'); } diff --git a/auth/imap/db/upgrade.php b/auth/imap/db/upgrade.php index e89b077beef..d26dac23551 100644 --- a/auth/imap/db/upgrade.php +++ b/auth/imap/db/upgrade.php @@ -38,6 +38,7 @@ function xmldb_auth_imap_upgrade($oldversion) { if ($oldversion < 2017020700) { // Convert info in config plugins from auth/imap to auth_imap. upgrade_fix_config_auth_plugin_names('imap'); + upgrade_fix_config_auth_plugin_defaults('imap'); upgrade_plugin_savepoint(true, 2017020700, 'auth', 'imap'); } diff --git a/auth/ldap/db/upgrade.php b/auth/ldap/db/upgrade.php index 4b8bab0fdc0..355b7199995 100644 --- a/auth/ldap/db/upgrade.php +++ b/auth/ldap/db/upgrade.php @@ -62,6 +62,7 @@ function xmldb_auth_ldap_upgrade($oldversion) { if ($oldversion < 2017020700) { // Convert info in config plugins from auth/ldap to auth_ldap. upgrade_fix_config_auth_plugin_names('ldap'); + upgrade_fix_config_auth_plugin_defaults('ldap'); upgrade_plugin_savepoint(true, 2017020700, 'auth', 'ldap'); } diff --git a/auth/manual/db/upgrade.php b/auth/manual/db/upgrade.php index e0dc4a6039c..ca483c02c25 100644 --- a/auth/manual/db/upgrade.php +++ b/auth/manual/db/upgrade.php @@ -50,6 +50,7 @@ function xmldb_auth_manual_upgrade($oldversion) { if ($oldversion < 2017020700) { // Convert info in config plugins from auth/manual to auth_manual. upgrade_fix_config_auth_plugin_names('manual'); + upgrade_fix_config_auth_plugin_defaults('manual'); upgrade_plugin_savepoint(true, 2017020700, 'auth', 'manual'); } diff --git a/auth/mnet/db/upgrade.php b/auth/mnet/db/upgrade.php index 1aceca6f770..46786fefb2a 100644 --- a/auth/mnet/db/upgrade.php +++ b/auth/mnet/db/upgrade.php @@ -49,6 +49,7 @@ function xmldb_auth_mnet_upgrade($oldversion) { if ($oldversion < 2017020700) { // Convert info in config plugins from auth/mnet to auth_mnet. upgrade_fix_config_auth_plugin_names('mnet'); + upgrade_fix_config_auth_plugin_defaults('mnet'); upgrade_plugin_savepoint(true, 2017020700, 'auth', 'mnet'); } diff --git a/auth/nntp/db/upgrade.php b/auth/nntp/db/upgrade.php index 4fd893da1df..8708487f9fc 100644 --- a/auth/nntp/db/upgrade.php +++ b/auth/nntp/db/upgrade.php @@ -38,6 +38,7 @@ function xmldb_auth_nntp_upgrade($oldversion) { if ($oldversion < 2017020700) { // Convert info in config plugins from auth/nntp to auth_nntp. upgrade_fix_config_auth_plugin_names('nntp'); + upgrade_fix_config_auth_plugin_defaults('nntp'); upgrade_plugin_savepoint(true, 2017020700, 'auth', 'nntp'); } diff --git a/auth/none/db/upgrade.php b/auth/none/db/upgrade.php index 2f858cba433..56bf606b719 100644 --- a/auth/none/db/upgrade.php +++ b/auth/none/db/upgrade.php @@ -38,6 +38,7 @@ function xmldb_auth_none_upgrade($oldversion) { if ($oldversion < 2017020700) { // Convert info in config plugins from auth/none to auth_none. upgrade_fix_config_auth_plugin_names('none'); + upgrade_fix_config_auth_plugin_defaults('none'); upgrade_plugin_savepoint(true, 2017020700, 'auth', 'none'); } diff --git a/auth/pam/db/upgrade.php b/auth/pam/db/upgrade.php index 480fe3955e1..e69eb57abfa 100644 --- a/auth/pam/db/upgrade.php +++ b/auth/pam/db/upgrade.php @@ -38,6 +38,7 @@ function xmldb_auth_pam_upgrade($oldversion) { if ($oldversion < 2017020700) { // Convert info in config plugins from auth/pam to auth_pam. upgrade_fix_config_auth_plugin_names('pam'); + upgrade_fix_config_auth_plugin_defaults('pam'); upgrade_plugin_savepoint(true, 2017020700, 'auth', 'pam'); } diff --git a/auth/pop3/db/upgrade.php b/auth/pop3/db/upgrade.php index ea91a444e26..e540c77b2eb 100644 --- a/auth/pop3/db/upgrade.php +++ b/auth/pop3/db/upgrade.php @@ -38,6 +38,7 @@ function xmldb_auth_pop3_upgrade($oldversion) { if ($oldversion < 2017020700) { // Convert info in config plugins from auth/pop3 to auth_pop3. upgrade_fix_config_auth_plugin_names('pop3'); + upgrade_fix_config_auth_plugin_defaults('pop3'); upgrade_plugin_savepoint(true, 2017020700, 'auth', 'pop3'); } diff --git a/auth/shibboleth/db/upgrade.php b/auth/shibboleth/db/upgrade.php index 3bd2d13eaa9..38cec6c46a3 100644 --- a/auth/shibboleth/db/upgrade.php +++ b/auth/shibboleth/db/upgrade.php @@ -38,6 +38,7 @@ function xmldb_auth_shibboleth_upgrade($oldversion) { if ($oldversion < 2017020700) { // Convert info in config plugins from auth/shibboleth to auth_shibboleth. upgrade_fix_config_auth_plugin_names('shibboleth'); + upgrade_fix_config_auth_plugin_defaults('shibboleth'); upgrade_plugin_savepoint(true, 2017020700, 'auth', 'shibboleth'); } diff --git a/auth/upgrade.txt b/auth/upgrade.txt index 1a3a50a3c5f..fff9fdd86cd 100644 --- a/auth/upgrade.txt +++ b/auth/upgrade.txt @@ -3,10 +3,11 @@ information provided here is intended especially for developers. === 3.3 === -* Authentication plugins have been migrated to use the admin settings API. - Plugins should use a settings.php file to manage configurations rather than using the config.html files. - See how the helper function upgrade_fix_config_auth_plugin_names() can be used to convert the legacy settings to the - new ones. +* Authentication plugins have been migrated to use the admin settings API. Plugins should use a settings.php file to + manage configurations rather than using the old config.html files. See how the helper function + upgrade_fix_config_auth_plugin_names() can be used to convert the legacy settings to the new ones. Another helper + function upgrade_fix_config_auth_plugin_defaults() can be used to populate the settings with default values so that + they are not falsely reported as newly added ones. * The function 'print_auth_lock_options' has been replaced by 'display_auth_lock_options' which uses the admin settings API. See auth_manual as an exmple of how it can be used. More information can be found in MDL-12689. * The list of supported identity providers (SSO IdP) returned by the 'loginpage_idp_list' method (used to render the diff --git a/lib/upgradelib.php b/lib/upgradelib.php index 12c7a6ec90b..fc81446d97f 100644 --- a/lib/upgradelib.php +++ b/lib/upgradelib.php @@ -2569,3 +2569,53 @@ function upgrade_fix_config_auth_plugin_names($plugin) { unset_config($name, 'auth/'.$plugin); } } + +/** + * Populate the auth plugin settings with defaults if needed. + * + * As a result of fixing the auth plugins config storage, many settings would + * be falsely reported as new ones by admin/upgradesettings.php. We do not want + * to confuse admins so we try to reduce the bewilderment by pre-populating the + * config_plugins table with default values. This should be done only for + * disabled auth methods. The enabled methods have their settings already + * stored, so reporting actual new settings for them is valid. + * + * @param string $plugin the auth plugin name such as 'cas', 'manual' or 'mnet' + */ +function upgrade_fix_config_auth_plugin_defaults($plugin) { + global $CFG; + + $pluginman = core_plugin_manager::instance(); + $enabled = $pluginman->get_enabled_plugins('auth'); + + if (isset($enabled[$plugin])) { + // Do not touch settings of enabled auth methods. + return; + } + + // We can't directly use {@link core\plugininfo\auth::load_settings()} here + // because the plugins are not fully upgraded yet. Instead, we emulate what + // that method does. We fetch a temporary instance of the plugin's settings + // page to get access to the settings and their defaults. Note we are not + // adding that temporary instance into the admin tree. Yes, this is a hack. + + $plugininfo = $pluginman->get_plugin_info('auth_'.$plugin); + $adminroot = admin_get_root(); + $ADMIN = $adminroot; + $auth = $plugininfo; + + $section = $plugininfo->get_settings_section_name(); + $settingspath = $plugininfo->full_path('settings.php'); + + if (file_exists($settingspath)) { + $settings = new admin_settingpage($section, 'Emulated settings page for auth_'.$plugin, 'moodle/site:config'); + include($settingspath); + + if ($settings) { + // Consistently with what admin/cli/upgrade.php does, apply the default settings twice. + // I assume this is done for theoretical cases when a default value depends on an other. + admin_apply_default_settings($settings, false); + admin_apply_default_settings($settings, false); + } + } +}