MDL-30268 add 'Can download files' field to service

This commit is contained in:
Jerome Mouneyrac 2011-11-16 11:51:03 +08:00
parent 6be90ce05f
commit af03513f93
8 changed files with 79 additions and 30 deletions

View File

@ -68,6 +68,11 @@ class external_service_form extends moodleform {
get_string('restrictedusers', 'webservice'));
$mform->addHelpButton('restrictedusers', 'restrictedusers', 'webservice');
//can users download files
$mform->addElement('advcheckbox', 'downloadfiles', get_string('downloadfiles', 'webservice'));
$mform->setAdvanced('downloadfiles');
$mform->addHelpButton('downloadfiles', 'downloadfiles', 'webservice');
/// needed to select automatically the 'No required capability" option
$currentcapabilityexist = false;
if (empty($service->requiredcapability)) {

View File

@ -62,6 +62,8 @@ $string['disabledwarning'] = 'All web service protocols are disabled. The "Enab
$string['doc'] = 'Documentation';
$string['docaccessrefused'] = 'You are not allowed to see the documentation for this token';
$string['documentation'] = 'web service documentation';
$string['downloadfiles'] = 'Can download files';
$string['downloadfiles_help'] = 'If enabled, any user can download files with their security keys. Of course they are restricted to the files they are allowed to download in the site.';
$string['editaservice'] = 'Edit service';
$string['editservice'] = 'Edit the service: {$a->name} (id: {$a->id})';
$string['enabled'] = 'Enabled';

5
lib/db/install.xml Normal file → Executable file
View File

@ -1,5 +1,5 @@
<?xml version="1.0" encoding="UTF-8" ?>
<XMLDB PATH="lib/db" VERSION="20111101" COMMENT="XMLDB file for core Moodle tables"
<XMLDB PATH="lib/db" VERSION="20111115" COMMENT="XMLDB file for core Moodle tables"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="../../lib/xmldb/xmldb.xsd"
>
@ -2516,7 +2516,8 @@
<FIELD NAME="component" TYPE="char" LENGTH="100" NOTNULL="false" SEQUENCE="false" PREVIOUS="restrictedusers" NEXT="timecreated"/>
<FIELD NAME="timecreated" TYPE="int" LENGTH="10" NOTNULL="true" UNSIGNED="true" SEQUENCE="false" PREVIOUS="component" NEXT="timemodified"/>
<FIELD NAME="timemodified" TYPE="int" LENGTH="10" NOTNULL="false" UNSIGNED="true" SEQUENCE="false" PREVIOUS="timecreated" NEXT="shortname"/>
<FIELD NAME="shortname" TYPE="char" LENGTH="255" NOTNULL="false" SEQUENCE="false" COMMENT="a unique shortname" PREVIOUS="timemodified"/>
<FIELD NAME="shortname" TYPE="char" LENGTH="255" NOTNULL="false" SEQUENCE="false" COMMENT="a unique shortname" PREVIOUS="timemodified" NEXT="downloadfiles"/>
<FIELD NAME="downloadfiles" TYPE="int" LENGTH="1" NOTNULL="true" UNSIGNED="true" DEFAULT="0" SEQUENCE="false" COMMENT="1 if the service allow people to download file from webservice/plugins.php - 0 if not" PREVIOUS="shortname"/>
</FIELDS>
<KEYS>
<KEY NAME="primary" TYPE="primary" FIELDS="id"/>

View File

@ -477,6 +477,7 @@ $services = array(
'moodle_message_send_instantmessages'),
'enabled' => 0,
'restrictedusers' => 0,
'shortname' => MOODLE_OFFICIAL_MOBILE_SERVICE
'shortname' => MOODLE_OFFICIAL_MOBILE_SERVICE,
'downloadfiles' => 1
),
);

View File

@ -6916,6 +6916,21 @@ FROM
upgrade_main_savepoint(true, 2011110200.02);
}
if ($oldversion < 2011111500.01) {
// Define field downloadfiles to be added to external_services
$table = new xmldb_table('external_services');
$field = new xmldb_field('downloadfiles', XMLDB_TYPE_INTEGER, '1', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, '0', 'shortname');
// Conditionally launch add field downloadfiles
if (!$dbman->field_exists($table, $field)) {
$dbman->add_field($table, $field);
}
// Main savepoint reached
upgrade_main_savepoint(true, 2011111500.01);
}
return true;
}

View File

