/** * Get a OneDrive token. * * @param bool $system If true, get a system API ser token instead of the user's token. * @param int|null $userid The userid to get a token for. If null, the current user will be used. * @return \local_o365\oauth2\token A OneDrive token object. */ protected function get_onedrive_token($system = false, $userid = null) { global $USER; $resource = \local_o365\rest\onedrive::get_resource(); if ($system === true) { return \local_o365\oauth2\systemtoken::instance(null, $resource, $this->clientdata, $this->httpclient); } else { $userid = !empty($userid) ? $userid : $USER->id; return \local_o365\oauth2\token::instance($userid, $resource, $this->clientdata, $this->httpclient); } }
/** * Get a OneDrive token. * * @return \local_o365\oauth2\token A OneDrive token object. */ protected function get_onedrive_token() { global $USER; $resource = \local_o365\rest\onedrive::get_resource(); return \local_o365\oauth2\token::instance($USER->id, $resource, $this->clientdata, $this->httpclient); }
/** * Check if a service resource is valid. */ public function mode_checkserviceresource() { $data = new \stdClass(); $success = false; $setting = required_param('setting', PARAM_TEXT); $value = required_param('value', PARAM_TEXT); $clientdata = \local_o365\oauth2\clientdata::instance_from_oidc(); $httpclient = new \local_o365\httpclient(); if ($setting === 'aadtenant') { $resource = \local_o365\rest\azuread::get_resource(); $token = \local_o365\oauth2\systemtoken::instance(null, $resource, $clientdata, $httpclient); if (empty($token)) { throw new \moodle_exception('errorchecksystemapiuser', 'local_o365'); } $apiclient = new \local_o365\rest\azuread($token, $httpclient); $data->valid = $apiclient->test_tenant($value); $success = true; } else { if ($setting === 'odburl') { $data->valid = \local_o365\rest\onedrive::validate_resource($value, $clientdata, $httpclient); $success = true; } } echo $this->ajax_response($data, $success); }