Example #1
0
 /**
  * Handles an existing Moodle user connecting to OpenID Connect.
  *
  * @param \auth_oidc\event\user_connected $event The triggered event.
  * @return bool Success/Failure.
  */
 public static function handle_oidc_user_connected(\auth_oidc\event\user_connected $event)
 {
     // Get additional tokens for the user.
     $eventdata = $event->get_data();
     if (!empty($eventdata['userid'])) {
         $clientdata = \local_o365\oauth2\clientdata::instance_from_oidc();
         if (!empty($clientdata)) {
             try {
                 $httpclient = new \local_o365\httpclient();
                 $azureresource = \local_o365\rest\calendar::get_resource();
                 $token = \local_o365\oauth2\token::instance($eventdata['userid'], $azureresource, $clientdata, $httpclient);
             } catch (\Exception $e) {
                 return false;
             }
         }
     }
     return true;
 }
Example #2
0
 /**
  * Manage calendar syncing.
  */
 public function mode_calendar()
 {
     global $DB, $USER, $OUTPUT, $PAGE;
     if (empty($this->o365connected)) {
         throw new \moodle_exception('ucp_notconnected', 'local_o365');
     }
     $outlookresource = \local_o365\rest\calendar::get_resource();
     if (empty($outlookresource)) {
         throw new \Exception('Not configured');
     }
     $httpclient = new \local_o365\httpclient();
     $clientdata = \local_o365\oauth2\clientdata::instance_from_oidc();
     $token = \local_o365\oauth2\token::instance($USER->id, $outlookresource, $clientdata, $httpclient);
     $calsync = new \local_o365\feature\calsync\main();
     $o365calendars = $calsync->get_calendars();
     $customdata = ['o365calendars' => [], 'usercourses' => enrol_get_my_courses(['id', 'fullname']), 'cancreatesiteevents' => false, 'cancreatecourseevents' => []];
     foreach ($o365calendars as $o365calendar) {
         $customdata['o365calendars'][] = ['id' => $o365calendar['Id'], 'name' => $o365calendar['Name']];
     }
     $primarycalid = $customdata['o365calendars'][0]['id'];
     // Determine permissions to create events. Determines whether user can sync from o365 to Moodle.
     $customdata['cancreatesiteevents'] = has_capability('moodle/calendar:manageentries', \context_course::instance(SITEID));
     foreach ($customdata['usercourses'] as $courseid => $course) {
         $cancreateincourse = has_capability('moodle/calendar:manageentries', \context_course::instance($courseid));
         $customdata['cancreatecourseevents'][$courseid] = $cancreateincourse;
     }
     $mform = new \local_o365\feature\calsync\form\subscriptions('?action=calendar', $customdata);
     if ($mform->is_cancelled()) {
         redirect(new \moodle_url('/local/o365/ucp.php'));
     } else {
         if ($fromform = $mform->get_data()) {
             \local_o365\feature\calsync\form\subscriptions::update_subscriptions($fromform, $primarycalid, $customdata['cancreatesiteevents'], $customdata['cancreatecourseevents']);
             redirect(new \moodle_url('/local/o365/ucp.php'));
         } else {
             $PAGE->requires->jquery();
             $defaultdata = [];
             $existingsubsrs = $DB->get_recordset('local_o365_calsub', ['user_id' => $USER->id]);
             foreach ($existingsubsrs as $existingsubrec) {
                 if ($existingsubrec->caltype === 'site') {
                     $defaultdata['sitecal']['checked'] = '1';
                     $defaultdata['sitecal']['syncwith'] = $existingsubrec->o365calid;
                     $defaultdata['sitecal']['syncbehav'] = $existingsubrec->syncbehav;
                 } else {
                     if ($existingsubrec->caltype === 'user') {
                         $defaultdata['usercal']['checked'] = '1';
                         $defaultdata['usercal']['syncwith'] = $existingsubrec->o365calid;
                         $defaultdata['usercal']['syncbehav'] = $existingsubrec->syncbehav;
                     } else {
                         if ($existingsubrec->caltype === 'course') {
                             $defaultdata['coursecal'][$existingsubrec->caltypeid]['checked'] = '1';
                             $defaultdata['coursecal'][$existingsubrec->caltypeid]['syncwith'] = $existingsubrec->o365calid;
                             $defaultdata['coursecal'][$existingsubrec->caltypeid]['syncbehav'] = $existingsubrec->syncbehav;
                         }
                     }
                 }
             }
             $existingsubsrs->close();
             $mform->set_data($defaultdata);
             echo $OUTPUT->header();
             $mform->display();
             echo $OUTPUT->footer();
         }
     }
 }
Example #3
0
 /**
  * Get a token that can be used for calendar syncing.
  *
  * @param int $muserid The ID of a Moodle user to get a token for.
  * @return \local_o365\oauth2\token|null Either a token for calendar syncing, or null if no token could be retrieved.
  */
 public function get_user_token($muserid)
 {
     $outlookresource = \local_o365\rest\calendar::get_resource();
     $usertoken = \local_o365\oauth2\token::instance($muserid, $outlookresource, $this->clientdata, $this->httpclient);
     return !empty($usertoken) ? $usertoken : null;
 }