Skip to content

Commit

Permalink
MDL-52811 course: prevent setting of 'lang' without permission
Browse files Browse the repository at this point in the history
  • Loading branch information
davosmith committed Feb 1, 2018
1 parent ecce451 commit 1433a07
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 4 deletions.
17 changes: 17 additions & 0 deletions admin/tool/uploadcourse/classes/course.php
Expand Up @@ -592,6 +592,23 @@ public function prepare() {
$coursedata['enddate'] = strtotime($coursedata['enddate']);
}

// If lang is specified, check the user is allowed to set that field.
if (!empty($coursedata['lang'])) {
if ($exists) {
$courseid = $DB->get_field('course', 'id', ['shortname' => $this->shortname]);
if (!has_capability('moodle/course:setforcedlanguage', context_course::instance($courseid))) {
$this->error('cannotforcelang', new lang_string('cannotforcelang', 'tool_uploadcourse'));
return false;
}
} else {
$catcontext = context_coursecat::instance($coursedata['category']);
if (!guess_if_creator_will_have_course_capability('moodle/course:setforcedlanguage', $catcontext)) {
$this->error('cannotforcelang', new lang_string('cannotforcelang', 'tool_uploadcourse'));
return false;
}
}
}

// Ultimate check mode vs. existence.
switch ($mode) {
case tool_uploadcourse_processor::MODE_CREATE_NEW:
Expand Down
1 change: 1 addition & 0 deletions admin/tool/uploadcourse/lang/en/tool_uploadcourse.php
Expand Up @@ -30,6 +30,7 @@
$string['allowresets_help'] = 'Whether the reset field is accepted or not.';
$string['cachedef_helper'] = 'Helper caching';
$string['cannotdeletecoursenotexist'] = 'Cannot delete a course that does not exist';
$string['cannotforcelang'] = 'No permission to force language for this course';
$string['cannotgenerateshortnameupdatemode'] = 'Cannot generate a shortname when updates are allowed';
$string['cannotreadbackupfile'] = 'Cannot read the backup file';
$string['cannotrenamecoursenotexist'] = 'Cannot rename a course that does not exist';
Expand Down
6 changes: 6 additions & 0 deletions backup/moodle2/restore_stepslib.php
Expand Up @@ -1827,6 +1827,7 @@ public function process_course($data) {
// When restoring to a new course we can set all the things except for the ID number.
$canchangeidnumber = $isnewcourse || has_capability('moodle/course:changeidnumber', $context, $userid);
$canchangesummary = $isnewcourse || has_capability('moodle/course:changesummary', $context, $userid);
$canforcelanguage = has_capability('moodle/course:setforcedlanguage', $context);

$data = (object)$data;
$data->id = $this->get_courseid();
Expand All @@ -1851,6 +1852,11 @@ public function process_course($data) {
unset($data->summaryformat);
}

// Unset lang if user can't change it.
if (!$canforcelanguage) {
unset($data->lang);
}

// Only allow the idnumber to be set if the user has permission and the idnumber is not already in use by
// another course on this site.
if (!empty($data->idnumber) && $canchangeidnumber && $this->task->is_samesite()
Expand Down
16 changes: 12 additions & 4 deletions course/externallib.php
Expand Up @@ -713,8 +713,13 @@ public static function create_courses($courses) {
require_capability('moodle/course:create', $context);

// Make sure lang is valid
if (array_key_exists('lang', $course) and empty($availablelangs[$course['lang']])) {
throw new moodle_exception('errorinvalidparam', 'webservice', '', 'lang');
if (array_key_exists('lang', $course)) {
if (empty($availablelangs[$course['lang']])) {
throw new moodle_exception('errorinvalidparam', 'webservice', '', 'lang');
}
if (!has_capability('moodle/course:setforcedlanguage', $context)) {
unset($course['lang']);
}
}

// Make sure theme is valid
Expand Down Expand Up @@ -911,8 +916,11 @@ public static function update_courses($courses) {
}

// Make sure lang is valid.
if (array_key_exists('lang', $course) && empty($availablelangs[$course['lang']])) {
throw new moodle_exception('errorinvalidparam', 'webservice', '', 'lang');
if (array_key_exists('lang', $course) && ($oldcourse->lang != $course['lang'])) {
require_capability('moodle/course:setforcedlanguage', $context);
if (empty($availablelangs[$course['lang']])) {
throw new moodle_exception('errorinvalidparam', 'webservice', '', 'lang');
}
}

// Make sure theme is valid.
Expand Down

0 comments on commit 1433a07

Please sign in to comment.