示例#1
0
 /**
  * Validate that a given SharePoint url is accessible with the given client data.
  *
  * @param string $uncleanurl Uncleaned, unvalidated URL to check.
  * @param \local_o365\oauth2\clientdata $clientdata oAuth2 Credentials
  * @param \local_o365\httpclientinterface $httpclient An HttpClient to use for transport.
  * @return string One of:
  *                    "invalid" : The URL is not a usable SharePoint url.
  *                    "notempty" : The URL is a usable SharePoint url, and the SharePoint site exists.
  *                    "valid" : The URL is a usable SharePoint url, and the SharePoint site doesn't exist.
  */
 public static function validate_site($uncleanurl, \local_o365\oauth2\clientdata $clientdata, \local_o365\httpclientinterface $httpclient)
 {
     $siteinfo = static::parse_site_url($uncleanurl);
     if (empty($siteinfo)) {
         return 'invalid';
     }
     $token = \local_o365\oauth2\systemtoken::get_for_new_resource(null, $siteinfo['resource'], $clientdata, $httpclient);
     if (empty($token)) {
         return 'invalid';
     }
     $sharepoint = new \local_o365\rest\sharepoint($token, $httpclient);
     $sharepoint->override_resource($siteinfo['resource']);
     // Try to get the / site's info to validate we can communicate with this parent Sharepoint site.
     try {
         $mainsiteinfo = $sharepoint->get_site();
     } catch (\Exception $e) {
         return 'invalid';
     }
     if ($siteinfo['subsiteurl'] === '/') {
         // We just successfully got the / site's info, so if we're going to use that, it's obviously not empty.
         return 'notempty';
     }
     $subsiteexists = $sharepoint->site_exists($siteinfo['subsiteurl']);
     return $subsiteexists === true ? 'notempty' : 'valid';
 }
示例#2
0
 /**
  * Do the job.
  */
 public function execute()
 {
     global $DB;
     $oidcconfig = get_config('auth_oidc');
     if (empty($oidcconfig)) {
         throw new \moodle_exception('erroracpauthoidcnotconfig', 'local_o365');
     }
     $spresource = \local_o365\rest\sharepoint::get_resource();
     if (empty($spresource)) {
         throw new \moodle_exception('erroracplocalo365notconfig', 'local_o365');
     }
     $httpclient = new \local_o365\httpclient();
     $clientdata = new \local_o365\oauth2\clientdata($oidcconfig->clientid, $oidcconfig->clientsecret, $oidcconfig->authendpoint, $oidcconfig->tokenendpoint);
     $sptoken = \local_o365\oauth2\systemtoken::instance(null, $spresource, $clientdata, $httpclient);
     if (empty($sptoken)) {
         throw new \moodle_exception('erroracpnosptoken', 'local_o365');
     }
     $sharepoint = new \local_o365\rest\sharepoint($sptoken, $httpclient);
     $sharepoint->set_site('');
     $moodlesiteuri = $sharepoint->get_moodle_parent_site_uri();
     if ($sharepoint->site_exists($moodlesiteuri) === false) {
         $moodlesitename = get_string('acp_parentsite_name', 'local_o365');
         $moodlesitedesc = get_string('acp_parentsite_desc', 'local_o365');
         $frontpagerec = $DB->get_record('course', ['id' => SITEID], 'id,shortname');
         if (!empty($frontpagerec) && !empty($frontpagerec->shortname)) {
             $moodlesitename = $frontpagerec->shortname;
         }
         $result = $sharepoint->create_site($moodlesitename, $moodlesiteuri, $moodlesitedesc);
         mtrace('Created parent site');
     }
     $courses = $DB->get_recordset('course');
     $successes = [];
     $failures = [];
     foreach ($courses as $course) {
         if ($course->id == SITEID) {
             continue;
         }
         try {
             $sharepoint->create_course_site($course);
             $successes[] = $course->id;
             mtrace('Created course subsite for course ' . $course->id);
         } catch (\Exception $e) {
             $failures[$course->id] = $e->getMessage();
         }
     }
     set_config('sharepoint_initialized', '1', 'local_o365');
 }
