MDL-77256 tool_uploaduser: fix get allowed roles for courses

During user upload the permission to assign roles is checked in the wrong context
This commit is contained in:
Roberto Bravo
2023-03-07 18:37:22 +01:00
parent 556208417d
commit 6c3b8ae16d
4 changed files with 260 additions and 16 deletions
+19 -8
View File
@@ -126,7 +126,6 @@ class process {
$today = make_timestamp(date('Y', $today), date('m', $today), date('d', $today), 0, 0, 0);
$this->today = $today;
$this->rolecache = uu_allowed_roles_cache(); // Course roles lookup cache.
$this->sysrolecache = uu_allowed_sysroles_cache(); // System roles lookup cache.
$this->supportedauths = uu_supported_auths(); // Officially supported plugins that are enabled.
@@ -1209,14 +1208,18 @@ class process {
}
}
if (!array_key_exists($courseid, $this->rolecache)) {
$this->rolecache[$courseid] = uu_allowed_roles_cache(null, (int)$courseid);
}
if ($courseid == SITEID) {
// Technically frontpage does not have enrolments, but only role assignments,
// let's not invent new lang strings here for this rarely used feature.
if (!empty($user->{'role'.$i})) {
$rolename = $user->{'role'.$i};
if (array_key_exists($rolename, $this->rolecache)) {
$roleid = $this->rolecache[$rolename]->id;
if (array_key_exists($rolename, $this->rolecache[$courseid]) ) {
$roleid = $this->rolecache[$courseid][$rolename]->id;
} else {
$this->upt->track('enrolments', get_string('unknownrole', 'error', s($rolename)), 'error');
continue;
@@ -1226,7 +1229,7 @@ class process {
$a = new \stdClass();
$a->course = $shortname;
$a->role = $this->rolecache[$roleid]->name;
$a->role = $this->rolecache[$courseid][$roleid]->name;
$this->upt->track('enrolments', get_string('enrolledincourserole', 'enrol_manual', $a), 'info');
}
@@ -1236,8 +1239,8 @@ class process {
$roleid = false;
if (!empty($user->{'role'.$i})) {
$rolename = $user->{'role'.$i};
if (array_key_exists($rolename, $this->rolecache)) {
$roleid = $this->rolecache[$rolename]->id;
if (array_key_exists($rolename, $this->rolecache[$courseid])) {
$roleid = $this->rolecache[$courseid][$rolename]->id;
} else {
$this->upt->track('enrolments', get_string('unknownrole', 'error', s($rolename)), 'error');
continue;
@@ -1256,7 +1259,15 @@ class process {
}
} else {
// No role specified, use the default from manual enrol plugin.
$roleid = $this->manualcache[$courseid]->roleid;
$defaultenrolroleid = (int)$this->manualcache[$courseid]->roleid;
// Validate the current user can assign this role.
if (array_key_exists($defaultenrolroleid, $this->rolecache[$courseid]) ) {
$roleid = $defaultenrolroleid;
} else {
$role = $DB->get_record('role', ['id' => $defaultenrolroleid]);
$this->upt->track('enrolments', get_string('unknownrole', 'error', s($role->shortname)), 'error');
continue;
}
}
if ($roleid) {
@@ -1299,7 +1310,7 @@ class process {
$a = new \stdClass();
$a->course = $shortname;
$a->role = $this->rolecache[$roleid]->name;
$a->role = $this->rolecache[$courseid][$roleid]->name;
$this->upt->track('enrolments', get_string('enrolledincourserole', 'enrol_manual', $a), 'info');
}
}