Skip to content

Commit

Permalink
mnet MDL-22267 updated mnet_server_faultand mnet_server_exception str…
Browse files Browse the repository at this point in the history
…ing handling

This is so that mnet_server_fault doesn't have to call get_string (in
case of coding exception)
  • Loading branch information
Penny Leach committed May 1, 2010
1 parent 15b45bd commit d234faf
Show file tree
Hide file tree
Showing 6 changed files with 32 additions and 40 deletions.
8 changes: 4 additions & 4 deletions auth/mnet/auth.php
Original file line number Diff line number Diff line change
Expand Up @@ -59,17 +59,17 @@ function user_authorise($token, $useragent) {

$mnet_session = $DB->get_record('mnet_session', array('token'=>$token, 'useragent'=>$useragent));
if (empty($mnet_session)) {
throw new mnet_server_exception(1, get_string('authfail_nosessionexists', 'mnet'));
throw new mnet_server_exception(1, 'authfail_nosessionexists');
}

// check session confirm timeout
if ($mnet_session->confirm_timeout < time()) {
throw new mnet_server_exception(2, get_string('authfail_sessiontimedout', 'mnet'));
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, get_string('authfail_usermismatch', 'mnet'));
throw new mnet_server_exception(3, 'authfail_usermismatch');
}

