示例#1
0
 /**
  * Do the job.
  */
 public function execute()
 {
     if (\local_o365\utils::is_configured() !== true) {
         return false;
     }
     $aadsyncenabled = get_config('local_o365', 'aadsync');
     if (empty($aadsyncenabled) || $aadsyncenabled === 'photosynconlogin') {
         mtrace('Azure AD cron sync disabled. Nothing to do.');
         return true;
     }
     $httpclient = new \local_o365\httpclient();
     $clientdata = \local_o365\oauth2\clientdata::instance_from_oidc();
     $usersync = new \local_o365\feature\usersync\main($clientdata, $httpclient);
     $skiptoken = get_config('local_o365', 'task_usersync_lastskiptoken');
     if (empty($skiptoken)) {
         $skiptoken = '';
     }
     for ($i = 0; $i < 5; $i++) {
         $users = $usersync->get_users('default', $skiptoken);
         if (!empty($users) && is_array($users) && !empty($users['value']) && is_array($users['value'])) {
             $usersync->sync_users($users['value']);
         } else {
             // No users returned, we're likely past the last page of results. Erase deltalink state and exit loop.
             mtrace('No more users to sync.');
             set_config('task_usersync_lastskiptoken', '', 'local_o365');
             break;
         }
         $nextlink = '';
         if (isset($users['odata.nextLink'])) {
             $nextlink = $users['odata.nextLink'];
         } else {
             if (isset($users['@odata.nextLink'])) {
                 $nextlink = $users['@odata.nextLink'];
             }
         }
         // If we have an odata.nextLink, extract deltalink value and store in $deltalink for the next loop. Otherwise break.
         if (!empty($nextlink)) {
             $skiptoken = $this->extract_skiptoken($nextlink);
             if (empty($skiptoken)) {
                 $skiptoken = '';
                 mtrace('Bad odata.nextLink received.');
                 break;
             }
         } else {
             $skiptoken = '';
             mtrace('No odata.nextLink received.');
             break;
         }
     }
     if (!empty($skiptoken)) {
         mtrace('Partial user sync completed. Saving place for next run.');
     } else {
         mtrace('Full user sync completed. Resetting saved state for new run.');
     }
     set_config('task_usersync_lastskiptoken', $skiptoken, 'local_o365');
     return true;
 }
 /**
  * Test sync_users method.
  */
 public function test_sync_users()
 {
     global $CFG, $DB;
     set_config('aadsync', 'create', 'local_o365');
     for ($i = 1; $i <= 2; $i++) {
         $muser = ['auth' => 'oidc', 'deleted' => '0', 'mnethostid' => $CFG->mnet_localhost_id, 'username' => 'testuser' . $i . '@example.onmicrosoft.com', 'firstname' => 'Test', 'lastname' => 'User' . $i, 'email' => 'testuser' . $i . '@example.onmicrosoft.com', 'lang' => 'en'];
         $DB->insert_record('user', (object) $muser);
         $token = ['oidcuniqid' => '00000000-0000-0000-0000-00000000000' . $i, 'authcode' => '000', 'username' => 'testuser' . $i . '@example.onmicrosoft.com', 'scope' => 'test', 'resource' => \local_o365\rest\azuread::get_resource(), 'token' => '000', 'expiry' => '9999999999', 'refreshtoken' => 'fsdfsdf' . $i, 'idtoken' => 'sdfsdfsdf' . $i];
         $DB->insert_record('auth_oidc_token', (object) $token);
     }
     $response = ['value' => [$this->get_aad_userinfo(1), $this->get_aad_userinfo(3)]];
     $response = json_encode($response);
     $clientdata = $this->get_mock_clientdata();
     $httpclient = new \local_o365\tests\mockhttpclient();
     $httpclient->set_response($response);
     $apiclient = new \local_o365\rest\azuread($this->get_mock_token(), $httpclient);
     $usersync = new \local_o365\feature\usersync\main($clientdata, $httpclient);
     $users = $apiclient->get_users();
     $usersync->sync_users($users['value']);
     $existinguser = ['auth' => 'oidc', 'username' => '*****@*****.**'];
     $this->assertTrue($DB->record_exists('user', $existinguser));
     $createduser = ['auth' => 'oidc', 'username' => '*****@*****.**'];
     $this->assertTrue($DB->record_exists('user', $createduser));
     $createduser = $DB->get_record('user', $createduser);
     $this->assertEquals('Test', $createduser->firstname);
     $this->assertEquals('User3', $createduser->lastname);
     $this->assertEquals('*****@*****.**', $createduser->email);
     $this->assertEquals('Toronto', $createduser->city);
     $this->assertEquals('CA', $createduser->country);
     $this->assertEquals('Dev', $createduser->department);
     $this->assertEquals('en', $createduser->lang);
 }
示例#3
0
// 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 block_microsoft
 * @author James McQuillan <*****@*****.**>
 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
 * @copyright (C) 2015 onwards Microsoft Open Technologies, Inc. (http://msopentech.com/)
 */
require_once __DIR__ . '/../../config.php';
require_login();
$aadsync = get_config('local_o365', 'aadsync');
$aadsync = array_flip(explode(',', $aadsync));
// Only profile sync once for each session.
if (empty($SESSION->block_microsoft_profilesync) && isset($aadsync['photosynconlogin'])) {
    $PAGE->requires->jquery();
    $usersync = new \local_o365\feature\usersync\main();
    $usersync->assign_photo($USER->id, null);
    $SESSION->block_microsoft_profilesync = true;
}