From af03513f9332bccda05ba723ac01a6373ecb0310 Mon Sep 17 00:00:00 2001 From: Jerome Mouneyrac Date: Wed, 16 Nov 2011 11:51:03 +0800 Subject: [PATCH] MDL-30268 add 'Can download files' field to service --- admin/webservice/forms.php | 5 +++ lang/en/webservice.php | 2 ++ lib/db/install.xml | 5 +-- lib/db/services.php | 3 +- lib/db/upgrade.php | 15 ++++++++ lib/upgradelib.php | 6 ++++ version.php | 2 +- webservice/externallib.php | 71 ++++++++++++++++++++++++-------------- 8 files changed, 79 insertions(+), 30 deletions(-) mode change 100644 => 100755 lib/db/install.xml diff --git a/admin/webservice/forms.php b/admin/webservice/forms.php index 377d8cfa725..75202284f36 100644 --- a/admin/webservice/forms.php +++ b/admin/webservice/forms.php @@ -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)) { diff --git a/lang/en/webservice.php b/lang/en/webservice.php index 48760276de0..77b73e1df85 100644 --- a/lang/en/webservice.php +++ b/lang/en/webservice.php @@ -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'; diff --git a/lib/db/install.xml b/lib/db/install.xml old mode 100644 new mode 100755 index 345c5c24033..aeabe86e5ed --- a/lib/db/install.xml +++ b/lib/db/install.xml @@ -1,5 +1,5 @@ - @@ -2516,7 +2516,8 @@ - + + diff --git a/lib/db/services.php b/lib/db/services.php index 73eed2ca014..f91ec49cbd2 100644 --- a/lib/db/services.php +++ b/lib/db/services.php @@ -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 ), ); diff --git a/lib/db/upgrade.php b/lib/db/upgrade.php index 83adec74439..4d24b80088b 100644 --- a/lib/db/upgrade.php +++ b/lib/db/upgrade.php @@ -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; } diff --git a/lib/upgradelib.php b/lib/upgradelib.php index d450ca98892..fadfe7c30e9 100644 --- a/lib/upgradelib.php +++ b/lib/upgradelib.php @@ -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(); diff --git a/version.php b/version.php index eb0a3297af2..19816d1a81f 100644 --- a/version.php +++ b/version.php @@ -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 diff --git a/webservice/externallib.php b/webservice/externallib.php index f1c0e32bac6..0c86f3c1b94 100644 --- a/webservice/externallib.php +++ b/webservice/externallib.php @@ -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), ) ); }