/**
 * Looks for links pointing to Office 365 Video content and processes them.
 *
 * @param $link HTML tag containing a link
 * @return string HTML content after processing.
 */
function filter_oembed_o365videocallback($link)
{
    if (empty($link[3])) {
        return $link[0];
    }
    $link[3] = preg_replace("/&/", "&", $link[3]);
    $values = array();
    parse_str($link[3], $values);
    if (empty($values['chid']) || empty($values['vid'])) {
        return $link[0];
    }
    if (!\local_o365\rest\sharepoint::is_configured()) {
        \local_o365\utils::debug('filter_oembed share point is not configured', 'filter_oembed_o365videocallback');
        return $link[0];
    }
    try {
        $spresource = \local_o365\rest\sharepoint::get_resource();
        if (!empty($spresource)) {
            $httpclient = new \local_o365\httpclient();
            $clientdata = \local_o365\oauth2\clientdata::instance_from_oidc();
            $sptoken = \local_o365\oauth2\systemtoken::instance(null, $spresource, $clientdata, $httpclient);
            if (!empty($sptoken)) {
                $sharepoint = new \local_o365\rest\sharepoint($sptoken, $httpclient);
                // Retrieve api url for video service.
                $url = $sharepoint->videoservice_discover();
                if (!empty($url)) {
                    $sharepoint->override_resource($url);
                    $width = 640;
                    if (!empty($values['width'])) {
                        $width = $values['width'];
                    }
                    $height = 360;
                    if (!empty($values['height'])) {
                        $height = $values['height'];
                    }
                    // Retrieve embed code.
                    return $sharepoint->get_video_embed_code($values['chid'], $values['vid'], $width, $height);
                }
            }
        }
    } catch (\Exception $e) {
        \local_o365\utils::debug('filter_oembed share point execption: ' . $e->getMessage(), 'filter_oembed_o365videocallback');
    }
    return $link[0];
}
示例#4
0
 /**
  * Test create_course_site method.
  */
 public function test_create_course_site()
 {
     global $DB;
     $requiredcapability = \local_o365\rest\sharepoint::get_course_site_required_capability();
     $course = $this->getDataGenerator()->create_course();
     $role = $this->getDataGenerator()->create_role(['archetype' => 'editingteacher']);
     $coursecontext = \context_course::instance($course->id);
     $user1 = $this->getDataGenerator()->create_user(['auth' => 'oidc']);
     $user2 = $this->getDataGenerator()->create_user(['auth' => 'oidc']);
     $aaduserdata = (object) ['type' => 'user', 'subtype' => '', 'objectid' => '', 'moodleid' => $user1->id, 'o365name' => '*****@*****.**', 'timecreated' => time(), 'timemodified' => time()];
     $aaduserdata->id = $DB->insert_record('local_o365_objects', $aaduserdata);
     $result = $this->getDataGenerator()->role_assign($role, $user1->id, $coursecontext);
     $httpclient = new \local_o365\tests\mockhttpclient();
     $httpresponses = ['', $this->get_response_create_site($course->fullname, $course->shortname, $course->summary), $this->get_response_create_group('testgroup', 'testgroup'), $this->get_response_assign_group_permission(), $this->get_response_add_user_to_group($aaduserdata->userupn)];
     $httpclient->set_responses($httpresponses);
     $apiclient = new \local_o365\rest\sharepoint($this->get_mock_token(), $httpclient);
     $apiclient->create_course_site($course->id);
     $coursespsite = $DB->get_record('local_o365_coursespsite', ['courseid' => $course->id]);
     $this->assertNotEmpty($coursespsite);
     $this->assertEquals('/moodle/' . $course->shortname, $coursespsite->siteurl);
     $spgroupdata = $DB->get_records('local_o365_spgroupdata', ['coursespsiteid' => $coursespsite->id]);
     $this->assertNotEmpty($spgroupdata);
 }
 /**
  * Do the job.
  */
 public function execute()
 {
     global $DB;
     // API Setup.
     try {
         $spresource = \local_o365\rest\sharepoint::get_resource();
         if (empty($spresource)) {
             throw new \moodle_exception('erroracplocalo365notconfig', 'local_o365');
         }
         $httpclient = new \local_o365\httpclient();
         $clientdata = \local_o365\oauth2\clientdata::instance_from_oidc();
         $sptoken = \local_o365\oauth2\systemtoken::instance(null, $spresource, $clientdata, $httpclient);
         if (empty($sptoken)) {
             throw new \moodle_exception('erroracpnosptoken', 'local_o365');
         }
         $sharepoint = new \local_o365\rest\sharepoint($sptoken, $httpclient);
     } catch (\Exception $e) {
         $errmsg = 'ERROR: Problem initializing SharePoint API. Reason: ' . $e->getMessage();
         mtrace($errmsg);
         \local_o365\utils::debug($errmsg, 'local_o365\\task\\sharepointinit::execute');
         set_config('sharepoint_initialized', 'error', 'local_o365');
         return false;
     }
     // Create parent site(s).
     try {
         mtrace('Creating parent site for Moodle...');
         $moodlesiteuri = $sharepoint->get_moodle_parent_site_uri();
         $sitelevels = explode('/', $moodlesiteuri);
         $currentparentsite = '';
         foreach ($sitelevels as $partialurl) {
             $sharepoint->set_site($currentparentsite);
             if ($sharepoint->site_exists($currentparentsite . '/' . $partialurl) === false) {
                 $moodlesitename = get_string('acp_parentsite_name', 'local_o365');
                 $moodlesitedesc = get_string('acp_parentsite_desc', 'local_o365');
                 $frontpagerec = $DB->get_record('course', ['id' => SITEID], 'id,shortname');
                 if (!empty($frontpagerec) && !empty($frontpagerec->shortname)) {
                     $moodlesitename = $frontpagerec->shortname;
                 }
                 mtrace('Setting parent site to "' . $currentparentsite . '", creating subsite "' . $partialurl . '"');
                 $result = $sharepoint->create_site($moodlesitename, $partialurl, $moodlesitedesc);
                 $currentparentsite .= '/' . $partialurl;
                 mtrace('Created parent site "' . $currentparentsite . '"');
             } else {
                 $currentparentsite .= '/' . $partialurl;
                 mtrace('Parent site "' . $currentparentsite . '" already exists.');
             }
         }
         mtrace('Finished creating Moodle parent site.');
     } catch (\Exception $e) {
         $errmsg = 'ERROR: Problem creating parent site. Reason: ' . $e->getMessage();
         mtrace($errmsg);
         \local_o365\utils::debug($errmsg, 'local_o365\\task\\sharepointinit::execute');
         set_config('sharepoint_initialized', 'error', 'local_o365');
         return false;
     }
     // Create course sites.
     mtrace('Creating course subsites in "' . $moodlesiteuri . '"');
     $sharepoint->set_site($moodlesiteuri);
     $courses = $DB->get_recordset('course');
     $successes = [];
     $failures = [];
     foreach ($courses as $course) {
         if ($course->id == SITEID) {
             continue;
         }
         try {
             $sharepoint->create_course_site($course);
             $successes[] = $course->id;
             mtrace('Created course subsite for course ' . $course->id);
         } catch (\Exception $e) {
             mtrace('Encountered error creating course subsite for course ' . $course->id);
             $failures[$course->id] = $e->getMessage();
         }
     }
     if (!empty($failures)) {
         $errmsg = 'ERROR: Encountered problems creating course sites.';
         mtrace($errmsg . ' See logs.');
         \local_o365\utils::debug($errmsg, 'local_o365\\task\\sharepointinit::execute', $failures);
         set_config('sharepoint_initialized', 'error', 'local_o365');
     } else {
         set_config('sharepoint_initialized', '1', 'local_o365');
         mtrace('SharePoint successfully initialized.');
         return true;
     }
 }