From e68c8405405f8d059818c32f1f248988a0440159 Mon Sep 17 00:00:00 2001 From: Prasanna LMSACE Date: Thu, 25 Apr 2024 18:47:12 +0530 Subject: [PATCH] Improvement: Enhance smart menu restrictions for authenticated and guest roles, resolves #571 --- classes/form/smartmenu_edit_form.php | 4 +++- classes/form/smartmenu_item_edit_form.php | 4 +++- smartmenus/menulib.php | 18 ++++++++++++++-- ..._smartmenusettings_menuitems_rules.feature | 20 +++++++++++------- ...nion_smartmenusettings_menus_rules.feature | 21 ++++++++++++------- 5 files changed, 49 insertions(+), 18 deletions(-) diff --git a/classes/form/smartmenu_edit_form.php b/classes/form/smartmenu_edit_form.php index 58cc884a133..ceed93ef728 100644 --- a/classes/form/smartmenu_edit_form.php +++ b/classes/form/smartmenu_edit_form.php @@ -188,7 +188,9 @@ public function definition() { $rolelist = role_get_names(\context_system::instance()); $roleoptions = []; foreach ($rolelist as $role) { - $roleoptions[$role->id] = $role->localname; + if ($role->archetype !== 'frontpage') { // Frontpage roles are not supported in the menus restriction. + $roleoptions[$role->id] = $role->localname; + } } $byroleswidget = $mform->addElement('autocomplete', 'roles', get_string('smartmenusbyrole', 'theme_boost_union'), $roleoptions); diff --git a/classes/form/smartmenu_item_edit_form.php b/classes/form/smartmenu_item_edit_form.php index cfe0083258c..804b158e13a 100644 --- a/classes/form/smartmenu_item_edit_form.php +++ b/classes/form/smartmenu_item_edit_form.php @@ -358,7 +358,9 @@ public function definition() { $rolelist = role_get_names(\context_system::instance()); $roleoptions = []; foreach ($rolelist as $role) { - $roleoptions[$role->id] = $role->localname; + if ($role->archetype !== 'frontpage') { // Frontpage roles are not supported in the items restriction. + $roleoptions[$role->id] = $role->localname; + } } $byroleswidget = $mform->addElement('autocomplete', 'roles', get_string('smartmenusbyrole', 'theme_boost_union'), $roleoptions); diff --git a/smartmenus/menulib.php b/smartmenus/menulib.php index 2f445af4bba..51c79884751 100644 --- a/smartmenus/menulib.php +++ b/smartmenus/menulib.php @@ -137,7 +137,7 @@ public function verify_access_restrictions() { * @return void */ public function restriction_byroles(&$query) { - global $DB; + global $DB, $CFG; $roles = $this->data->roles; // Roles not mentioned then stop the role check. @@ -145,6 +145,21 @@ public function restriction_byroles(&$query) { return true; } + // Verify the default user role is set to view the menus. + $defaultuserroleid = isset($CFG->defaultuserroleid) ? $CFG->defaultuserroleid : 0; + if ($defaultuserroleid && in_array($defaultuserroleid, $roles) && !empty($this->userid) && !isguestuser($this->userid)) { + return true; + } + + // Verify the guest user have any menus or items to view. + if (isguestuser()) { + $guestroles = get_archetype_roles('guest'); + $guestroleid = array_column($guestroles, 'id'); + if (array_intersect($guestroleid, $roles)) { + return true; + } + } + list($insql, $inparam) = $DB->get_in_or_equal($roles, SQL_PARAMS_NAMED, 'rl'); $contextsql = ($this->data->rolecontext == smartmenu::SYSTEMCONTEXT) @@ -156,7 +171,6 @@ public function restriction_byroles(&$query) { 'systemcontext' => context_system::instance()->id, ]; $query->params += array_merge($params, $inparam); - } /** diff --git a/tests/behat/theme_boost_union_smartmenusettings_menuitems_rules.feature b/tests/behat/theme_boost_union_smartmenusettings_menuitems_rules.feature index 7dab82b6642..64296e4af2f 100644 --- a/tests/behat/theme_boost_union_smartmenusettings_menuitems_rules.feature +++ b/tests/behat/theme_boost_union_smartmenusettings_menuitems_rules.feature @@ -62,7 +62,7 @@ Feature: Configuring the theme_boost_union plugin on the "Smart menus" page, app And I set the field "By role" to "" And I set the field "Context" to "" And I click on "Save changes" "button" - And I should not see smart menu "Quick links" item "Resources" in location "Main, Menu, User, Bottom" + And I see smart menu "Quick links" item "Resources" in location "Main, Menu, User, Bottom" And I log out And I log in as "coursemanager" Then I see smart menu "Quick links" item "Resources" in location "Main, Menu, User, Bottom" @@ -74,14 +74,20 @@ Feature: Configuring the theme_boost_union plugin on the "Smart menus" page, app Then I see smart menu "Quick links" item "Resources" in location "Main, Menu, User, Bottom" And I log out And I log in as "systemmanager" - Then I should see smart menu "Quick links" item "Resources" in location "Main, Menu, User, Bottom" + Then I see smart menu "Quick links" item "Resources" in location "Main, Menu, User, Bottom" + And I log in as "guest" + Then I see smart menu "Quick links" item "Resources" in location "Main, Menu, Bottom" + And I log out + And I should not see smart menu "Quick links" item "Resources" in location "Main, Menu, Bottom" Examples: - | byrole | context | student1shouldorshouldnot | teachershouldorshouldnot | managershouldorshouldnot | - | Manager | Any | should not | should not | should | - | Manager, Student | Any | should | should not | should | - | Manager, Student, Teacher | Any | should | should | should | - | Manager, Student, Teacher | System | should not | should not | should not | + | byrole | context | student1shouldorshouldnot | teachershouldorshouldnot | managershouldorshouldnot | guestshouldorshouldnot | adminshouldorshouldnot | systemshouldorshouldnot | + | Manager | Any | should not | should not | should | should not | should not | should | + | Manager, Student | Any | should | should not | should | should not | should not | should | + | Manager, Student, Teacher | Any | should | should | should | should not | should not | should | + | Manager, Student, Teacher | System | should not | should not | should not | should not | should not | should | + | Authenticated user | Any | should | should | should | should not | should | should | + | Guest | Any | should not | should not | should not | should | should not | should not | @javascript Scenario Outline: Smartmenu: Menu items: Rules - Show smart menu item based on the user assignment in single cohorts diff --git a/tests/behat/theme_boost_union_smartmenusettings_menus_rules.feature b/tests/behat/theme_boost_union_smartmenusettings_menus_rules.feature index 2d15bc677de..eb8607c8cfd 100644 --- a/tests/behat/theme_boost_union_smartmenusettings_menus_rules.feature +++ b/tests/behat/theme_boost_union_smartmenusettings_menus_rules.feature @@ -61,7 +61,7 @@ Feature: Configuring the theme_boost_union plugin on the "Smart menus" page, app And I set the field "By role" to "" And I set the field "Context" to "" And I click on "Save and return" "button" - And I should not see smart menu "Quick links" in location "Main, Menu, User, Bottom" + And I see smart menu "Quick links" in location "Main, Menu, User, Bottom" And I log out And I log in as "coursemanager" Then I see smart menu "Quick links" in location "Main, Menu, User, Bottom" @@ -73,14 +73,21 @@ Feature: Configuring the theme_boost_union plugin on the "Smart menus" page, app Then I see smart menu "Quick links" in location "Main, Menu, User, Bottom" And I log out And I log in as "systemmanager" - Then I should see smart menu "Quick links" in location "Main, Menu, User, Bottom" + Then I see smart menu "Quick links" in location "Main, Menu, User, Bottom" + And I log out + And I log in as "guest" + Then I see smart menu "Quick links" in location "Main, Menu, Bottom" + And I log out + And I should not see smart menu "Quick links" in location "Main, Menu, Bottom" Examples: - | byrole | context | student1shouldorshouldnot | teachershouldorshouldnot | managershouldorshouldnot | - | Manager | Any | should not | should not | should | - | Manager, Student | Any | should | should not | should | - | Manager, Student, Teacher | Any | should | should | should | - | Manager, Student, Teacher | System | should not | should not | should not | + | byrole | context | student1shouldorshouldnot | teachershouldorshouldnot | managershouldorshouldnot | guestshouldorshouldnot | adminshouldorshouldnot | systemshouldorshouldnot | + | Manager | Any | should not | should not | should | should not | should not | should | + | Manager, Student | Any | should | should not | should | should not | should not | should | + | Manager, Student, Teacher | Any | should | should | should | should not | should not | should | + | Manager, Student, Teacher | System | should not | should not | should not | should not | should not | should | + | Authenticated user | Any | should | should | should | should not | should | should | + | Guest | Any | should not | should not | should not | should | should not | should not | @javascript Scenario Outline: Smartmenu: Menus: Rules - Show smart menu based on the user assignment in single cohorts