Пример #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;
 }
Пример #2
0
 /**
  * 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);
 }