From d5e6a73b243bcf77463daae7784d721253f14af9 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Jan=20Dagef=C3=B6rde?=
 <jan.dagefoerde@ercis.uni-muenster.de>
Date: Wed, 12 Jul 2017 16:50:22 +0200
Subject: [PATCH] bad patch -- allow email to be omitted in userinfo response

---
 lib/classes/oauth2/api.php |  2 +-
 lib/db/install.xml         |  2 +-
 lib/db/upgrade.php         | 14 ++++++++++++++
 version.php                |  2 +-
 4 files changed, 17 insertions(+), 3 deletions(-)

diff --git a/lib/classes/oauth2/api.php b/lib/classes/oauth2/api.php
index d7c0cad7d4..0e533d1d7f 100644
--- a/lib/classes/oauth2/api.php
+++ b/lib/classes/oauth2/api.php
@@ -752,7 +752,7 @@ class api {
         $record->issuerid = $issuer->get('id');
         $record->refreshtoken = $refreshtoken;
         $record->grantedscopes = $scopes;
-        $record->email = $userinfo['email'];
+        $record->email = isset($userinfo['email']) ? $userinfo['email'] : '';
         $record->username = $userinfo['username'];
 
         $systemaccount = new system_account(0, $record);
diff --git a/lib/db/install.xml b/lib/db/install.xml
index 39ab0c6230..29486634dc 100644
--- a/lib/db/install.xml
+++ b/lib/db/install.xml
@@ -3515,7 +3515,7 @@
         <FIELD NAME="issuerid" TYPE="int" LENGTH="10" NOTNULL="true" SEQUENCE="false" COMMENT="The id of the oauth 2 identity issuer"/>
         <FIELD NAME="refreshtoken" TYPE="text" NOTNULL="true" SEQUENCE="false" COMMENT="The refresh token used to request access tokens."/>
         <FIELD NAME="grantedscopes" TYPE="text" NOTNULL="true" SEQUENCE="false" COMMENT="The scopes that this system account has been granted access to."/>
-        <FIELD NAME="email" TYPE="text" NOTNULL="true" SEQUENCE="false" COMMENT="The email that was connected to this issuer."/>
+        <FIELD NAME="email" TYPE="text" NOTNULL="false" SEQUENCE="false" COMMENT="The email that was connected to this issuer."/>
         <FIELD NAME="username" TYPE="text" NOTNULL="true" SEQUENCE="false" COMMENT="The username that was connected as a system account to this issue."/>
       </FIELDS>
       <KEYS>
diff --git a/lib/db/upgrade.php b/lib/db/upgrade.php
index 6e7dbf6e67..ba8a5c037c 100644
--- a/lib/db/upgrade.php
+++ b/lib/db/upgrade.php
@@ -2875,5 +2875,19 @@ function xmldb_main_upgrade($oldversion) {
         upgrade_main_savepoint(true, 2017051500.06);
     }
 
+    if ($oldversion < 2017051500.11) {
+
+        // Changing nullability of field email on table oauth2_system_account to null.
+        $table = new xmldb_table('oauth2_system_account');
+        $field = new xmldb_field('email', XMLDB_TYPE_TEXT, null, null, null, null, null, 'grantedscopes');
+
+        // Launch change of nullability for field email.
+        $dbman->change_field_notnull($table, $field);
+
+        // Main savepoint reached.
+        upgrade_main_savepoint(true, 2017051500.11);
+    }
+
+
     return true;
 }
diff --git a/version.php b/version.php
index 9075cb9fae..62953e94ee 100644
--- a/version.php
+++ b/version.php
@@ -29,7 +29,7 @@
 
 defined('MOODLE_INTERNAL') || die();
 
-$version  = 2017051500.09;              // 20170515      = branching date YYYYMMDD - do not modify!
+$version  = 2017051500.11;              // 20170515      = branching date YYYYMMDD - do not modify!
                                         //         RR    = release increments - 00 in DEV branches.
                                         //           .XX = incremental changes.
 
-- 
2.13.0