$userdata = mnet_strip_user((array)$user, mnet_fields_to_send($remoteclient));
Expand Down Expand Up @@ -463,7 +463,7 @@ function update_enrolments($username, $courses) {
// with that host...
if (!$userid = $DB->get_field('mnet_session', 'userid',
array('username'=>$username, 'mnethostid'=>$remoteclient->id))) {
throw new mnet_server_exception(1, get_string('authfail_nosessionexists', 'mnet'));
throw new mnet_server_exception(1, 'authfail_nosessionexists');
}

if (empty($courses)) { // no courses? clear out quickly
Expand Down
14 changes: 7 additions & 7 deletions enrol/mnet/enrol.php
Original file line number Diff line number Diff line change
Expand Up @@ -310,12 +310,12 @@ function enrol_user($user, $courseid) {
if ($userrecord->id = $DB->insert_record('user', $userrecord)) {
$userrecord = $DB->get_record('user', array('id'=>$userrecord->id));
} else {
throw new mnet_server_exception(5011, get_string('couldnotcreateuser', 'enrol_mnet'));
throw new mnet_server_exception(5011, 'couldnotcreateuser', 'enrol_mnet');
}
}

if (! $course = $DB->get_record('course', array('id'=>$courseid))) {
throw new mnet_server_exception(5012, get_string('coursenotfound', 'enrol_mnet'));
throw new mnet_server_exception(5012, 'coursenotfound', 'enrol_mnet');
}

$courses = $this->available_courses();
Expand All @@ -324,9 +324,9 @@ function enrol_user($user, $courseid) {
if (enrol_into_course($course, $userrecord, 'mnet')) {
return true;
}
throw new mnet_server_exception(5016, get_string('couldnotenrol', 'enrol_mnet'));
throw new mnet_server_exception(5016, 'couldnotenrol', 'enrol_mnet');
}
throw new mnet_server_exception(5013, get_string('courseunavailable', 'enrol_mnet'));
throw new mnet_server_exception(5013, 'courseunavailable', 'enrol_mnet');
}

/**
Expand All @@ -341,11 +341,11 @@ function unenrol_user($username, $courseid) {
$remoteclient = get_mnet_remote_client();

if (!$userrecord = $DB->get_record('user', array('username'=>$username, 'mnethostid'=>$remoteclient->id))) {
throw new mnet_exception(5014, get_string('usernotfound', 'enrol_mnet'));
throw new mnet_exception(5014, 'usernotfound', 'enrol_mnet');
}

if (! $course = $DB->get_record('course', array('id'=>$courseid))) {
throw new mnet_server_exception(5012, get_string('coursenotfound', 'enrol_mnet'));
throw new mnet_server_exception(5012, 'coursenotfound', 'enrol_mnet');
}

$context = get_context_instance(CONTEXT_COURSE, $course->id);
Expand All @@ -354,7 +354,7 @@ function unenrol_user($username, $courseid) {
// require_capability('moodle/role:assign', $context, NULL, false);

if (!role_unassign(0, $userrecord->id, 0, $context->id)) {
throw new mnet_exception(5015, get_string('couldnotunenrol', 'enrol_mnet'));
throw new mnet_exception(5015, 'couldnotunenrol', 'enrol_mnet');
}

return true;
Expand Down
10 changes: 5 additions & 5 deletions mnet/xmlrpc/server.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@
mnet_debug($HTTP_RAW_POST_DATA, 2);

if (!isset($_SERVER)) {
exit(mnet_server_fault(712, "phperror"));
exit(mnet_server_fault(712, get_string('phperror', 'mnet')));
}


Expand Down Expand Up @@ -95,19 +95,19 @@
// so detect a few common cases and send appropriate errors
if (($remoteclient->request_was_encrypted == false) && ($remoteclient->plaintext_is_ok() == false)) {
mnet_debug('non encrypted request');
exit(mnet_server_fault(7021, 'forbidden-transport'));
exit(mnet_server_fault(7021, get_string('forbidden-transport', 'mnet')));
}

if ($remoteclient->request_was_signed == false) {
// Request was not signed
mnet_debug('non signed request');
exit(mnet_server_fault(711, 'verifysignature-error'));
exit(mnet_server_fault(711, get_string('verifysignature-error', 'mnet')));
}

if ($remoteclient->signatureok == false) {
// We were unable to verify the signature
mnet_debug('non verified signature');
exit(mnet_server_fault(710, 'verifysignature-invalid'));
exit(mnet_server_fault(710, get_string('verifysignature-invalid', 'mnet')));
}
mnet_debug('unknown error');
exit(mnet_server_fault(7000, 'unknownerror'));
exit(mnet_server_fault(7000, get_string('unknownerror', 'mnet')));
20 changes: 6 additions & 14 deletions mnet/xmlrpc/serverlib.php
Original file line number Diff line number Diff line change
Expand Up @@ -160,8 +160,7 @@ function mnet_server_strip_signature($plaintextmessage) {
* Return the proper XML-RPC content to report an error in the local language.
*
* @param int $code The ID code of the error message
* @param string $text The array-key of the error message in the lang file
* or the full string (will be detected by the function
* @param string $text The full string of the error message (get_string will <b>not be called</b>)
* @param string $param The $a param for the error message in the lang file
* @return string $text The text of the error message
*/
Expand All @@ -170,13 +169,7 @@ function mnet_server_fault($code, $text, $param = null) {
$code = 0;
}
$code = intval($code);

$string = get_string($text, 'mnet', $param);
if (strpos($string, '[[') === 0) {
$string = $text;
}

return mnet_server_fault_xml($code, $string);
return mnet_server_fault_xml($code, $text);
}

