mirror of
https://github.com/moodle/moodle.git
synced 2025-04-22 17:02:03 +02:00
webservice MDL-23341 add required capabilities to the service function declaration (lib/services.php), display missing capaibilities for users linked to a token and for authorised users, display function capabilities, refactor service add function page
This commit is contained in:
parent
304f00da6c
commit
72f68b51e6
@ -22,130 +22,104 @@
|
||||
* @copyright 2009 Moodle Pty Ltd (http://moodle.com)
|
||||
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
||||
*/
|
||||
|
||||
require_once('../../config.php');
|
||||
require_once($CFG->libdir.'/adminlib.php');
|
||||
require_once($CFG->libdir.'/externallib.php');
|
||||
require_once($CFG->libdir . '/adminlib.php');
|
||||
require_once($CFG->libdir . '/externallib.php');
|
||||
require_once($CFG->dirroot . '/webservice/lib.php');
|
||||
require_once('forms.php');
|
||||
|
||||
$id = required_param('id', PARAM_INT);
|
||||
$fid = optional_param('fid', 0, PARAM_INT);
|
||||
$action = optional_param('action', '', PARAM_ACTION);
|
||||
$serviceid = required_param('id', PARAM_INT);
|
||||
$functionid = optional_param('fid', 0, PARAM_INT);
|
||||
$action = optional_param('action', '', PARAM_ACTION);
|
||||
$confirm = optional_param('confirm', 0, PARAM_BOOL);
|
||||
|
||||
$PAGE->set_url('/admin/websevice/service_functions.php', array('id'=>$id));
|
||||
admin_externalpage_setup('externalservicefunctions');
|
||||
|
||||
//define nav bar
|
||||
$PAGE->set_url('/admin/websevice/service_functions.php', array('id' => $serviceid));
|
||||
$PAGE->navbar->ignore_active(true);
|
||||
$PAGE->navbar->add(get_string('administrationsite'));
|
||||
$PAGE->navbar->add(get_string('plugins', 'admin'));
|
||||
$PAGE->navbar->add(get_string('webservices', 'webservice'));
|
||||
$PAGE->navbar->add(get_string('externalservices', 'webservice'), new moodle_url('/admin/settings.php?section=externalservices'));
|
||||
$PAGE->navbar->add(get_string('functions', 'webservice'), new moodle_url('/admin/webservice/service_functions.php?id='.$id));
|
||||
if ($action == "add") {
|
||||
$PAGE->navbar->add(get_string('addfunctions', 'webservice'));
|
||||
} else if ($action == "delete") {
|
||||
$PAGE->navbar->add(get_string('removefunction', 'webservice'));
|
||||
}
|
||||
$PAGE->navbar->add(get_string('externalservices', 'webservice'),
|
||||
new moodle_url('/admin/settings.php?section=externalservices'));
|
||||
$PAGE->navbar->add(get_string('functions', 'webservice'),
|
||||
new moodle_url('/admin/webservice/service_functions.php?id=' . $serviceid));
|
||||
|
||||
admin_externalpage_setup('externalservicefunctions');
|
||||
$service = $DB->get_record('external_services', array('id' => $serviceid), '*', MUST_EXIST);
|
||||
$webservicemanager = new webservice();
|
||||
$renderer = $PAGE->get_renderer('core', 'webservice');
|
||||
$functionlisturl = new moodle_url('/admin/webservice/service_functions.php',
|
||||
array('id' => $serviceid));
|
||||
|
||||
$returnurl = "$CFG->wwwroot/$CFG->admin/settings.php?section=externalservices";
|
||||
$thisurl = "service_functions.php?id=$id";
|
||||
// Add or Delete operations
|
||||
switch ($action) {
|
||||
case 'add':
|
||||
$PAGE->navbar->add(get_string('addfunctions', 'webservice'));
|
||||
/// Add function operation
|
||||
if (confirm_sesskey() and $service and empty($service->component)) {
|
||||
$mform = new external_service_functions_form(null,
|
||||
array('action' => 'add', 'id' => $service->id));
|
||||
|
||||
$service = $DB->get_record('external_services', array('id'=>$id), '*', MUST_EXIST);
|
||||
|
||||
if ($action === 'delete' and confirm_sesskey() and $service and empty($service->component)) {
|
||||
$function = $DB->get_record('external_functions', array('id'=>$fid), '*', MUST_EXIST);
|
||||
if (!$confirm) {
|
||||
echo $OUTPUT->header();
|
||||
$optionsyes = array('id'=>$id, 'action'=>'delete', 'confirm'=>1, 'sesskey'=>sesskey(), 'fid'=>$function->id);
|
||||
$optionsno = array('id'=>$id);
|
||||
$formcontinue = new single_button(new moodle_url('service_functions.php', $optionsyes), get_string('remove'));
|
||||
$formcancel = new single_button(new moodle_url('service_functions.php', $optionsno), get_string('cancel'), 'get');
|
||||
echo $OUTPUT->confirm(get_string('removefunctionconfirm', 'webservice', (object)array('service'=>$service->name, 'function'=>$function->name)), $formcontinue, $formcancel);
|
||||
echo $OUTPUT->footer();
|
||||
die;
|
||||
}
|
||||
$DB->delete_records('external_services_functions', array('externalserviceid'=>$service->id, 'functionname'=>$function->name));
|
||||
redirect($thisurl);
|
||||
|
||||
} else if ($action === 'add' and confirm_sesskey() and $service and empty($service->component)) {
|
||||
$mform = new external_service_functions_form(null, array('action'=>'add', 'id'=>$service->id));
|
||||
|
||||
if ($mform->is_cancelled()) {
|
||||
redirect($thisurl);
|
||||
} else if ($data = $mform->get_data()) {
|
||||
ignore_user_abort(true); // no interruption here!
|
||||
foreach($data->fid as $fid) {
|
||||
$function = $DB->get_record('external_functions', array('id'=>$fid), '*', MUST_EXIST);
|
||||
// make sure the function is not there yet
|
||||
if (!$DB->record_exists('external_services_functions', array('externalserviceid'=>$service->id, 'functionname'=>$function->name))) {
|
||||
$new = new object();
|
||||
$new->externalserviceid = $service->id;
|
||||
$new->functionname = $function->name;
|
||||
$DB->insert_record('external_services_functions', $new);
|
||||
//cancelled add operation, redirect to function list page
|
||||
if ($mform->is_cancelled()) {
|
||||
redirect($functionlisturl);
|
||||
}
|
||||
|
||||
//add the function to the service then redirect to function list page
|
||||
if ($data = $mform->get_data()) {
|
||||
ignore_user_abort(true); // no interruption here!
|
||||
foreach ($data->fid as $fid) {
|
||||
$function = $webservicemanager->get_external_function_by_id(
|
||||
$fid, MUST_EXIST);
|
||||
// make sure the function is not there yet
|
||||
if (!$webservicemanager->service_function_exists($function->name,
|
||||
$service->id)) {
|
||||
$webservicemanager->add_external_function_to_service(
|
||||
$function->name, $service->id);
|
||||
}
|
||||
}
|
||||
redirect($functionlisturl);
|
||||
}
|
||||
|
||||
//Add function operation page output
|
||||
echo $OUTPUT->header();
|
||||
echo $OUTPUT->heading($service->name);
|
||||
$mform->display();
|
||||
echo $OUTPUT->footer();
|
||||
die;
|
||||
}
|
||||
redirect($thisurl);
|
||||
}
|
||||
|
||||
//ask for function id
|
||||
echo $OUTPUT->header();
|
||||
echo $OUTPUT->heading($service->name);
|
||||
$mform->display();
|
||||
echo $OUTPUT->footer();
|
||||
die;
|
||||
break;
|
||||
|
||||
case 'delete':
|
||||
$PAGE->navbar->add(get_string('removefunction', 'webservice'));
|
||||
/// Delete function operation
|
||||
if (confirm_sesskey() and $service and empty($service->component)) {
|
||||
//check that the function to remove exists
|
||||
$function = $webservicemanager->get_external_function_by_id(
|
||||
$functionid, MUST_EXIST);
|
||||
|
||||
//display confirmation page
|
||||
if (!$confirm) {
|
||||
echo $OUTPUT->header();
|
||||
echo $renderer->admin_remove_service_function_confirmation($function, $service);
|
||||
echo $OUTPUT->footer();
|
||||
die;
|
||||
}
|
||||
|
||||
//or remove the function from the service, then redirect to the function list
|
||||
$webservicemanager->remove_external_function_from_service($function->name,
|
||||
$service->id);
|
||||
redirect($functionlisturl);
|
||||
}
|
||||
break;
|
||||
}
|
||||
|
||||
/// OUTPUT function list page
|
||||
echo $OUTPUT->header();
|
||||
|
||||
echo $OUTPUT->heading($service->name);
|
||||
|
||||
$select = "name IN (SELECT s.functionname
|
||||
FROM {external_services_functions} s
|
||||
WHERE s.externalserviceid = :sid
|
||||
)";
|
||||
|
||||
$functions = $DB->get_records_select('external_functions', $select, array('sid'=>$service->id), 'name');
|
||||
|
||||
$strfunction = get_string('function', 'webservice');
|
||||
$strdelete = get_string('removefunction', 'webservice');
|
||||
$stredit = get_string('edit');
|
||||
|
||||
$table = new html_table();
|
||||
$table->head = array($strfunction);
|
||||
$table->align = array('left');
|
||||
$table->width = '100%';
|
||||
$table->data = array();
|
||||
$table->head[] = get_string('description');
|
||||
$table->align[] = 'left';
|
||||
|
||||
if (empty($service->component)) {
|
||||
$table->head[] = $stredit;
|
||||
$table->align[] = 'center';
|
||||
}
|
||||
|
||||
$durl = "service_functions.php?sesskey=".sesskey();
|
||||
|
||||
foreach ($functions as $function) {
|
||||
$function = external_function_info($function);
|
||||
$description = "<span style=\"font-size:90%\">".$function->description."</span>"; //TODO: must use class here!
|
||||
if (empty($service->component)) {
|
||||
$delete = "<a href=\"$durl&action=delete&fid=$function->id&id=$service->id\">$strdelete</a>";
|
||||
$table->data[] = array($function->name, $description, $delete);
|
||||
} else {
|
||||
$table->data[] = array($function->name, $description);
|
||||
}
|
||||
}
|
||||
|
||||
echo html_writer::table($table);
|
||||
|
||||
|
||||
// we can edit only custom functions, the build-in would be overridden after each upgrade
|
||||
if (empty($service->component)) {
|
||||
$url = new moodle_url('service_functions.php', array('sesskey'=>sesskey(), 'id'=>$service->id, 'action'=>'add'));
|
||||
echo "<a href=$url>".get_string('add')."</a>";
|
||||
}
|
||||
|
||||
|
||||
$functions = $webservicemanager->get_external_functions(array($service->id));
|
||||
echo $renderer->admin_service_function_list($functions, $service);
|
||||
echo $OUTPUT->footer();
|
||||
|
||||
|
@ -91,9 +91,19 @@ $selectoroptions->alloweduserselector = $alloweduserselector;
|
||||
$selectoroptions->potentialuserselector = $potentialuserselector;
|
||||
echo $renderer->admin_authorised_user_selector($selectoroptions);
|
||||
|
||||
//display the list of allowed users with their options (ip/timecreated / validuntil...)
|
||||
//check that the user has the service required capability (if needed)
|
||||
/// get the missing capabilities for all users (will be displayed into the renderer)
|
||||
$allowedusers = $webservicemanager->get_ws_authorised_users($id);
|
||||
$usersmissingcaps = $webservicemanager->get_missing_capabilities_by_users($allowedusers, $id);
|
||||
|
||||
//add the missing capabilities to the allowed users object to be displayed by renderer
|
||||
foreach ($allowedusers as &$alloweduser) {
|
||||
if (!is_siteadmin($alloweduser->id)) {
|
||||
$alloweduser->missingcapabilities = implode(',', $usersmissingcaps[$alloweduser->id]);
|
||||
}
|
||||
}
|
||||
|
||||
/// display the list of allowed users with their options (ip/timecreated / validuntil...)
|
||||
//check that the user has the service required capability (if needed)
|
||||
if (!empty($allowedusers)) {
|
||||
$renderer = $PAGE->get_renderer('core', 'webservice');
|
||||
echo $OUTPUT->heading(get_string('serviceuserssettings', 'webservice'), 3, 'main');
|
||||
|
@ -105,6 +105,9 @@ $string['key'] = 'Key';
|
||||
$string['keyshelp'] = 'The keys are used to access your Moodle account from external applications.';
|
||||
$string['manageprotocols'] = 'Manage protocols';
|
||||
$string['managetokens'] = 'Manage tokens';
|
||||
$string['missingcaps'] = 'Missing capabilities.';
|
||||
$string['missingcaps_help'] = 'List of capabilities that the service functions require but that the user hasn\'t. You need to add these capabilities to this user in order to use the service.
|
||||
<br/><br/>Note: in rare case some of these "required" capabilities could depend of a function use case. For example \'moodle_role_get_enrolled_users\' function requires \'moodle/site:viewparticipants\' capability only for a site-wide request.';
|
||||
$string['missingpassword'] = 'Missing password';
|
||||
$string['missingusername'] = 'Missing username';
|
||||
$string['norequiredcapability'] = 'No required capability';
|
||||
@ -126,6 +129,7 @@ $string['requireauthentication'] = 'This method requires authentication with xxx
|
||||
$string['required'] = 'Required';
|
||||
$string['requiredcapability'] = 'Required capability';
|
||||
$string['requiredcapability_help'] = 'If set, only users with the required capability can access the service.';
|
||||
$string['requiredcaps'] = 'Required capabilities';
|
||||
$string['resettokenconfirm'] = 'Do you really want to reset this web service key for <strong>{$a->user}</strong> on the service <strong>{$a->service}</strong>?';
|
||||
$string['resettokenconfirmsimple'] = 'Do you really want to reset this key? Any saved links containing the old key will not work anymore.';
|
||||
$string['response'] = 'Response';
|
||||
@ -140,7 +144,7 @@ $string['selectedcapability'] = 'Selected';
|
||||
$string['selectedcapabilitydoesntexit'] = 'The currently set required capability ({$a}) doesn\'t exist any more. Please change it and save the changes.';
|
||||
$string['selectservice'] = 'Select a service';
|
||||
$string['selectspecificuser'] = 'Select a specific user';
|
||||
$string['selectspecificuserdescription'] = 'On the <strong>Manage service</strong> page, click on \'Authorised users\' and add the user to the authorised users list.';
|
||||
$string['selectspecificuserdescription'] = 'On the <strong>Manage service</strong> page, click on \'Authorised users\' and add the user to the authorised users list. The required user capabilities will be displayed. We recommend to create a role for this service and to add the capabilities to the role, then assign the role to the user.';
|
||||
$string['service'] = 'Service';
|
||||
$string['servicehelpexplanation'] = 'A service is a set of functions. A service can be accessed by all users or just specified users. <strong>Custom services</strong> are services that you created. <strong>Default services</strong> are services that were added by default to Moodle. You can not change functions from a <strong>Default service</strong>.';
|
||||
$string['servicename'] = 'Service name';
|
||||
@ -164,6 +168,7 @@ $string['tokencreator'] = 'Creator';
|
||||
$string['updateusersettings'] = 'Update';
|
||||
$string['userasclients'] = 'Users as clients with token';
|
||||
$string['userasclientsdescription'] = 'The following steps help you to set up the Moodle web service for users as clients. These steps also help to set up the recommended token (security keys) authentication method. In this use case, the user will generate his token from his <strong>Security keys</strong> profile page.';
|
||||
$string['usermissingcaps'] = 'Missing capabilities: {$a}.';
|
||||
$string['usersettingssaved'] = 'User settings saved';
|
||||
$string['validuntil'] = 'Valid until';
|
||||
$string['validuntil_help'] = 'If set, the service will be inactivated after this date for this user.';
|
||||
|
@ -6936,8 +6936,8 @@ class admin_setting_managewebservicetokens extends admin_setting {
|
||||
$return = $OUTPUT->box_start('generalbox webservicestokenui');
|
||||
|
||||
$table = new html_table();
|
||||
$table->head = array($strtoken, $struser, $strservice, $strcontext, $striprestriction, $strvaliduntil, $stroperation);
|
||||
$table->align = array('left', 'left', 'left', 'center', 'center', 'center', 'center');
|
||||
$table->head = array($strtoken, $struser, $strservice, $striprestriction, $strvaliduntil, $stroperation);
|
||||
$table->align = array('left', 'left', 'left', 'center', 'center', 'center');
|
||||
$table->width = '100%';
|
||||
$table->data = array();
|
||||
|
||||
@ -6946,7 +6946,7 @@ class admin_setting_managewebservicetokens extends admin_setting {
|
||||
//TODO: in order to let the administrator delete obsolete token, split this request in multiple request or use LEFT JOIN
|
||||
|
||||
//here retrieve token list (including linked users firstname/lastname and linked services name)
|
||||
$sql = "SELECT t.id, t.token, u.id AS userid, u.firstname, u.lastname, s.name, t.validuntil
|
||||
$sql = "SELECT t.id, t.token, u.id AS userid, u.firstname, u.lastname, s.name, t.validuntil, s.id AS serviceid
|
||||
FROM {external_tokens} t, {user} u, {external_services} s
|
||||
WHERE t.creatorid=? AND t.tokentype = ? AND s.id = t.externalserviceid AND t.userid = u.id";
|
||||
$tokens = $DB->get_records_sql($sql, array($USER->id, EXTERNAL_TOKEN_PERMANENT));
|
||||
@ -6971,7 +6971,26 @@ class admin_setting_managewebservicetokens extends admin_setting {
|
||||
$useratag = html_writer::start_tag('a', array('href' => $userprofilurl));
|
||||
$useratag .= $token->firstname." ".$token->lastname;
|
||||
$useratag .= html_writer::end_tag('a');
|
||||
$table->data[] = array($token->token, $useratag, $token->name, '', $iprestriction, $validuntil, $delete);
|
||||
|
||||
//check user missing capabilities
|
||||
require_once($CFG->dirroot . '/webservice/lib.php');
|
||||
$webservicemanager = new webservice();
|
||||
$usermissingcaps = $webservicemanager->get_missing_capabilities_by_users(
|
||||
array(array('id' => $token->userid)), $token->serviceid);
|
||||
|
||||
if (!is_siteadmin($token->userid)) {
|
||||
$missingcapabilities = implode(',',
|
||||
$usermissingcaps[$token->userid]);
|
||||
if (!empty($missingcapabilities)) {
|
||||
$useratag .= html_writer::tag('div',
|
||||
get_string('usermissingcaps', 'webservice',
|
||||
$missingcapabilities)
|
||||
. ' ' . $OUTPUT->help_icon('missingcaps', 'webservice'),
|
||||
array('class' => 'missingcaps', 'id' => 'usermissingcaps'));
|
||||
}
|
||||
}
|
||||
|
||||
$table->data[] = array($token->token, $useratag, $token->name, $iprestriction, $validuntil, $delete);
|
||||
}
|
||||
|
||||
$return .= html_writer::table($table);
|
||||
|
@ -1,5 +1,5 @@
|
||||
<?xml version="1.0" encoding="UTF-8" ?>
|
||||
<XMLDB PATH="lib/db" VERSION="20100718" COMMENT="XMLDB file for core Moodle tables"
|
||||
<XMLDB PATH="lib/db" VERSION="20100721" COMMENT="XMLDB file for core Moodle tables"
|
||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xsi:noNamespaceSchemaLocation="../../lib/xmldb/xmldb.xsd"
|
||||
>
|
||||
@ -2484,7 +2484,8 @@
|
||||
<FIELD NAME="classname" TYPE="char" LENGTH="100" NOTNULL="true" SEQUENCE="false" PREVIOUS="name" NEXT="methodname"/>
|
||||
<FIELD NAME="methodname" TYPE="char" LENGTH="100" NOTNULL="true" SEQUENCE="false" PREVIOUS="classname" NEXT="classpath"/>
|
||||
<FIELD NAME="classpath" TYPE="char" LENGTH="255" NOTNULL="false" SEQUENCE="false" PREVIOUS="methodname" NEXT="component"/>
|
||||
<FIELD NAME="component" TYPE="char" LENGTH="100" NOTNULL="true" SEQUENCE="false" PREVIOUS="classpath"/>
|
||||
<FIELD NAME="component" TYPE="char" LENGTH="100" NOTNULL="true" SEQUENCE="false" PREVIOUS="classpath" NEXT="capabilities"/>
|
||||
<FIELD NAME="capabilities" TYPE="char" LENGTH="255" NOTNULL="false" SEQUENCE="false" COMMENT="all capabilities that are required to be run by the function (separated by comma)" PREVIOUS="component"/>
|
||||
</FIELDS>
|
||||
<KEYS>
|
||||
<KEY NAME="primary" TYPE="primary" FIELDS="id"/>
|
||||
|
@ -34,6 +34,7 @@ $functions = array(
|
||||
'classpath' => 'group/externallib.php',
|
||||
'description' => 'Creates new groups.',
|
||||
'type' => 'write',
|
||||
'capabilities'=> 'moodle/course:managegroups',
|
||||
),
|
||||
|
||||
'moodle_group_get_groups' => array(
|
||||
@ -42,6 +43,7 @@ $functions = array(
|
||||
'classpath' => 'group/externallib.php',
|
||||
'description' => 'Returns group details.',
|
||||
'type' => 'read',
|
||||
'capabilities'=> 'moodle/course:managegroups',
|
||||
),
|
||||
|
||||
'moodle_group_get_course_groups' => array(
|
||||
@ -50,6 +52,7 @@ $functions = array(
|
||||
'classpath' => 'group/externallib.php',
|
||||
'description' => 'Returns all groups in specified course.',
|
||||
'type' => 'read',
|
||||
'capabilities'=> 'moodle/course:managegroups',
|
||||
),
|
||||
|
||||
'moodle_group_delete_groups' => array(
|
||||
@ -58,6 +61,7 @@ $functions = array(
|
||||
'classpath' => 'group/externallib.php',
|
||||
'description' => 'Deletes all specified groups.',
|
||||
'type' => 'delete',
|
||||
'capabilities'=> 'moodle/course:managegroups',
|
||||
),
|
||||
|
||||
'moodle_group_get_groupmembers' => array(
|
||||
@ -66,6 +70,7 @@ $functions = array(
|
||||
'classpath' => 'group/externallib.php',
|
||||
'description' => 'Returns group members.',
|
||||
'type' => 'read',
|
||||
'capabilities'=> 'moodle/course:managegroups',
|
||||
),
|
||||
|
||||
'moodle_group_add_groupmembers' => array(
|
||||
@ -74,6 +79,7 @@ $functions = array(
|
||||
'classpath' => 'group/externallib.php',
|
||||
'description' => 'Adds group members.',
|
||||
'type' => 'write',
|
||||
'capabilities'=> 'moodle/course:managegroups',
|
||||
),
|
||||
|
||||
'moodle_group_delete_groupmembers' => array(
|
||||
@ -82,7 +88,12 @@ $functions = array(
|
||||
'classpath' => 'group/externallib.php',
|
||||
'description' => 'Deletes group members.',
|
||||
'type' => 'delete',
|
||||
'capabilities'=> 'moodle/course:managegroups',
|
||||
),
|
||||
|
||||
|
||||
// === file related functions ===
|
||||
|
||||
'moodle_file_get_files' => array(
|
||||
'classname' => 'moodle_file_external',
|
||||
'methodname' => 'get_files',
|
||||
@ -99,12 +110,14 @@ $functions = array(
|
||||
),
|
||||
|
||||
// === user related functions ===
|
||||
|
||||
'moodle_user_create_users' => array(
|
||||
'classname' => 'moodle_user_external',
|
||||
'methodname' => 'create_users',
|
||||
'classpath' => 'user/externallib.php',
|
||||
'description' => 'Create users.',
|
||||
'type' => 'write',
|
||||
'capabilities'=> 'moodle/user:create',
|
||||
),
|
||||
|
||||
'moodle_user_get_users_by_id' => array(
|
||||
@ -113,6 +126,7 @@ $functions = array(
|
||||
'classpath' => 'user/externallib.php',
|
||||
'description' => 'Get users by id.',
|
||||
'type' => 'read',
|
||||
'capabilities'=> 'moodle/user:viewdetails',
|
||||
),
|
||||
|
||||
'moodle_user_delete_users' => array(
|
||||
@ -121,6 +135,7 @@ $functions = array(
|
||||
'classpath' => 'user/externallib.php',
|
||||
'description' => 'Delete users.',
|
||||
'type' => 'write',
|
||||
'capabilities'=> 'moodle/user:delete',
|
||||
),
|
||||
|
||||
'moodle_user_update_users' => array(
|
||||
@ -129,14 +144,19 @@ $functions = array(
|
||||
'classpath' => 'user/externallib.php',
|
||||
'description' => 'Update users.',
|
||||
'type' => 'write',
|
||||
'capabilities'=> 'moodle/user:update',
|
||||
),
|
||||
|
||||
// === enrol related functions ===
|
||||
|
||||
'moodle_enrol_get_enrolled_users' => array(
|
||||
'classname' => 'moodle_enrol_external',
|
||||
'methodname' => 'get_enrolled_users',
|
||||
'classpath' => 'enrol/externallib.php',
|
||||
'description' => 'Get list of course participants',
|
||||
'type' => 'read',
|
||||
'capabilities'=> 'moodle/site:viewparticipants, moodle/course:viewparticipants,
|
||||
moodle/role:review, moodle/site:accessallgroups, moodle/course:enrolreview',
|
||||
),
|
||||
|
||||
'moodle_role_assign' => array(
|
||||
@ -145,6 +165,7 @@ $functions = array(
|
||||
'classpath' => 'enrol/externallib.php',
|
||||
'description' => 'Manual role assignments.',
|
||||
'type' => 'write',
|
||||
'capabilities'=> 'moodle/role:assign',
|
||||
),
|
||||
|
||||
'moodle_role_unassign' => array(
|
||||
@ -153,6 +174,7 @@ $functions = array(
|
||||
'classpath' => 'enrol/externallib.php',
|
||||
'description' => 'Manual role unassignments.',
|
||||
'type' => 'write',
|
||||
'capabilities'=> 'moodle/role:assign',
|
||||
),
|
||||
|
||||
|
||||
);
|
||||
|
@ -4863,6 +4863,22 @@ WHERE gradeitemid IS NOT NULL AND grademax IS NOT NULL");
|
||||
upgrade_main_savepoint(true, 2010071800);
|
||||
}
|
||||
|
||||
if ($oldversion < 2010072300) {
|
||||
|
||||
// Define field capabilities to be added to external_functions
|
||||
$table = new xmldb_table('external_functions');
|
||||
$field = new xmldb_field('capabilities', XMLDB_TYPE_CHAR, '255', null, null, null, null, 'component');
|
||||
|
||||
// Conditionally launch add field capabilities
|
||||
if (!$dbman->field_exists($table, $field)) {
|
||||
$dbman->add_field($table, $field);
|
||||
}
|
||||
|
||||
// Main savepoint reached
|
||||
upgrade_main_savepoint(true, 2010072300);
|
||||
}
|
||||
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
|
@ -759,6 +759,11 @@ function external_update_descriptions($component) {
|
||||
$dbfunction->classpath = $function['classpath'];
|
||||
$update = true;
|
||||
}
|
||||
$functioncapabilities = key_exists('capabilities', $function)?$function['capabilities']:'';
|
||||
if ($dbfunction->capabilities != $functioncapabilities) {
|
||||
$dbfunction->capabilities = $functioncapabilities;
|
||||
$update = true;
|
||||
}
|
||||
if ($update) {
|
||||
$DB->update_record('external_functions', $dbfunction);
|
||||
}
|
||||
@ -770,6 +775,7 @@ function external_update_descriptions($component) {
|
||||
$dbfunction->methodname = $function['methodname'];
|
||||
$dbfunction->classpath = empty($function['classpath']) ? null : $function['classpath'];
|
||||
$dbfunction->component = $component;
|
||||
$dbfunction->capabilities = key_exists('capabilities', $function)?$function['capabilities']:'';
|
||||
$dbfunction->id = $DB->insert_record('external_functions', $dbfunction);
|
||||
}
|
||||
unset($functions);
|
||||
|
@ -115,3 +115,10 @@
|
||||
.plugincompattable td.ok {color: #008000;}
|
||||
.plugincompattable td.warning {color: #DF7800;}
|
||||
.plugincompattable td.error {color: #DF0000;}
|
||||
|
||||
/**
|
||||
* Web services
|
||||
*/
|
||||
#page-admin-webservice-service_users .missingcaps {color: #ff6600;font-size: 90%;}
|
||||
#page-admin-setting-webservicetokens .missingcaps {color: #ff6600;font-size: 90%;}
|
||||
#page-admin-websevice-service_functions .functiondesc {font-size: 90%;}
|
@ -142,4 +142,12 @@ table.flexible .r1 {background-color: #FAFAFA;}
|
||||
.path-admin .manageauthtable {width:90%;margin:0 auto;}
|
||||
|
||||
#explaincaps .rolecap.yes {background-color: #DDFFDD;}
|
||||
#explaincaps .rolecap.no {background-color: #FFDDDD;}
|
||||
#explaincaps .rolecap.no {background-color: #FFDDDD;}
|
||||
|
||||
|
||||
/**
|
||||
* Web services
|
||||
*/
|
||||
#page-admin-webservice-service_users .missingcaps {color: #ff6600;font-size: 90%;}
|
||||
#page-admin-setting-webservicetokens .missingcaps {color: #ff6600;font-size: 90%;}
|
||||
#page-admin-websevice-service_functions .functiondesc {font-size: 90%;}
|
@ -6,7 +6,7 @@
|
||||
// This is compared against the values stored in the database to determine
|
||||
// whether upgrades should be performed (see lib/db/*.php)
|
||||
|
||||
$version = 2010072201; // YYYYMMDD = date of the last version bump
|
||||
$version = 2010072300; // YYYYMMDD = date of the last version bump
|
||||
// XX = daily increments
|
||||
|
||||
$release = '2.0 Preview 4+ (Build: 20100723)'; // Human-friendly version name
|
||||
|
@ -224,8 +224,169 @@ class webservice {
|
||||
global $DB;
|
||||
return $DB->get_record('external_tokens', array('token'=>$token), '*', MUST_EXIST);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the list of all functions for given service ids
|
||||
* @param array $serviceids
|
||||
* @return array functions
|
||||
*/
|
||||
public function get_external_functions($serviceids) {
|
||||
global $DB;
|
||||
if (!empty($serviceids)) {
|
||||
list($serviceids, $params) = $DB->get_in_or_equal($serviceids);
|
||||
$sql = "SELECT f.*
|
||||
FROM {external_functions} f
|
||||
WHERE f.name IN (SELECT sf.functionname
|
||||
FROM {external_services_functions} sf
|
||||
WHERE sf.externalserviceid $serviceids)";
|
||||
$functions = $DB->get_records_sql($sql, $params);
|
||||
} else {
|
||||
$functions = array();
|
||||
}
|
||||
return $functions;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get list of required capabilities of a service, sorted by functions
|
||||
* @param integer $serviceid
|
||||
* @return array
|
||||
* example of return value:
|
||||
* Array
|
||||
* (
|
||||
* [moodle_group_create_groups] => Array
|
||||
* (
|
||||
* [0] => moodle/course:managegroups
|
||||
* )
|
||||
*
|
||||
* [moodle_enrol_get_enrolled_users] => Array
|
||||
* (
|
||||
* [0] => moodle/site:viewparticipants
|
||||
* [1] => moodle/course:viewparticipants
|
||||
* [2] => moodle/role:review
|
||||
* [3] => moodle/site:accessallgroups
|
||||
* [4] => moodle/course:enrolreview
|
||||
* )
|
||||
* )
|
||||
*/
|
||||
public function get_service_required_capabilities($serviceid) {
|
||||
$functions = $this->get_external_functions(array($serviceid));
|
||||
$requiredusercaps = array();
|
||||
foreach ($functions as $function) {
|
||||
$functioncaps = split(',', $function->capabilities);
|
||||
if (!empty($functioncaps) and !empty($functioncaps[0])) {
|
||||
foreach ($functioncaps as $functioncap) {
|
||||
$requiredusercaps[$function->name][] = trim($functioncap);
|
||||
}
|
||||
}
|
||||
}
|
||||
return $requiredusercaps;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get user capabilities (with context)
|
||||
* Only usefull for documentation purpose
|
||||
* @param integer $userid
|
||||
* @return array
|
||||
*/
|
||||
public function get_user_capabilities($userid) {
|
||||
global $DB;
|
||||
//retrieve the user capabilities
|
||||
$sql = "SELECT rc.id, rc.capability FROM {role_capabilities} rc, {role_assignments} ra
|
||||
WHERE rc.roleid=ra.roleid AND ra.id= ?";
|
||||
$dbusercaps = $DB->get_records_sql($sql, array($userid));
|
||||
$usercaps = array();
|
||||
foreach ($dbusercaps as $usercap) {
|
||||
$usercaps[$usercap->capability] = true;
|
||||
}
|
||||
return $usercaps;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get users missing capabilities for a given service
|
||||
* @param array $users
|
||||
* @param integer $serviceid
|
||||
* @return array of missing capabilities, the key being the user id
|
||||
*/
|
||||
public function get_missing_capabilities_by_users($users, $serviceid) {
|
||||
global $DB;
|
||||
$usersmissingcaps = array();
|
||||
|
||||
//retrieve capabilities required by the service
|
||||
$servicecaps = $this->get_service_required_capabilities($serviceid);
|
||||
|
||||
//retrieve users missing capabilities
|
||||
foreach ($users as $user) {
|
||||
//cast user array into object to be a bit more flexible
|
||||
if (is_array($user)) {
|
||||
$user = (object) $user;
|
||||
}
|
||||
$usercaps = $this->get_user_capabilities($user->id);
|
||||
|
||||
//detect the missing capabilities
|
||||
foreach ($servicecaps as $functioname => $functioncaps) {
|
||||
foreach ($functioncaps as $functioncap) {
|
||||
if (!key_exists($functioncap, $usercaps)) {
|
||||
if (!isset($usersmissingcaps[$user->id])
|
||||
or array_search($functioncap, $usersmissingcaps[$user->id]) === false) {
|
||||
$usersmissingcaps[$user->id][] = $functioncap;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return $usersmissingcaps;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get a external function for a given id
|
||||
* @param function id $functionid
|
||||
* @param integer $strictness IGNORE_MISSING, MUST_EXIST...
|
||||
* @return object external function
|
||||
*/
|
||||
public function get_external_function_by_id($functionid, $strictness=IGNORE_MISSING) {
|
||||
global $DB;
|
||||
$function = $DB->get_record('external_functions',
|
||||
array('id' => $functionid), '*', $strictness);
|
||||
return $function;
|
||||
}
|
||||
|
||||
/**
|
||||
* Add a function to a service
|
||||
* @param string $functionname
|
||||
* @param integer $serviceid
|
||||
*/
|
||||
public function add_external_function_to_service($functionname, $serviceid) {
|
||||
global $DB;
|
||||
$addedfunction = new object();
|
||||
$addedfunction->externalserviceid = $serviceid;
|
||||
$addedfunction->functionname = $functionname;
|
||||
$DB->insert_record('external_services_functions', $addedfunction);
|
||||
}
|
||||
|
||||
/**
|
||||
* Test whether a external function is already linked to a service
|
||||
* @param string $functionname
|
||||
* @param integer $serviceid
|
||||
* @return bool true if a matching function exists for the service, else false.
|
||||
* @throws dml_exception if error
|
||||
*/
|
||||
public function service_function_exists($functionname, $serviceid) {
|
||||
global $DB;
|
||||
return $DB->record_exists('external_services_functions',
|
||||
array('externalserviceid' => $serviceid,
|
||||
'functionname' => $functionname));
|
||||
}
|
||||
|
||||
public function remove_external_function_from_service($functionname, $serviceid) {
|
||||
global $DB;
|
||||
$DB->delete_records('external_services_functions',
|
||||
array('externalserviceid' => $serviceid, 'functionname' => $functionname));
|
||||
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* Exception indicating access control problem in web service call
|
||||
@ -562,17 +723,8 @@ abstract class webservice_zend_server extends webservice_server {
|
||||
$rs->close();
|
||||
|
||||
// now get the list of all functions
|
||||
if ($serviceids) {
|
||||
list($serviceids, $params) = $DB->get_in_or_equal($serviceids);
|
||||
$sql = "SELECT f.*
|
||||
FROM {external_functions} f
|
||||
WHERE f.name IN (SELECT sf.functionname
|
||||
FROM {external_services_functions} sf
|
||||
WHERE sf.externalserviceid $serviceids)";
|
||||
$functions = $DB->get_records_sql($sql, $params);
|
||||
} else {
|
||||
$functions = array();
|
||||
}
|
||||
$wsmanager = new webservice();
|
||||
$functions = $wsmanager->get_external_functions($serviceids);
|
||||
|
||||
// now make the virtual WS class with all the fuctions for this particular user
|
||||
$methods = '';
|
||||
|
@ -35,10 +35,8 @@ class core_webservice_renderer extends plugin_renderer_base {
|
||||
* @return string html
|
||||
*/
|
||||
public function admin_authorised_user_selector(&$options) {
|
||||
global $OUTPUT;
|
||||
|
||||
$formcontent = html_writer::empty_tag('input',
|
||||
array('name' => 'sesskey', 'value' => sesskey(), 'type' => 'hidden'));
|
||||
array('name' => 'sesskey', 'value' => sesskey(), 'type' => 'hidden'));
|
||||
|
||||
$table = new html_table();
|
||||
$table->size = array('45%', '10%', '45%');
|
||||
@ -62,16 +60,18 @@ class core_webservice_renderer extends plugin_renderer_base {
|
||||
|
||||
//create the three cells
|
||||
$label = html_writer::tag('label', get_string('serviceusers', 'webservice'),
|
||||
array('for' => 'removeselect'));
|
||||
array('for' => 'removeselect'));
|
||||
$label = html_writer::tag('p', $label);
|
||||
$authoriseduserscell = new html_table_cell($label . $options->alloweduserselector->display(true));
|
||||
$authoriseduserscell = new html_table_cell($label .
|
||||
$options->alloweduserselector->display(true));
|
||||
$authoriseduserscell->id = 'existingcell';
|
||||
$buttonscell = new html_table_cell($addbutton . html_writer::empty_tag('br') . $removebutton);
|
||||
$buttonscell->id = 'buttonscell';
|
||||
$label = html_writer::tag('label', get_string('potusers', 'webservice'),
|
||||
array('for' => 'addselect'));
|
||||
array('for' => 'addselect'));
|
||||
$label = html_writer::tag('p', $label);
|
||||
$otheruserscell = new html_table_cell($label . $options->potentialuserselector->display(true));
|
||||
$otheruserscell = new html_table_cell($label .
|
||||
$options->potentialuserselector->display(true));
|
||||
$otheruserscell->id = 'potentialcell';
|
||||
|
||||
$cells = array($authoriseduserscell, $buttonscell, $otheruserscell);
|
||||
@ -84,7 +84,7 @@ class core_webservice_renderer extends plugin_renderer_base {
|
||||
$actionurl = new moodle_url('/admin/webservice/service_users.php',
|
||||
array('id' => $options->serviceid));
|
||||
$html = html_writer::tag('form', $formcontent,
|
||||
array('id' => 'assignform', 'action' => $actionurl, 'method' => 'post'));
|
||||
array('id' => 'assignform', 'action' => $actionurl, 'method' => 'post'));
|
||||
return $html;
|
||||
}
|
||||
|
||||
@ -94,33 +94,121 @@ class core_webservice_renderer extends plugin_renderer_base {
|
||||
* @return string $html
|
||||
*/
|
||||
public function admin_authorised_user_list($users, $serviceid) {
|
||||
global $OUTPUT;
|
||||
$html = $OUTPUT->box_start('generalbox', 'alloweduserlist');
|
||||
$html = $this->output->box_start('generalbox', 'alloweduserlist');
|
||||
foreach ($users as $user) {
|
||||
$modifiedauthoriseduserurl = new moodle_url('/admin/webservice/service_user_settings.php',
|
||||
array('userid' => $user->id, 'serviceid' => $serviceid));
|
||||
$html .= html_writer::tag('a', $user->firstname . " " . $user->lastname . ", " . $user->email,
|
||||
$html .= html_writer::tag('a', $user->firstname . " "
|
||||
. $user->lastname . ", " . $user->email,
|
||||
array('href' => $modifiedauthoriseduserurl));
|
||||
$html .= html_writer::empty_tag('br');
|
||||
//add missing capabilities
|
||||
if (!empty($user->missingcapabilities)) {
|
||||
$html .= html_writer::tag('div',
|
||||
get_string('usermissingcaps', 'webservice', $user->missingcapabilities)
|
||||
. ' ' . $this->output->help_icon('missingcaps', 'webservice'),
|
||||
array('class' => 'missingcaps', 'id' => 'usermissingcaps'));
|
||||
$html .= html_writer::empty_tag('br');
|
||||
} else {
|
||||
$html .= html_writer::empty_tag('br') . html_writer::empty_tag('br');
|
||||
}
|
||||
}
|
||||
$html .= $OUTPUT->box_end();
|
||||
$html .= $this->output->box_end();
|
||||
return $html;
|
||||
}
|
||||
|
||||
/**
|
||||
* Display Reset token confirmation box
|
||||
* Display a confirmation page to remove a function from a service
|
||||
* @param object $function
|
||||
* @param object $service
|
||||
* @return string html
|
||||
*/
|
||||
public function admin_remove_service_function_confirmation($function, $service) {
|
||||
$optionsyes = array('id' => $service->id, 'action' => 'delete',
|
||||
'confirm' => 1, 'sesskey' => sesskey(), 'fid' => $function->id);
|
||||
$optionsno = array('id' => $service->id);
|
||||
$formcontinue = new single_button(new moodle_url('service_functions.php',
|
||||
$optionsyes), get_string('remove'));
|
||||
$formcancel = new single_button(new moodle_url('service_functions.php',
|
||||
$optionsno), get_string('cancel'), 'get');
|
||||
return $this->output->confirm(get_string('removefunctionconfirm', 'webservice',
|
||||
(object) array('service' => $service->name, 'function' => $function->name)),
|
||||
$formcontinue, $formcancel);
|
||||
}
|
||||
|
||||
/**
|
||||
* Display list of function for a given service
|
||||
* If the service is build-in do not display remove/add operation (read-only)
|
||||
* @param array $functions
|
||||
* @param object $service
|
||||
* @return string the table html + add operation html
|
||||
*/
|
||||
public function admin_service_function_list($functions, $service) {
|
||||
$table = new html_table();
|
||||
$table->head = array(get_string('function', 'webservice'),
|
||||
get_string('description'), get_string('requiredcaps', 'webservice'));
|
||||
$table->align = array('left', 'left', 'left');
|
||||
$table->size = array('15%', '40%', '40%');
|
||||
$table->width = '100%';
|
||||
$table->align[] = 'left';
|
||||
|
||||
//display remove function operation (except for build-in service)
|
||||
if (empty($service->component)) {
|
||||
$table->head[] = get_string('edit');
|
||||
$table->align[] = 'center';
|
||||
}
|
||||
|
||||
foreach ($functions as $function) {
|
||||
$function = external_function_info($function);
|
||||
$requiredcaps = html_writer::tag('div',
|
||||
empty($function->capabilities)?'':$function->capabilities,
|
||||
array('class' => 'functiondesc'));;
|
||||
$description = html_writer::tag('div', $function->description,
|
||||
array('class' => 'functiondesc'));
|
||||
//display remove function operation (except for build-in service)
|
||||
if (empty($service->component)) {
|
||||
$removeurl = new moodle_url('/admin/webservice/service_functions.php',
|
||||
array('sesskey' => sesskey(), 'fid' => $function->id,
|
||||
'id' => $service->id,
|
||||
'action' => 'delete'));
|
||||
$removelink = html_writer::tag('a',
|
||||
get_string('removefunction', 'webservice'),
|
||||
array('href' => $removeurl));
|
||||
$table->data[] = array($function->name, $description, $requiredcaps, $removelink);
|
||||
} else {
|
||||
$table->data[] = array($function->name, $description, $requiredcaps);
|
||||
}
|
||||
}
|
||||
|
||||
$html = html_writer::table($table);
|
||||
|
||||
//display add function operation (except for build-in service)
|
||||
if (empty($service->component)) {
|
||||
$addurl = new moodle_url('/admin/webservice/service_functions.php',
|
||||
array('sesskey' => sesskey(), 'id' => $service->id, 'action' => 'add'));
|
||||
$html .= html_writer::tag('a', get_string('add'), array('href' => $addurl));
|
||||
}
|
||||
|
||||
return $html;
|
||||
}
|
||||
|
||||
/**
|
||||
* Display Reset token confirmation box
|
||||
* @param object $token to reset
|
||||
* @return string html
|
||||
*/
|
||||
public function user_reset_token_confirmation($token) {
|
||||
global $OUTPUT, $CFG;
|
||||
global $CFG;
|
||||
$managetokenurl = $CFG->wwwroot . "/user/managetoken.php?sesskey=" . sesskey();
|
||||
$optionsyes = array('tokenid' => $token->id, 'action' => 'resetwstoken', 'confirm' => 1, 'sesskey' => sesskey());
|
||||
$optionsyes = array('tokenid' => $token->id, 'action' => 'resetwstoken', 'confirm' => 1,
|
||||
'sesskey' => sesskey());
|
||||
$optionsno = array('section' => 'webservicetokens', 'sesskey' => sesskey());
|
||||
$formcontinue = new single_button(new moodle_url($managetokenurl, $optionsyes), get_string('reset'));
|
||||
$formcancel = new single_button(new moodle_url($managetokenurl, $optionsno), get_string('cancel'), 'get');
|
||||
$html = $OUTPUT->confirm(get_string('resettokenconfirm', 'webservice',
|
||||
(object) array('user' => $token->firstname . " " . $token->lastname, 'service' => $token->name)),
|
||||
$formcontinue = new single_button(new moodle_url($managetokenurl, $optionsyes),
|
||||
get_string('reset'));
|
||||
$formcancel = new single_button(new moodle_url($managetokenurl, $optionsno),
|
||||
get_string('cancel'), 'get');
|
||||
$html = $this->output->confirm(get_string('resettokenconfirm', 'webservice',
|
||||
(object) array('user' => $token->firstname . " " .
|
||||
$token->lastname, 'service' => $token->name)),
|
||||
$formcontinue, $formcancel);
|
||||
return $html;
|
||||
}
|
||||
@ -132,7 +220,7 @@ class core_webservice_renderer extends plugin_renderer_base {
|
||||
* @return string html code
|
||||
*/
|
||||
public function user_webservice_tokens_box($tokens, $userid) {
|
||||
global $OUTPUT, $CFG;
|
||||
global $CFG;
|
||||
|
||||
// display strings
|
||||
$stroperation = get_string('operation', 'webservice');
|
||||
@ -142,8 +230,8 @@ class core_webservice_renderer extends plugin_renderer_base {
|
||||
$strcontext = get_string('context', 'webservice');
|
||||
$strvaliduntil = get_string('validuntil', 'webservice');
|
||||
|
||||
$return = $OUTPUT->heading(get_string('securitykeys', 'webservice'), 3, 'main', true);
|
||||
$return .= $OUTPUT->box_start('generalbox webservicestokenui');
|
||||
$return = $this->output->heading(get_string('securitykeys', 'webservice'), 3, 'main', true);
|
||||
$return .= $this->output->box_start('generalbox webservicestokenui');
|
||||
|
||||
$return .= get_string('keyshelp', 'webservice');
|
||||
|
||||
@ -158,8 +246,8 @@ class core_webservice_renderer extends plugin_renderer_base {
|
||||
//TODO: retrieve context
|
||||
|
||||
if ($token->creatorid == $userid) {
|
||||
$reset = "<a href=\"" . $CFG->wwwroot . "/user/managetoken.php?sesskey=" . sesskey() .
|
||||
"&action=resetwstoken&tokenid=" . $token->id . "\">";
|
||||
$reset = "<a href=\"" . $CFG->wwwroot . "/user/managetoken.php?sesskey="
|
||||
. sesskey() . "&action=resetwstoken&tokenid=" . $token->id . "\">";
|
||||
$reset .= get_string('reset') . "</a>";
|
||||
$creator = $token->firstname . " " . $token->lastname;
|
||||
} else {
|
||||
@ -188,13 +276,14 @@ class core_webservice_renderer extends plugin_renderer_base {
|
||||
$return .= get_string('notoken', 'webservice');
|
||||
}
|
||||
|
||||
$return .= $OUTPUT->box_end();
|
||||
$return .= $this->output->box_end();
|
||||
return $return;
|
||||
}
|
||||
|
||||
/**
|
||||
* Return documentation for a ws description object
|
||||
* ws description object can be 'external_multiple_structure', 'external_single_structure' or 'external_value'
|
||||
* ws description object can be 'external_multiple_structure', 'external_single_structure'
|
||||
* or 'external_value'
|
||||
* Example of documentation for moodle_group_create_groups function:
|
||||
list of (
|
||||
object {
|
||||
@ -219,10 +308,13 @@ class core_webservice_renderer extends plugin_renderer_base {
|
||||
if ($params->default === null) {
|
||||
$params->default = "null";
|
||||
}
|
||||
$required = html_writer::start_tag('b', array()) . get_string('default', 'webservice', $params->default) . html_writer::end_tag('b');
|
||||
$required = html_writer::start_tag('b', array()) .
|
||||
get_string('default', 'webservice', $params->default)
|
||||
. html_writer::end_tag('b');
|
||||
}
|
||||
if ($params->required == VALUE_OPTIONAL) {
|
||||
$required = html_writer::start_tag('b', array()) . get_string('optional', 'webservice') . html_writer::end_tag('b');
|
||||
$required = html_writer::start_tag('b', array()) .
|
||||
get_string('optional', 'webservice') . html_writer::end_tag('b');
|
||||
}
|
||||
$paramdesc .= " " . $required . " ";
|
||||
$paramdesc .= html_writer::start_tag('i', array());
|
||||
@ -238,7 +330,8 @@ class core_webservice_renderer extends plugin_renderer_base {
|
||||
|
||||
/// description object is a list
|
||||
if ($params instanceof external_multiple_structure) {
|
||||
return $paramdesc . "list of ( " . html_writer::empty_tag('br', array()) . $this->detailed_description_html($params->content) . ")";
|
||||
return $paramdesc . "list of ( " . html_writer::empty_tag('br', array())
|
||||
. $this->detailed_description_html($params->content) . ")";
|
||||
} else if ($params instanceof external_single_structure) {
|
||||
/// description object is an object
|
||||
$singlestructuredesc = $paramdesc . "object {" . html_writer::empty_tag('br', array());
|
||||
@ -246,7 +339,8 @@ class core_webservice_renderer extends plugin_renderer_base {
|
||||
$singlestructuredesc .= html_writer::start_tag('b', array());
|
||||
$singlestructuredesc .= $attributname;
|
||||
$singlestructuredesc .= html_writer::end_tag('b');
|
||||
$singlestructuredesc .= " " . $this->detailed_description_html($params->keys[$attributname]);
|
||||
$singlestructuredesc .= " " .
|
||||
$this->detailed_description_html($params->keys[$attributname]);
|
||||
}
|
||||
$singlestructuredesc .= "} ";
|
||||
$singlestructuredesc .= html_writer::empty_tag('br', array());
|
||||
@ -284,7 +378,8 @@ EOF;
|
||||
/// description object is a list
|
||||
if ($returndescription instanceof external_multiple_structure) {
|
||||
$return = $indentation . "<MULTIPLE>" . $brakeline;
|
||||
$return .= $this->description_in_indented_xml_format($returndescription->content, $indentation);
|
||||
$return .= $this->description_in_indented_xml_format($returndescription->content,
|
||||
$indentation);
|
||||
$return .= $indentation . "</MULTIPLE>" . $brakeline;
|
||||
return $return;
|
||||
} else if ($returndescription instanceof external_single_structure) {
|
||||
@ -292,8 +387,10 @@ EOF;
|
||||
$singlestructuredesc = $indentation . "<SINGLE>" . $brakeline;
|
||||
$keyindentation = $indentation . " ";
|
||||
foreach ($returndescription->keys as $attributname => $attribut) {
|
||||
$singlestructuredesc .= $keyindentation . "<KEY name=\"" . $attributname . "\">" . $brakeline .
|
||||
$this->description_in_indented_xml_format($returndescription->keys[$attributname], $keyindentation) .
|
||||
$singlestructuredesc .= $keyindentation . "<KEY name=\"" . $attributname . "\">"
|
||||
. $brakeline .
|
||||
$this->description_in_indented_xml_format(
|
||||
$returndescription->keys[$attributname], $keyindentation) .
|
||||
$keyindentation . "</KEY>" . $brakeline;
|
||||
}
|
||||
$singlestructuredesc .= $indentation . "</SINGLE>" . $brakeline;
|
||||
@ -343,7 +440,8 @@ EOF;
|
||||
$singlestructuredesc .= $brakeline . $keyindentation . "(";
|
||||
foreach ($paramdescription->keys as $attributname => $attribut) {
|
||||
$singlestructuredesc .= $brakeline . $keyindentation . "[" . $attributname . "] =>" .
|
||||
$this->xmlrpc_param_description_html($paramdescription->keys[$attributname], $keyindentation) .
|
||||
$this->xmlrpc_param_description_html(
|
||||
$paramdescription->keys[$attributname], $keyindentation) .
|
||||
$keyindentation;
|
||||
}
|
||||
$singlestructuredesc .= $brakeline . $keyindentation . ")";
|
||||
@ -373,8 +471,11 @@ EOF;
|
||||
* @return <type>
|
||||
*/
|
||||
public function colored_box_with_pre_tag($title, $content, $rgb = 'FEEBE5') {
|
||||
$coloredbox = html_writer::start_tag('ins', array()); //TODO: this tag removes xhtml strict error but cause warning
|
||||
$coloredbox .= html_writer::start_tag('div', array('style' => "border:solid 1px #DEDEDE;background:#" . $rgb . ";color:#222222;padding:4px;"));
|
||||
//TODO: this tag removes xhtml strict error but cause warning
|
||||
$coloredbox = html_writer::start_tag('ins', array());
|
||||
$coloredbox .= html_writer::start_tag('div',
|
||||
array('style' => "border:solid 1px #DEDEDE;background:#" . $rgb
|
||||
. ";color:#222222;padding:4px;"));
|
||||
$coloredbox .= html_writer::start_tag('pre', array());
|
||||
$coloredbox .= html_writer::start_tag('b', array());
|
||||
$coloredbox .= $title;
|
||||
@ -409,7 +510,8 @@ EOF;
|
||||
$initialparamstring = $paramstring;
|
||||
foreach ($paramdescription->keys as $attributname => $attribut) {
|
||||
$paramstring = $initialparamstring . '[' . $attributname . ']';
|
||||
$singlestructuredesc .= $this->rest_param_description_html($paramdescription->keys[$attributname], $paramstring);
|
||||
$singlestructuredesc .= $this->rest_param_description_html(
|
||||
$paramdescription->keys[$attributname], $paramstring);
|
||||
}
|
||||
return $singlestructuredesc;
|
||||
} else {
|
||||
@ -439,7 +541,6 @@ EOF;
|
||||
* @return string the html to diplay
|
||||
*/
|
||||
public function documentation_html($functions, $printableformat, $activatedprotocol, $authparams) {
|
||||
global $OUTPUT, $CFG;
|
||||
$br = html_writer::empty_tag('br', array());
|
||||
$brakeline = <<<EOF
|
||||
|
||||
@ -450,9 +551,9 @@ EOF;
|
||||
$docinfo->username = $authparams['wsusername'];
|
||||
$docurl = new moodle_url('http://docs.moodle.org/en/Development:Creating_a_web_service_client');
|
||||
$docinfo->doclink = html_writer::tag('a',
|
||||
get_string('wsclientdoc', 'webservice'), array('href' => $docurl));
|
||||
get_string('wsclientdoc', 'webservice'), array('href' => $docurl));
|
||||
$documentationhtml = html_writer::start_tag('table',
|
||||
array('style' => "margin-left:auto; margin-right:auto;"));
|
||||
array('style' => "margin-left:auto; margin-right:auto;"));
|
||||
$documentationhtml .= html_writer::start_tag('tr', array());
|
||||
$documentationhtml .= html_writer::start_tag('td', array());
|
||||
$documentationhtml .= get_string('wsdocumentationintro', 'webservice', $docinfo);
|
||||
@ -463,17 +564,19 @@ EOF;
|
||||
$authparams['print'] = true;
|
||||
//$parameters = array ('token' => $token, 'wsusername' => $username, 'wspassword' => $password, 'print' => true);
|
||||
$url = new moodle_url('/webservice/wsdoc.php', $authparams); // Required
|
||||
$documentationhtml .= $OUTPUT->single_button($url, get_string('print', 'webservice'));
|
||||
$documentationhtml .= $this->output->single_button($url, get_string('print', 'webservice'));
|
||||
$documentationhtml .= $br;
|
||||
|
||||
|
||||
/// each functions will be displayed into a collapsible region (opened if printableformat = true)
|
||||
/// each functions will be displayed into a collapsible region
|
||||
//(opened if printableformat = true)
|
||||
foreach ($functions as $functionname => $description) {
|
||||
|
||||
if (empty($printableformat)) {
|
||||
$documentationhtml .= print_collapsible_region_start('',
|
||||
'aera_' . $functionname,
|
||||
html_writer::start_tag('strong', array()) . $functionname . html_writer::end_tag('strong'),
|
||||
html_writer::start_tag('strong', array())
|
||||
. $functionname . html_writer::end_tag('strong'),
|
||||
false,
|
||||
!$printableformat,
|
||||
true);
|
||||
@ -484,7 +587,9 @@ EOF;
|
||||
|
||||
/// function global description
|
||||
$documentationhtml .= $br;
|
||||
$documentationhtml .= html_writer::start_tag('div', array('style' => 'border:solid 1px #DEDEDE;background:#E2E0E0;color:#222222;padding:4px;'));
|
||||
$documentationhtml .= html_writer::start_tag('div',
|
||||
array('style' => 'border:solid 1px #DEDEDE;background:#E2E0E0;
|
||||
color:#222222;padding:4px;'));
|
||||
$documentationhtml .= $description->description;
|
||||
$documentationhtml .= html_writer::end_tag('div');
|
||||
$documentationhtml .= $br . $br;
|
||||
@ -518,22 +623,28 @@ EOF;
|
||||
$documentationhtml .= html_writer::end_tag('b');
|
||||
$documentationhtml .= " (" . $required . ")"; // argument is required or optional ?
|
||||
$documentationhtml .= $br;
|
||||
$documentationhtml .= " " . $paramdesc->desc; // argument description
|
||||
$documentationhtml .= " "
|
||||
. $paramdesc->desc; // argument description
|
||||
$documentationhtml .= $br . $br;
|
||||
///general structure of the argument
|
||||
$documentationhtml .= $this->colored_box_with_pre_tag(get_string('generalstructure', 'webservice'),
|
||||
$documentationhtml .= $this->colored_box_with_pre_tag(
|
||||
get_string('generalstructure', 'webservice'),
|
||||
$this->detailed_description_html($paramdesc),
|
||||
'FFF1BC');
|
||||
///xml-rpc structure of the argument in PHP format
|
||||
if (!empty($activatedprotocol['xmlrpc'])) {
|
||||
$documentationhtml .= $this->colored_box_with_pre_tag(get_string('phpparam', 'webservice'),
|
||||
htmlentities('[' . $paramname . '] =>' . $this->xmlrpc_param_description_html($paramdesc)),
|
||||
$documentationhtml .= $this->colored_box_with_pre_tag(
|
||||
get_string('phpparam', 'webservice'),
|
||||
htmlentities('[' . $paramname . '] =>'
|
||||
. $this->xmlrpc_param_description_html($paramdesc)),
|
||||
'DFEEE7');
|
||||
}
|
||||
///POST format for the REST protocol for the argument
|
||||
if (!empty($activatedprotocol['rest'])) {
|
||||
$documentationhtml .= $this->colored_box_with_pre_tag(get_string('restparam', 'webservice'),
|
||||
htmlentities($this->rest_param_description_html($paramdesc, $paramname)),
|
||||
$documentationhtml .= $this->colored_box_with_pre_tag(
|
||||
get_string('restparam', 'webservice'),
|
||||
htmlentities($this->rest_param_description_html(
|
||||
$paramdesc, $paramname)),
|
||||
'FEEBE5');
|
||||
}
|
||||
$documentationhtml .= html_writer::end_tag('span');
|
||||
@ -554,21 +665,27 @@ EOF;
|
||||
}
|
||||
if (!empty($description->returns_desc)) {
|
||||
///general structure of the response
|
||||
$documentationhtml .= $this->colored_box_with_pre_tag(get_string('generalstructure', 'webservice'),
|
||||
$documentationhtml .= $this->colored_box_with_pre_tag(
|
||||
get_string('generalstructure', 'webservice'),
|
||||
$this->detailed_description_html($description->returns_desc),
|
||||
'FFF1BC');
|
||||
///xml-rpc structure of the response in PHP format
|
||||
if (!empty($activatedprotocol['xmlrpc'])) {
|
||||
$documentationhtml .= $this->colored_box_with_pre_tag(get_string('phpresponse', 'webservice'),
|
||||
htmlentities($this->xmlrpc_param_description_html($description->returns_desc)),
|
||||
$documentationhtml .= $this->colored_box_with_pre_tag(
|
||||
get_string('phpresponse', 'webservice'),
|
||||
htmlentities($this->xmlrpc_param_description_html(
|
||||
$description->returns_desc)),
|
||||
'DFEEE7');
|
||||
}
|
||||
///XML response for the REST protocol
|
||||
if (!empty($activatedprotocol['rest'])) {
|
||||
$restresponse = "<?xml version=\"1.0\" encoding=\"UTF-8\" ?>" . $brakeline . "<RESPONSE>" . $brakeline;
|
||||
$restresponse .= $this->description_in_indented_xml_format($description->returns_desc);
|
||||
$restresponse = "<?xml version=\"1.0\" encoding=\"UTF-8\" ?>"
|
||||
. $brakeline . "<RESPONSE>" . $brakeline;
|
||||
$restresponse .= $this->description_in_indented_xml_format(
|
||||
$description->returns_desc);
|
||||
$restresponse .="</RESPONSE>" . $brakeline;
|
||||
$documentationhtml .= $this->colored_box_with_pre_tag(get_string('restcode', 'webservice'),
|
||||
$documentationhtml .= $this->colored_box_with_pre_tag(
|
||||
get_string('restcode', 'webservice'),
|
||||
htmlentities($restresponse),
|
||||
'FEEBE5');
|
||||
}
|
||||
@ -591,7 +708,8 @@ EOF;
|
||||
<DEBUGINFO></DEBUGINFO>
|
||||
</EXCEPTION>
|
||||
EOF;
|
||||
$documentationhtml .= $this->colored_box_with_pre_tag(get_string('restexception', 'webservice'),
|
||||
$documentationhtml .= $this->colored_box_with_pre_tag(
|
||||
get_string('restexception', 'webservice'),
|
||||
htmlentities($restexceptiontext),
|
||||
'FEEBE5');
|
||||
|
||||
@ -617,35 +735,38 @@ EOF;
|
||||
* @return string the html to diplay
|
||||
*/
|
||||
public function login_page_html($errormessage) {
|
||||
global $CFG, $OUTPUT;
|
||||
|
||||
$br = html_writer::empty_tag('br', array());
|
||||
|
||||
$htmlloginpage = html_writer::start_tag('table', array('style' => "margin-left:auto; margin-right:auto;"));
|
||||
$htmlloginpage = html_writer::start_tag('table',
|
||||
array('style' => "margin-left:auto; margin-right:auto;"));
|
||||
$htmlloginpage .= html_writer::start_tag('tr', array());
|
||||
$htmlloginpage .= html_writer::start_tag('td', array());
|
||||
|
||||
// /// Display detailed error message when can't login
|
||||
// $htmlloginpage .= get_string('error','webservice',$errormessage);
|
||||
// $htmlloginpage .= html_writer::empty_tag('br', array());
|
||||
// $htmlloginpage .= html_writer::empty_tag('br', array());
|
||||
//login form - we cannot use moodle form as we don't have sessionkey
|
||||
$target = new moodle_url('/webservice/wsdoc.php', array()); // Required
|
||||
|
||||
$contents = get_string('entertoken', 'webservice');
|
||||
$contents .= $br . $br;
|
||||
$contents .= html_writer::empty_tag('input', array('type' => 'text', 'name' => 'token', 'style' => 'width: 30em;'));
|
||||
$contents .= html_writer::empty_tag('input',
|
||||
array('type' => 'text', 'name' => 'token', 'style' => 'width: 30em;'));
|
||||
|
||||
$contents .= $br . $br;
|
||||
$contents .= get_string('wsdocumentationlogin', 'webservice');
|
||||
$contents .= $br . $br;
|
||||
$contents .= html_writer::empty_tag('input', array('type' => 'text', 'name' => 'wsusername', 'style' => 'width: 30em;', 'value' => get_string('wsusername', 'webservice')));
|
||||
$contents .= html_writer::empty_tag('input',
|
||||
array('type' => 'text', 'name' => 'wsusername', 'style' => 'width: 30em;',
|
||||
'value' => get_string('wsusername', 'webservice')));
|
||||
$contents .= $br . $br;
|
||||
$contents .= html_writer::empty_tag('input', array('type' => 'text', 'name' => 'wspassword', 'style' => 'width: 30em;', 'value' => get_string('wspassword', 'webservice')));
|
||||
$contents .= html_writer::empty_tag('input',
|
||||
array('type' => 'text', 'name' => 'wspassword', 'style' => 'width: 30em;',
|
||||
'value' => get_string('wspassword', 'webservice')));
|
||||
$contents .= $br . $br;
|
||||
$contents .= html_writer::empty_tag('input', array('type' => 'submit', 'name' => 'submit', 'value' => get_string('wsdocumentation', 'webservice')));
|
||||
$contents .= html_writer::empty_tag('input',
|
||||
array('type' => 'submit', 'name' => 'submit',
|
||||
'value' => get_string('wsdocumentation', 'webservice')));
|
||||
|
||||
$htmlloginpage .= html_writer::tag('form', "<div>$contents</div>", array('method' => 'post', 'target' => $target));
|
||||
$htmlloginpage .= html_writer::tag('form', "<div>$contents</div>",
|
||||
array('method' => 'post', 'target' => $target));
|
||||
|
||||
$htmlloginpage .= html_writer::end_tag('td');
|
||||
$htmlloginpage .= html_writer::end_tag('tr');
|
||||
|
Loading…
x
Reference in New Issue
Block a user