From f1e320272f7811648ebfe282fe1e562bb8916574 Mon Sep 17 00:00:00 2001 From: Matt Porritt Date: Fri, 7 Jul 2023 16:31:05 +1000 Subject: [PATCH] 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. --- my/lib.php | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/my/lib.php b/my/lib.php index 1ed7676ceed..b20b1652a06 100644 --- a/my/lib.php +++ b/my/lib.php @@ -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.