diff --git a/CHANGES.md b/CHANGES.md index 1d6ec9600c8..a68bbca9024 100644 --- a/CHANGES.md +++ b/CHANGES.md @@ -6,6 +6,7 @@ Changes ### Unreleased +* 2024-05-11 - Improvement: Enhance smart menu restrictions for authenticated user role, guest roles and visitor role, resolves #571 * 2024-05-11 - Improvement: Smart menu "locations" must be filled with a value, resolves #404 * 2024-05-10 - Bugfix: Do not show empty smart menus to users, resolves #405 * 2024-05-09 - Bugfix: Smart menu menubar overlaid course index, resolves #607 diff --git a/classes/form/smartmenu_edit_form.php b/classes/form/smartmenu_edit_form.php index becdd8de8c2..c8768d6c0dc 100644 --- a/classes/form/smartmenu_edit_form.php +++ b/classes/form/smartmenu_edit_form.php @@ -189,7 +189,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..3eef818e154 100644 --- a/smartmenus/menulib.php +++ b/smartmenus/menulib.php @@ -137,11 +137,33 @@ 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. + // If no role restrictions are set. if ($roles == '' || empty($roles)) { + // Return directly. + return true; + } + + // If the user is logged in and the default user role is allowed to view the menu. + $defaultuserroleid = isset($CFG->defaultuserroleid) ? $CFG->defaultuserroleid : 0; + if ($defaultuserroleid && in_array($defaultuserroleid, $roles) && !empty($this->userid) && !isguestuser($this->userid)) { + // Return directly. + return true; + } + + // If the user is a guest and the guest role is allowed to view the menu. + $guestroleid = isset($CFG->guestroleid) ? $CFG->guestroleid : 0; + if ($guestroleid && in_array($guestroleid, $roles) && isguestuser()) { + // Return directly. + return true; + } + + // If the user is a visitor and the visitor role is allowed to view the menu. + $visitorroleid = isset($CFG->notloggedinroleid) ? $CFG->notloggedinroleid : 0; + if ($visitorroleid && in_array($visitorroleid, $roles) && !isloggedin() && !isguestuser()) { + // Return directly. return true; } @@ -156,7 +178,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 ee2c62576c7..9abd82f8a89 100644 --- a/tests/behat/theme_boost_union_smartmenusettings_menuitems_rules.feature +++ b/tests/behat/theme_boost_union_smartmenusettings_menuitems_rules.feature @@ -58,6 +58,12 @@ Feature: Configuring the theme_boost_union plugin on the "Smart menus" page, app And the following "system role assigns" exist: | user | course | role | | systemmanager | Acceptance test site | manager | + And the following "roles" exist: + | name | shortname | description | + | Visitor | visitor | My visitor role | + And I navigate to "Users > Permissions > User policies" in site administration + And I set the field "Role for visitors" to "Visitor (visitor)" + And I press "Save changes" When I navigate to smart menus And I should see "Quick links" in the "smartmenus" "table" And I should see smart menu "Quick links" item "Resources" in location "Main, Menu, User, Bottom" @@ -67,7 +73,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" @@ -79,14 +85,21 @@ 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 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 | visitorshouldorshouldnot | + | Manager | Any | should not | should not | should | should not | should not | should | should not | + | Manager, Student | Any | should | should not | should | should not | should not | should | should not | + | Manager, Student, Teacher | Any | should | should | should | should not | should not | should | should not | + | Manager, Student, Teacher | System | should not | should not | should not | should not | should not | should | should not | + | Authenticated user | Any | should | should | should | should not | should | should | should not | + | Guest | Any | should not | should not | should not | should | should not | should not | should not | + | Visitor | Any | should not | should not | should not | should not | should not | should not | should | @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..30dc4a17b88 100644 --- a/tests/behat/theme_boost_union_smartmenusettings_menus_rules.feature +++ b/tests/behat/theme_boost_union_smartmenusettings_menus_rules.feature @@ -53,6 +53,12 @@ Feature: Configuring the theme_boost_union plugin on the "Smart menus" page, app And the following "system role assigns" exist: | user | course | role | | systemmanager | Acceptance test site | manager | + And the following "roles" exist: + | name | shortname | description | + | Visitor | visitor | My visitor role | + And I navigate to "Users > Permissions > User policies" in site administration + And I set the field "Role for visitors" to "Visitor (visitor)" + And I press "Save changes" When I navigate to smart menus And I should see "Quick links" in the "smartmenus" "table" And I should see smart menu "Quick links" in location "Main, Menu, User, Bottom" @@ -61,7 +67,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 +79,22 @@ 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 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 | visitorshouldorshouldnot | + | Manager | Any | should not | should not | should | should not | should not | should | should not | + | Manager, Student | Any | should | should not | should | should not | should not | should | should not | + | Manager, Student, Teacher | Any | should | should | should | should not | should not | should | should not | + | Manager, Student, Teacher | System | should not | should not | should not | should not | should not | should | should not | + | Authenticated user | Any | should | should | should | should not | should | should | should not | + | Guest | Any | should not | should not | should not | should | should not | should not | should not | + | Visitor | Any | should not | should not | should not | should not | should not | should not | should | @javascript Scenario Outline: Smartmenu: Menus: Rules - Show smart menu based on the user assignment in single cohorts