Skip to content

Commit

Permalink
MDL-50783 Ajax: Configure how to call a webservice through db/service…
Browse files Browse the repository at this point in the history
….php

Now the db/service.php array can contain these extra keys to provide information
on how a webservice may be called:

    'ajax' => true (Default is false)

Replaces the xx_is_allowed_from_ajax callback.

    'loginrequired' => false (Default is true)

Means that this webservice can be called through lib/ajax/service-nosession.php
which sets NO_MOODLE_COOKIES to true (faster). This is only safe for webservices returning
static public data (e.g. get_string).
  • Loading branch information
Damyon Wiese committed Sep 14, 2015
1 parent 5d8c198 commit ba224fb
Show file tree
Hide file tree
Showing 19 changed files with 74 additions and 97 deletions.
2 changes: 1 addition & 1 deletion admin/tool/templatelibrary/amd/build/display.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion admin/tool/templatelibrary/amd/build/search.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion admin/tool/templatelibrary/amd/src/display.js
Expand Up @@ -133,7 +133,7 @@ define(['jquery', 'core/ajax', 'core/log', 'core/notification', 'core/templates'
component: component,
template: name
}
}]);
}], true, false);

// When returns a new promise that is resolved when all the passed in promises are resolved.
// The arguments to the done become the values of each resolved promise.
Expand Down
2 changes: 1 addition & 1 deletion admin/tool/templatelibrary/amd/src/search.js
Expand Up @@ -53,7 +53,7 @@ define(['jquery', 'core/ajax', 'core/log', 'core/notification', 'core/templates'
args: { component: componentStr, search: searchStr },
done: reloadListTemplate,
fail: notification.exception }
]);
], true, false);
};

var throttle = null;
Expand Down
18 changes: 0 additions & 18 deletions admin/tool/templatelibrary/classes/external.php
Expand Up @@ -63,14 +63,6 @@ public static function list_templates_parameters() {
return new external_function_parameters($params);
}

/**
* Expose to AJAX
* @return boolean
*/
public static function list_templates_is_allowed_from_ajax() {
return true;
}

/**
* Loads the list of templates.
* @param string $component Limit the search to a component.
Expand Down Expand Up @@ -108,16 +100,6 @@ public static function load_canonical_template_parameters() {
);
}

/**
* Can this function be called directly from ajax?
*
* @return boolean
* @since Moodle 2.9
*/
public static function load_canonical_template_is_allowed_from_ajax() {
return true;
}

/**
* Return a mustache template.
* Note - this function differs from the function core_output_load_template
Expand Down
6 changes: 5 additions & 1 deletion admin/tool/templatelibrary/db/services.php
Expand Up @@ -32,12 +32,16 @@
'description' => 'List/search templates by component.',
'type' => 'read',
'capabilities'=> '',
'ajax' => true,
'loginrequired' => false,
),
'tool_templatelibrary_load_canonical_template' => array(
'classname' => 'tool_templatelibrary\external',
'methodname' => 'load_canonical_template',
'description' => 'Load a canonical template by name (not the theme overidden one).',
'type' => 'read'
'type' => 'read',
'ajax' => true,
'loginrequired' => false,
),

);
Expand Down
31 changes: 31 additions & 0 deletions lib/ajax/service-nologin.php
@@ -0,0 +1,31 @@
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.

/**
* This file is used to call any registered externallib function in Moodle.
*
* It will process more than one request and return more than one response if required.
* It is recommended to add webservice functions and re-use this script instead of
* writing any new custom ajax scripts.
*
* @since Moodle 2.9
* @package core
* @copyright 2015 Damyon Wiese
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/

define('NO_MOODLE_COOKIES', true);
require_once('service.php');
5 changes: 3 additions & 2 deletions lib/ajax/service.php
Expand Up @@ -32,8 +32,6 @@
require_once(dirname(__FILE__) . '/../../config.php');
require_once($CFG->libdir . '/externallib.php');

require_sesskey();

$rawjson = file_get_contents('php://input');

$requests = json_decode($rawjson, true);
Expand All @@ -54,6 +52,7 @@
$externalfunctioninfo = external_function_info($methodname);

if (!$externalfunctioninfo->allowed_from_ajax) {
error_log('This external function is not available to ajax. Failed to call "' . $methodname . '"');
throw new moodle_exception('servicenotavailable', 'webservice');
}

Expand All @@ -62,6 +61,8 @@
if (!isloggedin()) {
error_log('This external function is not available to public users. Failed to call "' . $methodname . '"');
throw new moodle_exception('servicenotavailable', 'webservice');
} else {
require_sesskey();
}
}

Expand Down
2 changes: 1 addition & 1 deletion lib/amd/build/ajax.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion lib/amd/build/str.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion lib/amd/build/templates.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit ba224fb

Please sign in to comment.