/**
Expand Down Expand Up @@ -667,21 +660,20 @@ function mnet_server_dummy_method($methodname, $argsarray, $functionname) {
}
/**
* mnet server exception. extends moodle_exception, but takes slightly different arguments.
* error strings are always in mnet, so $module is not necessary,
* and unlike the rest of moodle, the actual int error code is used.
* this exception should only be used during an xmlrpc server request, ie, not for client requests.
*/
class mnet_server_exception extends moodle_exception {

/**
* @param int $intcode the numerical error associated with this fault. this is <b>not</b> the string errorcode
* @param string $languagekey the key to the language string for the error message, or the text of the message (mnet_server_fault figures it out for you) if you're not using mnet error string file
* @param string $langkey the error message in full (<b>get_string will not be used</b>)
* @param string $module the language module, defaults to 'mnet'
* @param mixed $a params for get_string
*/
public function __construct($intcode, $languagekey, $a=null) {
parent::__construct($languagekey, 'mnet', '', $a);
public function __construct($intcode, $languagekey, $module='mnet', $a=null) {
parent::__construct($languagekey, $module, '', $a);
$this->code = $intcode;
$this->message = $languagekey; // override this because mnet_server_fault (our handler) uses this directly

}
}
Expand Down
12 changes: 6 additions & 6 deletions portfolio/mahara/lib.php
Original file line number Diff line number Diff line change
Expand Up @@ -334,30 +334,30 @@ public static function fetch_file($token) {
$remoteclient = get_mnet_remote_client();
try {
if (!$transferid = $DB->get_field('portfolio_mahara_queue', 'transferid', array('token' => $token))) {
throw new mnet_server_exception(8009, get_string('mnet_notoken', 'portfolio_mahara'));
throw new mnet_server_exception(8009, 'mnet_notoken', 'portfolio_mahara');
}
$exporter = portfolio_exporter::rewaken_object($transferid);
} catch (portfolio_exception $e) {
throw new mnet_server_exception(8010, get_string('mnet_noid', 'portfolio_mahara'));
throw new mnet_server_exception(8010, 'mnet_noid', 'portfolio_mahara');
}
if ($exporter->get('instance')->get_config('mnethostid') != $remoteclient->id) {
throw new mnet_server_exception(8011, get_string('mnet_wronghost', 'portfolio_mahara'));
throw new mnet_server_exception(8011, 'mnet_wronghost', 'portfolio_mahara');
}
global $CFG;
try {
$i = $exporter->get('instance');
$f = $i->get('file');
if (empty($f) || !($f instanceof stored_file)) {
throw new mnet_server_exception(8012, get_string('mnet_nofile', 'portfolio_mahara'));
throw new mnet_server_exception(8012, 'mnet_nofile', 'portfolio_mahara');
}
try {
$c = $f->get_content();
} catch (file_exception $e) {
throw new mnet_server_exception(8013, get_string('mnet_nofilecontents', 'portfolio_mahara', $e->getMessage()));
throw new mnet_server_exception(8013, 'mnet_nofilecontents', 'portfolio_mahara', $e->getMessage());
}
$contents = base64_encode($c);
} catch (Exception $e) {
throw new mnet_server_exception(8013, get_string('mnet_nofile', 'portfolio_mahara'));
throw new mnet_server_exception(8013, 'mnet_nofile', 'portfolio_mahara');
}
$exporter->log_transfer();
$exporter->process_stage_cleanup(true);
Expand Down
8 changes: 4 additions & 4 deletions repository/remotemoodle/repository.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ public function retrieveFile($username, $source) {
// also is the user allowed to roam?
$USER = $DB->get_record('user',array('username' => $username, 'mnethostid' => $remoteclient->id));
if (empty($USER)) {
throw new mnet_server_exception(9012, get_string('usernotfound', 'repository_remotemoodle', $username));
throw new mnet_server_exception(9012, 'usernotfound', 'repository_remotemoodle', $username);
}

$file = unserialize(base64_decode($source));
Expand All @@ -80,7 +80,7 @@ public function retrieveFile($username, $source) {
$browser = get_file_browser();
$fileinfo = $browser->get_file_info(get_context_instance_by_id($contextid), $filearea, $itemid, $filepath, $filename);
if (empty($fileinfo)) {
throw new mnet_server_exception(9013, get_string('usercannotaccess', 'repository_remotemoodle', $file));
throw new mnet_server_exception(9013, 'usercannotaccess', 'repository_remotemoodle', $file);
}

///retrieve the file with file API functions and return it encoded in base64
Expand Down Expand Up @@ -109,14 +109,14 @@ public function getFileList($username, $search) {
// also is the user allowed to roam?
$USER = $DB->get_record('user',array('username' => $username, 'mnethostid' => $remoteclient->id));
if (empty($USER)) {
throw new mnet_server_exception(9012, get_string('usernotfound', 'repository_remotemoodle', $username));
throw new mnet_server_exception(9012, 'usernotfound', 'repository_remotemoodle', $username);
}

try {
return repository::get_user_file_tree($search);
}
catch (Exception $e) {
throw new mnet_server_exception(9014, get_string('failtoretrievelist', 'repository_remotemoodle'));
throw new mnet_server_exception(9014, 'failtoretrievelist', 'repository_remotemoodle');
}
}

Expand Down

0 comments on commit d234faf

Please sign in to comment.