MDL-78340 Blocks: Dashboards now respect block permission overrides

Patch makes user dashboard respect permission overrides
that have been set on individual blocks on the system
dashboard (indexsys.php). When a user dashboard is created
either when the user visits their dashboard for the first
time or after an admin reset. When blcoks are copied to
the new dashbaord overriden permissions are also copied.
This commit is contained in:
Matt Porritt 2023-07-07 16:31:05 +10:00 committed by Jenkins
parent b0d1cef0e0
commit f1e320272f

View File

@ -109,9 +109,11 @@ function my_copy_page(
$blockinstances = $DB->get_records('block_instances', array('parentcontextid' => $systemcontext->id,
'pagetypepattern' => $pagetype,
'subpagepattern' => $systempage->id));
$roles = get_all_roles();
$newblockinstanceids = [];
foreach ($blockinstances as $instance) {
$originalid = $instance->id;
$originalcontext = context_block::instance($originalid);
unset($instance->id);
$instance->parentcontextid = $usercontext->id;
$instance->subpagepattern = $page->id;
@ -126,6 +128,22 @@ function my_copy_page(
instance: $originalid to new block instance: $instance->id for
block: $instance->blockname", DEBUG_DEVELOPER);
}
// Check if there are any overrides on this block instance.
// We check against all roles, not just roles assigned to the user.
// This is so any overrides that are applied to the system default page
// will be applied to the user's page as well, even if their role assignment changes in the future.
foreach ($roles as $role) {
$rolecapabilities = get_capabilities_from_role_on_context($role, $originalcontext);
// If there are overrides, then apply them to the new block instance.
foreach ($rolecapabilities as $rolecapability) {
role_change_permission(
$rolecapability->roleid,
$blockcontext,
$rolecapability->capability,
$rolecapability->permission
);
}
}
}
// Clone block position overrides.