@ -900,6 +900,7 @@ function external_update_descriptions($component) {
$service['enabled'] = empty($service['enabled']) ? 0 : $service['enabled'];
$service['requiredcapability'] = empty($service['requiredcapability']) ? null : $service['requiredcapability'];
$service['restrictedusers'] = !isset($service['restrictedusers']) ? 1 : $service['restrictedusers'];
$service['downloadfiles'] = !isset($service['downloadfiles']) ? 0 : $service['downloadfiles'];
$service['shortname'] = !isset($service['shortname']) ? null : $service['shortname'];
$update = false;
@ -911,6 +912,10 @@ function external_update_descriptions($component) {
$dbservice->restrictedusers = $service['restrictedusers'];
$update = true;
}
if ($dbservice->downloadfiles != $service['downloadfiles']) {
$dbservice->downloadfiles = $service['downloadfiles'];
$update = true;
}
//if shortname is not a PARAM_ALPHANUMEXT, fail (tested here for service update and creation)
if (isset($service['shortname']) and
(clean_param($service['shortname'], PARAM_ALPHANUMEXT) != $service['shortname'])) {
@ -964,6 +969,7 @@ function external_update_descriptions($component) {
$dbservice->enabled = empty($service['enabled']) ? 0 : $service['enabled'];
$dbservice->requiredcapability = empty($service['requiredcapability']) ? null : $service['requiredcapability'];
$dbservice->restrictedusers = !isset($service['restrictedusers']) ? 1 : $service['restrictedusers'];
$dbservice->downloadfiles = !isset($service['downloadfiles']) ? 0 : $service['downloadfiles'];
$dbservice->shortname = !isset($service['shortname']) ? null : $service['shortname'];
$dbservice->component = $component;
$dbservice->timecreated = time();

View File

@ -30,7 +30,7 @@
defined('MOODLE_INTERNAL') || die();
$version = 2011111500.00; // YYYYMMDD = weekly release date of this DEV branch
$version = 2011111500.01; // YYYYMMDD = weekly release date of this DEV branch
// RR = release increments - 00 in DEV branches
// .XX = incremental changes

View File

@ -38,8 +38,7 @@ class core_webservice_external extends external_api {
new external_value(
PARAM_ALPHANUMEXT,
'service shortname'),
'service shortnames - by default, if the list is empty and mobile web services are enabled,
we return the mobile service functions',
'DEPRECATED PARAMETER - it was a design error in the original implementation. It is ignored now. (parameter kept for backward compatibility)',
VALUE_DEFAULT,
array()
),
@ -51,11 +50,11 @@ class core_webservice_external extends external_api {
* Return user information including profile picture + basic site information
* Note:
* - no capability checking because we return just known information by logged user
* @param array $serviceshortnames of service shortnames - the functions of these services will be returned
* @param array $serviceshortnames - DEPRECATED PARAMETER - values will be ignored - it was an original design error, we keep for backward compatibility.
* @return array
*/
public function get_site_info($serviceshortnames = array()) {
global $USER, $SITE, $CFG;
global $USER, $SITE, $CFG, $DB;
$params = self::validate_parameters(self::get_site_info_parameters(),
array('serviceshortnames'=>$serviceshortnames));
@ -63,20 +62,47 @@ class core_webservice_external extends external_api {
$profileimageurl = moodle_url::make_pluginfile_url(
get_context_instance(CONTEXT_USER, $USER->id)->id, 'user', 'icon', NULL, '/', 'f1');
require_once($CFG->dirroot . "/webservice/lib.php");
$webservice = new webservice();
//site information
$siteinfo = array(
'sitename' => $SITE->fullname,
'siteurl' => $CFG->wwwroot,
'username' => $USER->username,
'firstname' => $USER->firstname,
'lastname' => $USER->lastname,
'fullname' => fullname($USER),
'userid' => $USER->id,
'userpictureurl' => $profileimageurl->out(false)
);
//If no service listed always return the mobile one by default
if (empty($params['serviceshortnames']) and $CFG->enablewebservices) {
$mobileservice = $webservice->get_external_service_by_shortname(MOODLE_OFFICIAL_MOBILE_SERVICE);
if ($mobileservice->enabled) {
$params['serviceshortnames'] = array(MOODLE_OFFICIAL_MOBILE_SERVICE); //return mobile service by default
}
//Retrieve the service and functions from the web service linked to the token
//If you call this function directly from external (not a web service call),
//then it will still return site info without information about a service
//Note: wsusername/wspassword ws authentication is not supported.
$functions = array();
if ($CFG->enablewebservices) { //no need to check token if web service are disabled and not a ws call
$token = optional_param('wstoken', '', PARAM_ALPHANUM);
if (!empty($token)) { //no need to run if not a ws call
//retrieve service shortname
$servicesql = 'SELECT s.*
FROM {external_services} s, {external_tokens} t
WHERE t.externalserviceid = s.id AND token = ? AND t.userid = ? AND s.enabled = 1';
$service = $DB->get_record_sql($servicesql, array($token, $USER->id));
$siteinfo['downloadfiles'] = $service->downloadfiles;
if (!empty($service)) {
//retrieve the functions
$functionssql = "SELECT f.*
FROM {external_functions} f, {external_services_functions} sf
WHERE f.name = sf.functionname AND sf.externalserviceid = ?";
$functions = $DB->get_records_sql($functionssql, array($service->id));
} else {
throw new coding_exception('No service found in get_site_info: something is buggy, it should have fail at the ws server authentication layer.');
}
}
}
//retrieve the functions related to the services
$functions = $webservice->get_external_functions_by_enabled_services($params['serviceshortnames']);
//built up the returned values of the list of functions
$componentversions = array();
$avalaiblefunctions = array();
@ -106,17 +132,9 @@ class core_webservice_external extends external_api {
$avalaiblefunctions[] = $functioninfo;
}
return array(
'sitename' => $SITE->fullname,
'siteurl' => $CFG->wwwroot,
'username' => $USER->username,
'firstname' => $USER->firstname,
'lastname' => $USER->lastname,
'fullname' => fullname($USER),
'userid' => $USER->id,
'userpictureurl' => $profileimageurl->out(false),
'functions' => $avalaiblefunctions
);
$siteinfo['functions'] = $avalaiblefunctions;
return $siteinfo;
}
/**
@ -141,6 +159,7 @@ class core_webservice_external extends external_api {
'version' => new external_value(PARAM_FLOAT, 'The version number of moodle site/local plugin linked to the function')
), 'functions that are available')
),
'downloadfiles' => new external_value(PARAM_INT, '1 if users are allowed to download files, 0 if not', VALUE_OPTIONAL),
)
);
}