/**
  * Return user data for the provided token, compare with user_agent string.
  *
  * @param  string $token    The unique ID provided by remotehost.
  * @param  string $UA       User Agent string.
  * @return array  $userdata Array of user info for remote host
  */
 function user_authorise($token, $useragent)
 {
     global $CFG, $SITE, $DB;
     $remoteclient = get_mnet_remote_client();
     require_once $CFG->dirroot . '/mnet/xmlrpc/serverlib.php';
     $mnet_session = $DB->get_record('mnet_session', array('token' => $token, 'useragent' => $useragent));
     if (empty($mnet_session)) {
         throw new mnet_server_exception(1, 'authfail_nosessionexists');
     }
     // check session confirm timeout
     if ($mnet_session->confirm_timeout < time()) {
         throw new mnet_server_exception(2, 'authfail_sessiontimedout');
     }
     // session okay, try getting the user
     if (!($user = $DB->get_record('user', array('id' => $mnet_session->userid)))) {
         throw new mnet_server_exception(3, 'authfail_usermismatch');
     }
     $userdata = mnet_strip_user((array) $user, mnet_fields_to_send($remoteclient));
     // extra special ones
     $userdata['auth'] = 'mnet';
     $userdata['wwwroot'] = $this->mnet->wwwroot;
     $userdata['session.gc_maxlifetime'] = ini_get('session.gc_maxlifetime');
     if (array_key_exists('picture', $userdata) && !empty($user->picture)) {
         $fs = get_file_storage();
         $usercontext = get_context_instance(CONTEXT_USER, $user->id, MUST_EXIST);
         if ($usericonfile = $fs->get_file($usercontext->id, 'user', 'icon', 0, '/', 'f1.png')) {
             $userdata['_mnet_userpicture_timemodified'] = $usericonfile->get_timemodified();
             $userdata['_mnet_userpicture_mimetype'] = $usericonfile->get_mimetype();
         } else {
             if ($usericonfile = $fs->get_file($usercontext->id, 'user', 'icon', 0, '/', 'f1.jpg')) {
                 $userdata['_mnet_userpicture_timemodified'] = $usericonfile->get_timemodified();
                 $userdata['_mnet_userpicture_mimetype'] = $usericonfile->get_mimetype();
             }
         }
     }
     $userdata['myhosts'] = array();
     if ($courses = enrol_get_users_courses($user->id, false)) {
         $userdata['myhosts'][] = array('name' => $SITE->shortname, 'url' => $CFG->wwwroot, 'count' => count($courses));
     }
     $sql = "SELECT h.name AS hostname, h.wwwroot, h.id AS hostid,\n                       COUNT(c.id) AS count\n                  FROM {mnetservice_enrol_courses} c\n                  JOIN {mnetservice_enrol_enrolments} e ON (e.hostid = c.hostid AND e.remotecourseid = c.remoteid)\n                  JOIN {mnet_host} h ON h.id = c.hostid\n                 WHERE e.userid = ? AND c.hostid = ?\n              GROUP BY h.name, h.wwwroot, h.id";
     if ($courses = $DB->get_records_sql($sql, array($user->id, $remoteclient->id))) {
         foreach ($courses as $course) {
             $userdata['myhosts'][] = array('name' => $course->hostname, 'url' => $CFG->wwwroot . '/auth/mnet/jump.php?hostid=' . $course->hostid, 'count' => $course->count);
         }
     }
     return $userdata;
 }
示例#2
0
 /**
  * Send request to enrol our user to the remote course
  *
  * Updates our remote enrolments cache if the enrolment was successful.
  *
  * @uses mnet_xmlrpc_client Invokes XML-RPC request
  * @param object $user our user
  * @param object $remotecourse record from mnetservice_enrol_courses table
  * @return true|string true if success, error message from the remote host otherwise
  */
 public function req_enrol_user(stdclass $user, stdclass $remotecourse)
 {
     global $CFG, $DB;
     require_once $CFG->dirroot . '/mnet/xmlrpc/client.php';
     $peer = new mnet_peer();
     $peer->set_id($remotecourse->hostid);
     $request = new mnet_xmlrpc_client();
     $request->set_method('enrol/mnet/enrol.php/enrol_user');
     $request->add_param(mnet_strip_user((array) $user, mnet_fields_to_send($peer)));
     $request->add_param($remotecourse->remoteid);
     if ($request->send($peer) === true) {
         if ($request->response === true) {
             // cache the enrolment information in our table
             $enrolment = new stdclass();
             $enrolment->hostid = $peer->id;
             $enrolment->userid = $user->id;
             $enrolment->remotecourseid = $remotecourse->remoteid;
             $enrolment->enroltype = 'mnet';
             // $enrolment->rolename not known now, must be re-fetched
             // $enrolment->enroltime not known now, must be re-fetched
             $DB->insert_record('mnetservice_enrol_enrolments', $enrolment);
             return true;
         } else {
             return serialize(array('invalid response: ' . print_r($request->response, true)));
         }
     } else {
         return serialize($request->error);
     }
 }