Skip to content

Commit

Permalink
MDL-69448 backup: fix capability checks when unable to copy user data.
Browse files Browse the repository at this point in the history
When a given user doesn't have the capability to "Include user data"
during course copying, freeze the form element rather than not adding
it at all.

This caused problems as the element was required before preceding with
the course copy.
  • Loading branch information
paulholden committed Sep 4, 2020
1 parent 99777d9 commit 404eabc
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 9 deletions.
4 changes: 2 additions & 2 deletions backup/util/ui/classes/copy/copy.php
Expand Up @@ -111,8 +111,8 @@ private final function get_enrollment_roles(\stdClass $formdata): array {
* Take the validated form data and extract the required information for copy operations.
*
* @param \stdClass $formdata Data from the validated course copy form.
* @throws \moodle_exception
* @return \stdClass $copydata Data required for course copy operations.
* @throws \moodle_exception If one of the required copy fields is missing
*/
private final function get_copy_data(\stdClass $formdata): \stdClass {
$copydata = new \stdClass();
Expand All @@ -121,7 +121,7 @@ private final function get_copy_data(\stdClass $formdata): \stdClass {
if (isset($formdata->{$field})) {
$copydata->{$field} = $formdata->{$field};
} else {
throw new \moodle_exception('copy_class_field_not_found');
throw new \moodle_exception('copyfieldnotfound', 'backup', '', null, $field);
}
}

Expand Down
14 changes: 8 additions & 6 deletions backup/util/ui/classes/output/copy_form.php
Expand Up @@ -162,15 +162,17 @@ public function definition() {
}

// Keep source course user data.
$mform->addElement('select', 'userdata', get_string('userdata', 'backup'),
[0 => get_string('no'), 1 => get_string('yes')]);
$mform->setDefault('userdata', 0);
$mform->addHelpButton('userdata', 'userdata', 'backup');

$requiredcapabilities = array(
'moodle/restore:createuser', 'moodle/backup:userinfo', 'moodle/restore:userinfo'
);
if (has_all_capabilities($requiredcapabilities, $coursecontext)) {
$dataarray = array();
$dataarray[] = $mform->createElement('advcheckbox', 'userdata',
get_string('enable'), '', array('group' => 1), array(0, 1));
$mform->addGroup($dataarray, 'dataarray', get_string('userdata', 'backup'), ' ', false);
$mform->addHelpButton('dataarray', 'userdata', 'backup');
if (!has_all_capabilities($requiredcapabilities, $coursecontext)) {
$mform->hardFreeze('userdata');
$mform->setConstants('userdata', 0);
}

// Keep manual enrolments.
Expand Down
1 change: 1 addition & 0 deletions lang/en/backup.php
Expand Up @@ -169,6 +169,7 @@
$string['copydest'] = 'Destination';
$string['copyingcourse'] = 'Course copying in progress';
$string['copyingcourseshortname'] = 'copying';
$string['copyfieldnotfound'] = 'A required field was not found';
$string['copyformfail'] = 'AJAX submission of course copy form has failed.';
$string['copyop'] = 'Current operation';
$string['copyprogressheading'] = 'Course copies in progress';
Expand Down
4 changes: 3 additions & 1 deletion lib/classes/task/asynchronous_copy_task.php
Expand Up @@ -72,14 +72,16 @@ public function execute() {
$keepuserdata = (bool)$copyinfo->userdata;
$keptroles = $copyinfo->keptroles;

$backupplan->get_setting('users')->set_value('1');
$bc->set_kept_roles($keptroles);

// If we are not keeping user data don't include users or data in the backup.
// In this case we'll add the user enrolments at the end.
// Also if we have no roles to keep don't backup users.
if (empty($keptroles) || !$keepuserdata) {
$backupplan->get_setting('users')->set_status(\backup_setting::NOT_LOCKED);
$backupplan->get_setting('users')->set_value('0');
} else {
$backupplan->get_setting('users')->set_value('1');
}

// Do some preflight checks on the backup.
Expand Down

0 comments on commit 404eabc

Please sign in to comment.