Merge branch 'MDL-68845-master' of git://github.com/jleyva/moodle

This commit is contained in:
Jake Dallimore 2020-09-30 11:45:06 +08:00
commit 9d5e79dbac
6 changed files with 155 additions and 7 deletions

View File

@ -0,0 +1,99 @@
<?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 is the external method for exporting a calendar token.
*
* @package core_calendar
* @since Moodle 3.10
* @copyright 2020 Juan Leyva <juan@moodle.com>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
namespace core_calendar\external\export;
defined('MOODLE_INTERNAL') || die();
global $CFG;
require_once($CFG->libdir . '/externallib.php');
require_once($CFG->dirroot . '/calendar/lib.php');
use context_system;
use external_api;
use external_function_parameters;
use external_multiple_structure;
use external_single_structure;
use external_value;
use external_warnings;
use moodle_exception;
/**
* This is the external method for exporting a calendar token.
*
* @copyright 2020 Juan Leyva <juan@moodle.com>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class token extends external_api {
/**
* Returns description of method parameters.
*
* @return external_function_parameters.
* @since Moodle 3.10
*/
public static function execute_parameters() {
return new external_function_parameters([]);
}
/**
* Return the auth token required for exporting a calendar.
*
* @return array The access information
* @throws moodle_exception
* @since Moodle 3.10
*/
public static function execute() {
global $CFG, $USER;
$context = context_system::instance();
self::validate_context($context);
if (empty($CFG->enablecalendarexport)) {
throw new moodle_exception('Calendar export is disabled in this site.');
}
return [
'token' => calendar_get_export_token($USER),
'warnings' => [],
];
}
/**
* Returns description of method result value.
*
* @return external_description.
* @since Moodle 3.10
*/
public static function execute_returns() {
return new external_single_structure(
[
'token' => new external_value(PARAM_RAW, 'The calendar permanent access token for calendar export.'),
'warnings' => new external_warnings(),
]
);
}
}

View File

@ -141,10 +141,9 @@ $formdata = array(
$exportform = new core_calendar_export_form(null, $formdata);
$calendarurl = '';
if ($data = $exportform->get_data()) {
$password = $DB->get_record('user', array('id' => $USER->id), 'password');
$params = array();
$params['userid'] = $USER->id;
$params['authtoken'] = sha1($USER->id . (isset($password->password) ? $password->password : '') . $CFG->calendar_exportsalt);
$params['authtoken'] = calendar_get_export_token($USER);
$params['preset_what'] = $data->events['exportevents'];
$params['preset_time'] = $data->period['timeperiod'];

View File

@ -24,7 +24,7 @@ if (!$checkuserid && !$checkusername) {
}
//Check authentication token
$authuserid = !empty($userid) && $authtoken == sha1($userid . $user->password . $CFG->calendar_exportsalt);
$authuserid = !empty($userid) && $authtoken == calendar_get_export_token($user);
//allowing for fallback check of old url - MDL-27542
$authusername = !empty($username) && $authtoken == sha1($username . $user->password . $CFG->calendar_exportsalt);
if (!$authuserid && !$authusername) {
@ -44,7 +44,7 @@ $allowedwhat = ['all', 'user', 'groups', 'courses', 'categories'];
$allowedtime = ['weeknow', 'weeknext', 'monthnow', 'monthnext', 'recentupcoming', 'custom'];
if (!empty($generateurl)) {
$authtoken = sha1($user->id . $user->password . $CFG->calendar_exportsalt);
$authtoken = calendar_get_export_token($user);
$params = array();
$params['preset_what'] = $what;
$params['preset_time'] = $time;

View File

@ -3667,11 +3667,10 @@ function calendar_get_timestamp($d, $m, $y, $time = 0) {
* @return array The data for template and template name.
*/
function calendar_get_footer_options($calendar) {
global $CFG, $USER, $DB, $PAGE;
global $CFG, $USER, $PAGE;
// Generate hash for iCal link.
$rawhash = $USER->id . $DB->get_field('user', 'password', ['id' => $USER->id]) . $CFG->calendar_exportsalt;
$authtoken = sha1($rawhash);
$authtoken = calendar_get_export_token($USER);
$renderer = $PAGE->get_renderer('core_calendar');
$footer = new \core_calendar\external\footer_options_exporter($calendar, $USER->id, $authtoken);
@ -3905,3 +3904,15 @@ function calendar_internal_update_course_and_group_permission(int $courseid, con
}
}
}
/**
* Get the auth token for exporting the given user calendar.
* @param stdClass $user The user to export the calendar for
*
* @return string The export token.
*/
function calendar_get_export_token(stdClass $user): string {
global $CFG, $DB;
return sha1($user->id . $DB->get_field('user', 'password', ['id' => $user->id]) . $CFG->calendar_exportsalt);
}

View File

@ -963,4 +963,36 @@ class core_calendar_lib_testcase extends advanced_testcase {
// Viewing as someone not enrolled in a course with guest access on.
$this->assertTrue(calendar_view_event_allowed($caleventguest));
}
/**
* Test for calendar_get_export_token for current user.
*/
public function test_calendar_get_export_token_for_current_user() {
global $USER, $DB, $CFG;
$this->setAdminUser();
// Get my token.
$authtoken = calendar_get_export_token($USER);
$expected = sha1($USER->id . $DB->get_field('user', 'password', ['id' => $USER->id]) . $CFG->calendar_exportsalt);
$this->assertEquals($expected, $authtoken);
}
/**
* Test for calendar_get_export_token for another user.
*/
public function test_calendar_get_export_token_for_another_user() {
global $CFG;
// Get any user token.
$generator = $this->getDataGenerator();
$user = $generator->create_user();
// Get other user token.
$authtoken = calendar_get_export_token($user);
$expected = sha1($user->id . $user->password . $CFG->calendar_exportsalt);
$this->assertEquals($expected, $authtoken);
}
}

View File

@ -287,6 +287,13 @@ $functions = array(
'type' => 'read',
'ajax' => true,
],
'core_calendar_get_calendar_export_token' => [
'classname' => 'core_calendar\external\export\token',
'methodname' => 'execute',
'description' => 'Return the auth token required for exporting a calendar.',
'type' => 'read',
'services' => array(MOODLE_OFFICIAL_MOBILE_SERVICE),
],
'core_cohort_add_cohort_members' => array(
'classname' => 'core_cohort_external',
'methodname' => 'add_cohort_members',