From 28dddbc1299ac0f0dbac226f3cf5fcc3ca7f4829 Mon Sep 17 00:00:00 2001 From: Damyon Wiese Date: Fri, 10 Mar 2017 12:11:17 +0800 Subject: [PATCH] MDL-58219 oauth2: Show connected system account info Show the username and email of the connected system account (if it ever requires refreshing - this will help identity the account to re-authorise). Part of MDL-58220 --- admin/tool/oauth2/classes/output/renderer.php | 4 +++- auth/oauth2/db/install.xml | 6 +++--- auth/oauth2/db/upgrade.php | 13 +++++++++++++ auth/oauth2/version.php | 2 +- lib/classes/oauth2/api.php | 5 +++++ lib/classes/oauth2/system_account.php | 6 ++++++ lib/db/install.xml | 4 +++- lib/db/upgrade.php | 2 ++ version.php | 2 +- 9 files changed, 37 insertions(+), 7 deletions(-) mode change 100644 => 100755 auth/oauth2/db/install.xml diff --git a/admin/tool/oauth2/classes/output/renderer.php b/admin/tool/oauth2/classes/output/renderer.php index a9568d8de68..d38d7f00848 100644 --- a/admin/tool/oauth2/classes/output/renderer.php +++ b/admin/tool/oauth2/classes/output/renderer.php @@ -114,7 +114,9 @@ class renderer extends plugin_renderer_base { // Connected. if ($issuer->is_system_account_connected()) { - $systemauth = $OUTPUT->pix_icon('yes', get_string('systemaccountconnected', 'tool_oauth2'), 'tool_oauth2'); + $systemaccount = \core\oauth2\api::get_system_account($issuer); + $systemauth = s($systemaccount->get('email')) . ' (' . s($systemaccount->get('username')). ') '; + $systemauth .= $OUTPUT->pix_icon('yes', get_string('systemaccountconnected', 'tool_oauth2'), 'tool_oauth2'); } else { $systemauth = $OUTPUT->pix_icon('no', get_string('systemaccountnotconnected', 'tool_oauth2'), 'tool_oauth2'); } diff --git a/auth/oauth2/db/install.xml b/auth/oauth2/db/install.xml old mode 100644 new mode 100755 index 3e4933822df..be895b68d36 --- a/auth/oauth2/db/install.xml +++ b/auth/oauth2/db/install.xml @@ -1,5 +1,5 @@ - @@ -13,7 +13,7 @@ - + @@ -27,4 +27,4 @@ - + \ No newline at end of file diff --git a/auth/oauth2/db/upgrade.php b/auth/oauth2/db/upgrade.php index 45ebeb90685..e117395fec2 100644 --- a/auth/oauth2/db/upgrade.php +++ b/auth/oauth2/db/upgrade.php @@ -70,5 +70,18 @@ function xmldb_auth_oauth2_upgrade($oldversion) { upgrade_plugin_savepoint(true, 2017030700, 'auth', 'oauth2'); } + if ($oldversion < 2017031000) { + + // Changing type of field email on table auth_oauth2_linked_login to text. + $table = new xmldb_table('auth_oauth2_linked_login'); + $field = new xmldb_field('email', XMLDB_TYPE_TEXT, null, null, XMLDB_NOTNULL, null, null, 'username'); + + // Launch change of type for field email. + $dbman->change_field_type($table, $field); + + // Oauth2 savepoint reached. + upgrade_plugin_savepoint(true, 2017031000, 'auth', 'oauth2'); + } + return true; } diff --git a/auth/oauth2/version.php b/auth/oauth2/version.php index 7b2703b2298..82fb444a534 100644 --- a/auth/oauth2/version.php +++ b/auth/oauth2/version.php @@ -24,6 +24,6 @@ defined('MOODLE_INTERNAL') || die(); -$plugin->version = 2017030700; // The current plugin version (Date: YYYYMMDDXX) +$plugin->version = 2017031000; // The current plugin version (Date: YYYYMMDDXX) $plugin->requires = 2016112900; // Requires this Moodle version $plugin->component = 'auth_oauth2'; // Full name of the plugin (used for diagnostics) diff --git a/lib/classes/oauth2/api.php b/lib/classes/oauth2/api.php index 51fd8fca2c4..436e1cc983e 100644 --- a/lib/classes/oauth2/api.php +++ b/lib/classes/oauth2/api.php @@ -715,10 +715,15 @@ class api { if ($systemaccount) { $systemaccount->delete(); } + + $userinfo = $client->get_userinfo(); + $record = new stdClass(); $record->issuerid = $issuer->get('id'); $record->refreshtoken = $refreshtoken; $record->grantedscopes = $scopes; + $record->email = $userinfo['email']; + $record->username = $userinfo['username']; $systemaccount = new system_account(0, $record); diff --git a/lib/classes/oauth2/system_account.php b/lib/classes/oauth2/system_account.php index 2f770fc0315..c30b0d02fea 100644 --- a/lib/classes/oauth2/system_account.php +++ b/lib/classes/oauth2/system_account.php @@ -55,6 +55,12 @@ class system_account extends persistent { ), 'grantedscopes' => array( 'type' => PARAM_RAW, + ), + 'email' => array( + 'type' => PARAM_RAW, + ), + 'username' => array( + 'type' => PARAM_RAW, ) ); } diff --git a/lib/db/install.xml b/lib/db/install.xml index 29518ad8246..7610a685c53 100755 --- a/lib/db/install.xml +++ b/lib/db/install.xml @@ -1,5 +1,5 @@ - @@ -3510,6 +3510,8 @@ + + diff --git a/lib/db/upgrade.php b/lib/db/upgrade.php index 06988419548..5deb30d322b 100644 --- a/lib/db/upgrade.php +++ b/lib/db/upgrade.php @@ -2686,6 +2686,8 @@ function xmldb_main_upgrade($oldversion) { $table->add_field('issuerid', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, null); $table->add_field('refreshtoken', XMLDB_TYPE_TEXT, null, null, XMLDB_NOTNULL, null, null); $table->add_field('grantedscopes', XMLDB_TYPE_TEXT, null, null, XMLDB_NOTNULL, null, null); + $table->add_field('username', XMLDB_TYPE_TEXT, null, null, XMLDB_NOTNULL, null, null); + $table->add_field('email', XMLDB_TYPE_TEXT, null, null, XMLDB_NOTNULL, null, null); // Adding keys to table oauth2_system_account. $table->add_key('primary', XMLDB_KEY_PRIMARY, array('id')); diff --git a/version.php b/version.php index 14d302ac2a2..aaed5ce20e3 100644 --- a/version.php +++ b/version.php @@ -29,7 +29,7 @@ defined('MOODLE_INTERNAL') || die(); -$version = 2017033100.02; // YYYYMMDD = weekly release date of this DEV branch. +$version = 2017033100.03; // YYYYMMDD = weekly release date of this DEV branch. // RR = release increments - 00 in DEV branches. // .XX = incremental changes.