From b7acfd645b4f71d3012e706f30dd4165c2b11e26 Mon Sep 17 00:00:00 2001 From: Dan Poltawski Date: Tue, 29 May 2012 14:35:25 +0800 Subject: [PATCH] MDL-29857 google apis: OAuth 2.0 portfolio plugins upgrade We are disabling the plugins if not configured. As part of this upgrade we will emailing admins about this change if they have a plugin configured. This is required because OAuth 2 credentials are now required in the plugin configuration. NOTE: These strings are temporary and need to be improved. --- portfolio/googledocs/db/upgrade.php | 67 +++++++++++++++++++ .../lang/en/portfolio_googledocs.php | 7 +- portfolio/googledocs/lib.php | 12 ++++ portfolio/googledocs/version.php | 2 +- portfolio/picasa/db/upgrade.php | 67 +++++++++++++++++++ portfolio/picasa/lang/en/portfolio_picasa.php | 7 +- portfolio/picasa/lib.php | 12 ++++ portfolio/picasa/version.php | 2 +- 8 files changed, 172 insertions(+), 4 deletions(-) create mode 100644 portfolio/googledocs/db/upgrade.php create mode 100644 portfolio/picasa/db/upgrade.php diff --git a/portfolio/googledocs/db/upgrade.php b/portfolio/googledocs/db/upgrade.php new file mode 100644 index 00000000000..dfdc1dca43a --- /dev/null +++ b/portfolio/googledocs/db/upgrade.php @@ -0,0 +1,67 @@ +. + +/** + * @param int $oldversion the version we are upgrading from + * @return bool result + */ +function xmldb_portfolio_googledocs_upgrade($oldversion) { + global $CFG, $DB; + + $dbman = $DB->get_manager(); + + if ($oldversion < 2012053000) { + // Delete old user preferences containing authsub tokens. + $DB->delete_records('user_preferences', array('name' => 'google_authsub_sesskey')); + upgrade_plugin_savepoint(true, 2012053000, 'portfolio', 'googledocs'); + } + + if ($oldversion < 2012053001) { + $existing = $DB->get_record('portfolio_instance', array('plugin' => 'googledocs'), '*', IGNORE_MULTIPLE); + + if ($existing) { + portfolio_googledocs_admin_upgrade_notification(); + } + + upgrade_plugin_savepoint(true, 2012053001, 'portfolio', 'googledocs'); + } + + return true; +} + +function portfolio_googledocs_admin_upgrade_notification() { + $admins = get_admins(); + + if (empty($admins)) { + return; + } + $mainadmin = reset($admins); + + foreach ($admins as $admin) { + $message = new stdClass(); + $message->component = 'moodle'; + $message->name = 'notices'; + $message->userfrom = $mainadmin; + $message->userto = $admin; + $message->smallmessage = get_string('oauth2upgrade_message_small', 'portfolio_googledocs'); + $message->subject = get_string('oauth2upgrade_message_subject', 'portfolio_googledocs'); + $message->fullmessage = get_string('oauth2upgrade_message_content', 'portfolio_googledocs'); + $message->fullmessagehtml = get_string('oauth2upgrade_message_content', 'portfolio_googledocs'); + $message->fullmessageformat = FORMAT_PLAIN; + $message->notification = 1; + message_send($message); + } +} diff --git a/portfolio/googledocs/lang/en/portfolio_googledocs.php b/portfolio/googledocs/lang/en/portfolio_googledocs.php index 0fb864dac40..c52bba14df5 100644 --- a/portfolio/googledocs/lang/en/portfolio_googledocs.php +++ b/portfolio/googledocs/lang/en/portfolio_googledocs.php @@ -23,9 +23,14 @@ */ $string['clientid'] = 'Client ID'; -$string['oauthinfo'] = '

To use the google docs portfolio you must be registered with Google. Instructions for registing your installation with Google are described in Moodle Docs. The redirect url should be set to:

{$a->callbackurl}

'; $string['noauthtoken'] = 'An authentication token has not been recieved from google. Please ensure you are allowing moodle to access your google account'; +$string['nooauthcredentials'] = 'OAuth credentials required.'; +$string['nooauthcredentials_help'] = 'To use the Google Docs portfolio plugin you must configure OAuth credentials in the portfolio settings.'; $string['nosessiontoken'] = 'A session token does not exist preventing export to google.'; +$string['oauthinfo'] = '

To use the google docs portfolio you must be registered with Google. Instructions for registing your installation with Google are described in Moodle Docs. The redirect url should be set to:

{$a->callbackurl}

'; +$string['oauth2upgrade_message_subject'] = 'Important information regarding Google Docs portfolio plugin'; +$string['oauth2upgrade_message_content'] = 'As part of the upgrade to Moodle 2.3, the Google Docs portfolio plugin has been disabled due to changes in Googles API. To re-enable your plugin, you must configure oauth credentials in this plugin.'; +$string['oauth2upgrade_message_small'] = 'The Google Docs portfolio plugin has been disabled until configured with OAuth2'; $string['pluginname'] = 'Google Docs'; $string['sendfailed'] = 'The file {$a} failed to transfer to google'; $string['secret'] = 'Secret'; diff --git a/portfolio/googledocs/lib.php b/portfolio/googledocs/lib.php index c641f096df1..9820246f9db 100644 --- a/portfolio/googledocs/lib.php +++ b/portfolio/googledocs/lib.php @@ -126,4 +126,16 @@ class portfolio_plugin_googledocs extends portfolio_plugin_push_base { $this->googleoauth = new google_oauth($clientid, $secret, $returnurl->out(false), google_docs::REALM); } + + public function instance_sanity_check() { + $clientid = $this->get_config('clientid'); + $secret = $this->get_config('secret'); + + // If there is no oauth config (e.g. plugins upgraded from < 2.3 then + // there will be no config and this plugin should be disabled. + if (empty($clientid) or empty($secret)) { + return 'nooauthcredentials'; + } + return 0; + } } diff --git a/portfolio/googledocs/version.php b/portfolio/googledocs/version.php index a64c628b0b5..5cee5db320b 100644 --- a/portfolio/googledocs/version.php +++ b/portfolio/googledocs/version.php @@ -24,7 +24,7 @@ defined('MOODLE_INTERNAL') || die(); -$plugin->version = 2012051400; // The current plugin version (Date: YYYYMMDDXX). +$plugin->version = 2012053001; // The current plugin version (Date: YYYYMMDDXX). $plugin->requires = 2012051100; // Requires this Moodle version. $plugin->component = 'portfolio_googledocs'; // Full name of the plugin (used for diagnostics). $plugin->cron = 0; diff --git a/portfolio/picasa/db/upgrade.php b/portfolio/picasa/db/upgrade.php new file mode 100644 index 00000000000..e5d1cca6d1d --- /dev/null +++ b/portfolio/picasa/db/upgrade.php @@ -0,0 +1,67 @@ +. + +/** + * @param int $oldversion the version we are upgrading from + * @return bool result + */ +function xmldb_portfolio_picasa_upgrade($oldversion) { + global $CFG, $DB; + + $dbman = $DB->get_manager(); + + if ($oldversion < 2012053000) { + // Delete old user preferences containing authsub tokens. + $DB->delete_records('user_preferences', array('name' => 'google_authsub_sesskey_picasa')); + upgrade_plugin_savepoint(true, 2012053000, 'portfolio', 'picasa'); + } + + if ($oldversion < 2012053001) { + $existing = $DB->get_record('portfolio_instance', array('plugin' => 'picasa'), '*', IGNORE_MISSING); + + if ($existing) { + portfolio_picasa_admin_upgrade_notification(); + } + + upgrade_plugin_savepoint(true, 2012053001, 'portfolio', 'picasa'); + } + + return true; +} + +function portfolio_picasa_admin_upgrade_notification() { + $admins = get_admins(); + + if (empty($admins)) { + return; + } + $mainadmin = reset($admins); + + foreach ($admins as $admin) { + $message = new stdClass(); + $message->component = 'moodle'; + $message->name = 'notices'; + $message->userfrom = $mainadmin; + $message->userto = $admin; + $message->smallmessage = get_string('oauth2upgrade_message_small', 'portfolio_picasa'); + $message->subject = get_string('oauth2upgrade_message_subject', 'portfolio_picasa'); + $message->fullmessage = get_string('oauth2upgrade_message_content', 'portfolio_picasa'); + $message->fullmessagehtml = get_string('oauth2upgrade_message_content', 'portfolio_picasa'); + $message->fullmessageformat = FORMAT_PLAIN; + $message->notification = 1; + message_send($message); + } +} diff --git a/portfolio/picasa/lang/en/portfolio_picasa.php b/portfolio/picasa/lang/en/portfolio_picasa.php index 10c12e66152..93781b6df96 100644 --- a/portfolio/picasa/lang/en/portfolio_picasa.php +++ b/portfolio/picasa/lang/en/portfolio_picasa.php @@ -23,8 +23,13 @@ */ $string['clientid'] = 'Client ID'; -$string['oauthinfo'] = '

To use the Picasa portfolio you must be registered with Google. Instructions for registing your installation with Google are described in Moodle Docs. The redirect url should be set to:

{$a->callbackurl}

'; $string['noauthtoken'] = 'An authentication token has not been recieved from google. Please ensure you are allowing moodle to access your google account'; +$string['nooauthcredentials'] = 'OAuth credentials required.'; +$string['nooauthcredentials_help'] = 'To use the Picasa portfolio plugin you must configure OAuth credentials in the portfolio settings.'; +$string['oauthinfo'] = '

To use the Picasa portfolio you must be registered with Google. Instructions for registing your installation with Google are described in Moodle Docs. The redirect url should be set to:

{$a->callbackurl}

'; +$string['oauth2upgrade_message_subject'] = 'Important information regarding Picasa portfolio plugin'; +$string['oauth2upgrade_message_content'] = 'As part of the upgrade to Moodle 2.3, the Picasa portfolio plugin has been disabled due to changes in Googles API. To re-enable your plugin, you must configure oauth credentials in this plugin.'; +$string['oauth2upgrade_message_small'] = 'The Picasa portfolio plugin has been disabled until configured with OAuth2'; $string['pluginname'] = 'Picasa'; $string['sendfailed'] = 'The file {$a} failed to transfer to picasa'; $string['secret'] = 'Secret'; diff --git a/portfolio/picasa/lib.php b/portfolio/picasa/lib.php index 6d21336214b..d637b54edca 100644 --- a/portfolio/picasa/lib.php +++ b/portfolio/picasa/lib.php @@ -126,4 +126,16 @@ class portfolio_plugin_picasa extends portfolio_plugin_push_base { $this->googleoauth = new google_oauth($clientid, $secret, $returnurl->out(false), google_picasa::REALM); } + + public function instance_sanity_check() { + $clientid = $this->get_config('clientid'); + $secret = $this->get_config('secret'); + + // If there is no oauth config (e.g. plugins upgraded from < 2.3 then + // there will be no config and this plugin should be disabled. + if (empty($clientid) or empty($secret)) { + return 'nooauthcredentials'; + } + return 0; + } } diff --git a/portfolio/picasa/version.php b/portfolio/picasa/version.php index 113c9cf9e0d..b56c5e65b00 100644 --- a/portfolio/picasa/version.php +++ b/portfolio/picasa/version.php @@ -24,7 +24,7 @@ defined('MOODLE_INTERNAL') || die(); -$plugin->version = 2012051400; // The current plugin version (Date: YYYYMMDDXX). +$plugin->version = 2012053001; // The current plugin version (Date: YYYYMMDDXX). $plugin->requires = 2012051100; // Requires this Moodle version. $plugin->component = 'portfolio_picasa'; // Full name of the plugin (used for diagnostics). $plugin->cron = 0;