Beispiel #1
0
 public function test_delete_site()
 {
     global $DB;
     $this->resetAfterTest(true);
     $hub = new local_hub();
     $url = "http://example.com";
     $sitevalues = $this->get_sitevalues($url);
     $token = $hub->register_site($sitevalues);
     $this->check_tokens($hub, $sitevalues['url']);
     $site = $hub->get_site_by_url($url);
     $hub->delete_site($site->id);
     $site = $hub->get_site($site->id);
     // Should we be able to retrieve deleted sites like this?
     $this->assertEquals($site->deleted, 1);
     // Verify the tokens are either deleted or marked as deleted.
     $username = $url . '_registered_site_user';
     $user = $DB->get_record('user', array('username' => $username, 'idnumber' => $username));
     $service = $DB->get_record('external_services', array('name' => 'Registered site'));
     $this->assertEquals($this->get_external_token($user, $service), null);
     $this->assertEquals($hub->get_communication(WSSERVER, REGISTEREDSITE, $sitevalues['url']), null);
 }
/**
 * Process the registration - redirect the user to its admin confirmation registration page
 *
 * @param object $sitevalues the site values
 * @param boolean $secretexists true if a site is already registered with the same secret
 * @param type $urlexists true if a site is already registered with this url
 * @param type $sitewithsamesecret the DB site already existing for this secret
 * @param type $sitewithsameurl the DB site already existing for this url address
 */
function process_registration($sitevalues, $secretexists, $urlexists, $sitewithsamesecret, $sitewithsameurl)
{
    global $CFG;
    //check that the form has the required data
    //(to force people that don't call this page from a Moodle registration page to POST correct data.
    //Note that there is no good reason for people to do it)
    if (empty($sitevalues->token) or empty($sitevalues->url) or empty($sitevalues->name) or empty($sitevalues->contactname) or empty($sitevalues->contactemail) or empty($sitevalues->description) or empty($sitevalues->language)) {
        throw new moodle_exception('errorwrongdata', 'local_hub', new moodle_url('/index.php'));
    }
    //token is saved as secret in the DB
    $sitevalues->secret = $sitevalues->token;
    $hub = new local_hub();
    if (!$secretexists and !$urlexists) {
        $newtoken = $hub->register_site($sitevalues);
        //log the new site
        add_to_log(SITEID, 'local_hub', 'new site registration', '', $sitevalues->url);
    } else {
        if ($secretexists and $urlexists and $sitewithsamesecret->url == $sitewithsameurl->url) {
            //the site is already registered
            //It happens when new fresh site has been installed and the email link
            //wasn't followed till the end of the registration replacement process.
            $newtoken = $hub->register_site($sitevalues, $sitevalues->url);
            //log the overwritting site registration
            add_to_log(SITEID, 'local_hub', 'site registered a new time', '', $sitevalues->url);
        } else {
            //log the code logic error (it should never happen)
            add_to_log(SITEID, 'local_hub', 'registration code logic error', '', $sitevalues->url);
            throw new moodle_exception('codelogicerror', 'local_hub');
        }
    }
    //Redirect to the site with the created token
    redirect(new moodle_url($sitevalues->url . "/admin/registration/confirmregistration.php", array('newtoken' => $newtoken, 'url' => HUBURL, 'token' => $sitevalues->token, 'hubname' => get_config('local_hub', 'name'))));
}
 /**
  * Update site registration
  * two security check:
  * 1- if the url changed, unactivate the site and alert the administrator
  * 2- call the site by web service and confirm it, if confirmation fail, the update is declare failed
  * @return boolean 1 if updated was successfull
  */
 public static function update_site_info($siteinfo)
 {
     // Ensure the current user is allowed to run this function
     $context = context_system::instance();
     self::validate_context($context);
     require_capability('local/hub:updateinfo', $context);
     $params = self::validate_parameters(self::update_site_info_parameters(), array('siteinfo' => $siteinfo));
     //check that the hub can access the site
     $hubmanager = new local_hub();
     if (!$hubmanager->is_remote_site_valid($params['siteinfo']['url'])) {
         throw new moodle_exception('cannotregisternotavailablesite', 'local_hub', $params['siteinfo']['url'], $params['siteinfo']['url']);
     }
     //add ip information
     $params['siteinfo']['ip'] = getremoteaddr();
     //retieve site url
     $token = optional_param('wstoken', '', PARAM_ALPHANUM);
     $siteurl = $hubmanager->get_communication(WSSERVER, REGISTEREDSITE, null, $token)->remoteurl;
     //this following error should never happen
     //(communication record doesn't exist and webservice token exists)
     if (empty($siteurl)) {
         throw new moodle_exception('noexistingcommunication', 'local_hub');
     }
     $result = $hubmanager->register_site($params['siteinfo'], $siteurl);
     return 1;
 }