MDL-21894 "move functionality to create a token into function in externallib.php so it can be used when creating tokens for embedded applications" also fixed MDL-21893 "Errors in code for checking capability when creating token for service with requiredcapability" which I came across while re

This commit is contained in:
Jamie Pratt 2010-03-22 14:20:18 +00:00
parent a1656ddf79
commit 2822f40ae2
2 changed files with 44 additions and 29 deletions

View File

@ -27,6 +27,7 @@
require_once('../../config.php');
require_once($CFG->libdir.'/adminlib.php');
require_once('forms.php');
require_once($CFG->libdir.'/externallib.php');
$action = optional_param('action', '', PARAM_ACTION);
$tokenid = optional_param('tokenid', '', PARAM_SAFEDIR);
@ -66,38 +67,10 @@ switch ($action) {
redirect($returnurl);
} else if ($data = $mform->get_data()) {
ignore_user_abort(true); // no interruption here!
//generate token
$generatedtoken = md5(uniqid(rand(),1));
// make sure the token doesn't exist (even if it should be almost impossible with the random generation)
if ($DB->record_exists('external_tokens', array('token'=>$generatedtoken))) {
throw new moodle_exception('tokenalreadyexist');
} else {
$newtoken = new object();
$newtoken->token = $generatedtoken;
if (empty($service->requiredcapability) || has_capability($service->requiredcapability, $systemcontext, $data->user)) {
$newtoken->externalserviceid = $data->service;
} else {
throw new moodle_exception('nocapabilitytousethisservice');
}
$newtoken->tokentype = EXTERNAL_TOKEN_PERMANENT;
$newtoken->userid = $data->user;
//TODO: find a way to get the context - UPDATE FOLLOWING LINE
$newtoken->contextid = get_context_instance(CONTEXT_SYSTEM)->id;
$newtoken->creatorid = $USER->id;
$newtoken->timecreated = time();
$newtoken->validuntil = $data->validuntil;
if (!empty($data->iprestriction)) {
$newtoken->iprestriction = $data->iprestriction;
}
$DB->insert_record('external_tokens', $newtoken);
}
external_generate_token(EXTERNAL_TOKEN_PERMANENT, $data->service, $data->user, get_context_instance(CONTEXT_SYSTEM), $data->validuntil, $data->iprestriction);
redirect($returnurl);
}
//ask for function id
admin_externalpage_print_header();
echo $OUTPUT->heading(get_string('createtoken', 'webservice'));

View File

@ -410,3 +410,45 @@ class external_multiple_structure extends external_description {
*/
class external_function_parameters extends external_single_structure {
}
function external_generate_token($tokentype, $serviceorid, $userid, $contextorid, $validuntil=0, $iprestriction=''){
global $DB, $USER;
// make sure the token doesn't exist (even if it should be almost impossible with the random generation)
$numtries = 0;
do {
$numtries ++;
$generatedtoken = md5(uniqid(rand(),1));
if ($numtries > 5){
throw new moodle_exception('tokengenerationfailed');
}
} while ($DB->record_exists('external_tokens', array('token'=>$generatedtoken)));
$newtoken = new object();
$newtoken->token = $generatedtoken;
if (!is_object($serviceorid)){
$service = $DB->get_record('external_services', array('id' => $serviceorid));
} else {
$service = $serviceorid;
}
if (!is_object($contextorid)){
$context = get_context_instance_by_id($contextorid, MUST_EXIST);
} else {
$context = $contextorid;
}
if (empty($service->requiredcapability) || has_capability($service->requiredcapability, $context, $userid)) {
$newtoken->externalserviceid = $service->id;
} else {
throw new moodle_exception('nocapabilitytousethisservice');
}
$newtoken->tokentype = $tokentype;
$newtoken->userid = $userid;
$newtoken->contextid = $context->id;
$newtoken->creatorid = $USER->id;
$newtoken->timecreated = time();
$newtoken->validuntil = $validuntil;
if (!empty($iprestriction)) {
$newtoken->iprestriction = $iprestriction;
}
$DB->insert_record('external_tokens', $newtoken);
return $newtoken->token;
}