예제 #1
0
 /**
  * Get the token to authenticate with OneNote.
  *
  * @return string The token to authenticate with OneNote.
  */
 public function get_token()
 {
     global $USER;
     $httpclient = new \local_o365\httpclient();
     $clientdata = \local_o365\oauth2\clientdata::instance_from_oidc();
     $resource = \local_o365\rest\onenote::get_resource();
     $token = \local_o365\oauth2\token::instance($USER->id, $resource, $clientdata, $httpclient);
     return $token->get_token();
 }
예제 #2
0
 /**
  * Gets the instance of the correct api class. Use this method to get an instance of the api class.
  *
  * @return \local_onenote\api\base An implementation of the OneNote API.
  */
 public static function getinstance()
 {
     global $USER, $SESSION, $CFG;
     $msaccountclass = '\\local_onenote\\api\\msaccount';
     $o365class = '\\local_onenote\\api\\o365';
     $class = '';
     $iso365user = \local_o365\utils::is_o365_connected($USER->id) === true && class_exists('\\local_o365\\rest\\onenote') ? true : false;
     if ($iso365user === true) {
         $sesskey = class_exists('\\local_msaccount\\client') ? 'msaccount_client-' . md5(\local_msaccount\client::SCOPE) : null;
         $disableo365onenote = get_user_preferences('local_o365_disableo365onenote', 0);
         // If the user is logged in to msaccount OneNote, or has o365 OneNote disabled.
         $iso365user = !empty($sesskey) && !empty($SESSION->{$sesskey}) || !empty($disableo365onenote) ? false : $iso365user;
         if ($iso365user === true) {
             try {
                 $httpclient = new \local_o365\httpclient();
                 $clientdata = \local_o365\oauth2\clientdata::instance_from_oidc();
                 $onenoteresource = \local_o365\rest\onenote::get_resource();
                 $token = \local_o365\oauth2\token::instance($USER->id, $onenoteresource, $clientdata, $httpclient);
                 if (empty($token)) {
                     $iso365user = false;
                 }
             } catch (\Exception $e) {
                 $iso365user = false;
             }
         }
         if ($iso365user === true) {
             $class = $o365class;
         } else {
             $class = class_exists('\\local_msaccount\\client') ? $msaccountclass : null;
         }
     } else {
         $class = class_exists('\\local_msaccount\\client') ? $msaccountclass : null;
     }
     if (empty($class)) {
         throw new \moodle_exception('error_noapiavailable', 'local_onenote');
     }
     if (empty(self::$instance)) {
         self::$instance = new $class();
     }
     return self::$instance;
 }
예제 #3
0
 /**
  * Gets the instance of the correct api class. Use this method to get an instance of the api class.
  *
  * @return \local_onenote\api\base An implementation of the OneNote API.
  */
 public static function getinstance()
 {
     global $USER, $SESSION, $CFG;
     $msaccountclass = '\\local_onenote\\api\\msaccount';
     $o365class = '\\local_onenote\\api\\o365';
     $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);
         $disableo365onenote = get_user_preferences('local_o365_disableo365onenote', 0);
         $iso365user = !empty($SESSION->{$sesskey}) || !empty($disableo365onenote) ? false : $iso365user;
         if ($iso365user === true) {
             try {
                 $httpclient = new \local_o365\httpclient();
                 $clientdata = \local_o365\oauth2\clientdata::instance_from_oidc();
                 $onenoteresource = \local_o365\rest\onenote::get_resource();
                 $token = \local_o365\oauth2\token::instance($USER->id, $onenoteresource, $clientdata, $httpclient);
                 if (empty($token)) {
                     $iso365user = false;
                 }
             } catch (\Exception $e) {
                 $iso365user = false;
             }
         }
         $class = $iso365user === true ? $o365class : $msaccountclass;
     } else {
         $class = $msaccountclass;
     }
     if (empty(self::$instance)) {
         self::$instance = new $class();
     }
     return self::$instance;
 }