diff --git a/blocks/microsoft/block_microsoft.php b/blocks/microsoft/block_microsoft.php index b3f2d94f9..52661064e 100644 --- a/blocks/microsoft/block_microsoft.php +++ b/blocks/microsoft/block_microsoft.php @@ -57,7 +57,7 @@ public function get_content() { $o365config = get_config('local_o365'); try { - $o365connected = $DB->record_exists('local_o365_token', ['user_id' => $USER->id]); + $o365connected = \local_o365\utils::is_o365_connected($USER->id); if ($o365connected === true) { $langconnected = get_string('o365connected', 'block_microsoft'); $this->content->text .= '
'.$langconnected.'
'; diff --git a/local/o365/classes/page/ucp.php b/local/o365/classes/page/ucp.php index c343530da..749689fcf 100644 --- a/local/o365/classes/page/ucp.php +++ b/local/o365/classes/page/ucp.php @@ -41,15 +41,7 @@ class ucp extends base { public function header() { global $USER, $DB; $this->o365loginconnected = ($USER->auth === 'oidc') ? true : false; - $this->o365connected = false; - if ($DB->record_exists('local_o365_token', ['user_id' => $USER->id])) { - $this->o365connected = true; - } else { - $oidcparams = ['resource' => 'https://graph.windows.net', 'username' => $USER->username]; - if ($DB->record_exists('auth_oidc_token', $oidcparams)) { - $this->o365connected = true; - } - } + $this->o365connected = \local_o365\utils::is_o365_connected($USER->id); return true; } diff --git a/local/o365/classes/utils.php b/local/o365/classes/utils.php new file mode 100644 index 000000000..43db4746a --- /dev/null +++ b/local/o365/classes/utils.php @@ -0,0 +1,58 @@ +. + +/** + * @package local_o365 + * @author James McQuillan + * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later + * @copyright (C) 2014 onwards Microsoft Open Technologies, Inc. (http://msopentech.com/) + */ + +namespace local_o365; + +/** + * General purpose utility class. + */ +class utils { + /** + * Determine if a user is connected to Office365. + * + * @param int $userid The user's ID. + * @return bool Whether they are connected (true) or not (false). + */ + public static function is_o365_connected($userid) { + global $DB; + try { + if ($DB->record_exists('local_o365_token', ['user_id' => $userid])) { + return true; + } else { + $sql = 'SELECT * + FROM {auth_oidc_token} tok + JOIN {user} u ON tok.username = u.username + WHERE tok.resource = ? AND u.id = ? + LIMIT 0, 1'; + $params = ['https://graph.windows.net', $userid]; + $records = $DB->get_records_sql($sql, $params); + if (!empty($records)) { + return true; + } + } + return false; + } catch (\Exception $e) { + return false; + } + } +} \ No newline at end of file diff --git a/local/onenote/classes/api/base.php b/local/onenote/classes/api/base.php index 2d94bce24..98904723a 100644 --- a/local/onenote/classes/api/base.php +++ b/local/onenote/classes/api/base.php @@ -122,7 +122,7 @@ public static function getinstance() { $msaccountclass = '\local_onenote\api\msaccount'; $o365class = '\local_onenote\api\o365'; - $iso365user = (isset($USER->auth) && $USER->auth === 'oidc' && class_exists('\local_o365\rest\onenote')) ? true : false; + $iso365user = (\local_o365\utils::is_o365_connected($USER->id) === true && class_exists('\local_o365\rest\onenote')) ? true : false; if ($iso365user === true) { require_once($CFG->dirroot.'/local/msaccount/msaccount_client.php'); $sesskey = 'msaccount_client-'.md5(\msaccount_client::SCOPE);