Skip to content

Commit

Permalink
MSFTMPP-207: Fix o365 connection detection for non-login users.
Browse files Browse the repository at this point in the history
  • Loading branch information
jamesmcq committed Aug 5, 2015
1 parent 0c55ab7 commit e471e80
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 11 deletions.
2 changes: 1 addition & 1 deletion blocks/microsoft/block_microsoft.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 .= '<h5>'.$langconnected.'</h5>';
Expand Down
10 changes: 1 addition & 9 deletions local/o365/classes/page/ucp.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand Down
58 changes: 58 additions & 0 deletions local/o365/classes/utils.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.

/**
* @package local_o365
* @author James McQuillan <[email protected]>
* @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;
}
}
}
2 changes: 1 addition & 1 deletion local/onenote/classes/api/base.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down

0 comments on commit e471e80

Please sign in to comment.