Skip to content

Commit

Permalink
MDL-56310 restore: Confirm user has permission to change capabilities
Browse files Browse the repository at this point in the history
  • Loading branch information
Peter Dias authored and Jenkins committed Oct 30, 2020
1 parent 8aa5030 commit 086d433
Showing 1 changed file with 19 additions and 4 deletions.
23 changes: 19 additions & 4 deletions backup/moodle2/restore_stepslib.php
Original file line number Diff line number Diff line change
Expand Up @@ -2110,14 +2110,29 @@ public function process_override($data) {
$data = (object)$data;

// Check roleid is one of the mapped ones
$newroleid = $this->get_mappingid('role', $data->roleid);
$newrole = $this->get_mapping('role', $data->roleid);
$newroleid = $newrole->newitemid ?? false;
$userid = $this->task->get_userid();

// If newroleid and context are valid assign it via API (it handles dupes and so on)
if ($newroleid && $this->task->get_contextid()) {
if (!get_capability_info($data->capability)) {
if (!$capability = get_capability_info($data->capability)) {
$this->log("Capability '{$data->capability}' was not found!", backup::LOG_WARNING);
} else {
// TODO: assign_capability() needs one userid param to be able to specify our restore userid.
assign_capability($data->capability, $data->permission, $newroleid, $this->task->get_contextid());
$context = context::instance_by_id($this->task->get_contextid());
$overrideableroles = get_overridable_roles($context, ROLENAME_SHORT);
$safecapability = is_safe_capability($capability);

// Check if the new role is an overrideable role AND if the user performing the restore has the
// capability to assign the capability.
if (in_array($newrole->info['shortname'], $overrideableroles) &&
($safecapability && has_capability('moodle/role:safeoverride', $context, $userid) ||
!$safecapability && has_capability('moodle/role:override', $context, $userid))
) {
assign_capability($data->capability, $data->permission, $newroleid, $this->task->get_contextid());
} else {
$this->log("Insufficient capability to assign capability '{$data->capability}' to role!", backup::LOG_WARNING);
}
}
}
}
Expand Down

0 comments on commit 086d433

Please sign in to comment.