From 3d85a3f959288424fffe490b739b368feeba269b Mon Sep 17 00:00:00 2001 From: Nikolai Jahreis Date: Mon, 13 May 2024 15:30:39 +0000 Subject: [PATCH] add restriction to site admins to smartmenu(items) , resolves #421. --- classes/form/smartmenu_edit_form.php | 10 ++++++++ classes/form/smartmenu_item_edit_form.php | 10 ++++++++ db/install.xml | 4 +++- db/upgrade.php | 24 +++++++++++++++++++ lang/en/theme_boost_union.php | 3 +++ smartmenus/menulib.php | 18 ++++++++++++++ ..._smartmenusettings_menuitems_rules.feature | 20 ++++++++++++++++ version.php | 3 ++- 8 files changed, 90 insertions(+), 2 deletions(-) diff --git a/classes/form/smartmenu_edit_form.php b/classes/form/smartmenu_edit_form.php index c8768d6c0dc..724813191fe 100644 --- a/classes/form/smartmenu_edit_form.php +++ b/classes/form/smartmenu_edit_form.php @@ -208,6 +208,16 @@ public function definition() { $mform->setType('rolecontext', PARAM_INT); $mform->addHelpButton('rolecontext', 'smartmenusrolecontext', 'theme_boost_union'); + // Add restrict visibility by admin as header element. + $mform->addElement('header', 'restrictbyadminheader', + get_string('smartmenusrestrictbyadminheader', 'theme_boost_union')); + if (isset($this->_customdata['menu']) && $this->_customdata['menu']->byadmin) { + $mform->setExpanded('restrictbyadminheader'); + } + // Add admin restriction as advcheckbox element. + $mform->addElement('advcheckbox', 'byadmin', get_string('smartmenusbyadmin', 'theme_boost_union')); + $mform->addHelpButton('byadmin', 'smartmenusbyadmin', 'theme_boost_union'); + // Add restrict visibility by cohorts as header element. $mform->addElement('header', 'restrictbycohortsheader', get_string('smartmenusrestrictbycohortsheader', 'theme_boost_union')); diff --git a/classes/form/smartmenu_item_edit_form.php b/classes/form/smartmenu_item_edit_form.php index 804b158e13a..aafed6101c5 100644 --- a/classes/form/smartmenu_item_edit_form.php +++ b/classes/form/smartmenu_item_edit_form.php @@ -377,6 +377,16 @@ public function definition() { $mform->setType('rolecontext', PARAM_INT); $mform->addHelpButton('rolecontext', 'smartmenusrolecontext', 'theme_boost_union'); + // Add restrict visibility by admin as header element. + $mform->addElement('header', 'restrictbyadminheader', + get_string('smartmenusrestrictbyadminheader', 'theme_boost_union')); + if (isset($this->_customdata['menuitem']) && $this->_customdata['menuitem']->byadmin) { + $mform->setExpanded('restrictbyadminheader'); + } + // Add admin restriction as advcheckbox element. + $mform->addElement('advcheckbox', 'byadmin', get_string('smartmenusbyadmin', 'theme_boost_union')); + $mform->addHelpButton('byadmin', 'smartmenusbyadmin', 'theme_boost_union'); + // Add restrict visibility by cohorts as header element. $mform->addElement('header', 'restrictbycohortsheader', get_string('smartmenusrestrictbycohortsheader', 'theme_boost_union')); diff --git a/db/install.xml b/db/install.xml index 7d865e8dc9b..a1bb583694c 100644 --- a/db/install.xml +++ b/db/install.xml @@ -1,7 +1,7 @@ @@ -50,6 +50,7 @@ + @@ -93,6 +94,7 @@ + diff --git a/db/upgrade.php b/db/upgrade.php index 00f8c2593b7..45423bc195f 100644 --- a/db/upgrade.php +++ b/db/upgrade.php @@ -324,5 +324,29 @@ function xmldb_theme_boost_union_upgrade($oldversion) { upgrade_plugin_savepoint(true, 2023102027, 'theme', 'boost_union'); } + if ($oldversion < 2023102038) { + + // Define field byadmin to be added to theme_boost_union_menus. + $table = new xmldb_table('theme_boost_union_menus'); + $field = new xmldb_field('byadmin', XMLDB_TYPE_INTEGER, '9', null, XMLDB_NOTNULL, null, '0', 'visible'); + + // Conditionally launch add field byadmin. + if (!$dbman->field_exists($table, $field)) { + $dbman->add_field($table, $field); + } + + // Define field byadmin to be added to theme_boost_union_menuitems. + $table = new xmldb_table('theme_boost_union_menuitems'); + $field = new xmldb_field('byadmin', XMLDB_TYPE_INTEGER, '9', null, XMLDB_NOTNULL, null, '0', 'timemodified'); + + // Conditionally launch add field byadmin. + if (!$dbman->field_exists($table, $field)) { + $dbman->add_field($table, $field); + } + + // Boost_union savepoint reached. + upgrade_plugin_savepoint(true, 2023102038, 'theme', 'boost_union'); + } + return true; } diff --git a/lang/en/theme_boost_union.php b/lang/en/theme_boost_union.php index 36c8caeb48b..1087e9a813e 100644 --- a/lang/en/theme_boost_union.php +++ b/lang/en/theme_boost_union.php @@ -1082,6 +1082,8 @@ $string['smartmenusbylanguage_help'] = 'Restrict the visibility based on the user\'s language'; $string['smartmenusbyrole'] = 'By role'; $string['smartmenusbyrole_help'] = 'Restrict the visibility based on the user\'s roles.'; +$string['smartmenusbyadmin'] = 'Only for site admin'; +$string['smartmenusbyadmin_help'] = 'Restrict the visibility to site admin users.'; $string['smartmenusdynamiccoursescompletionstatus'] = 'Completion status'; $string['smartmenusdynamiccoursescompletionstatus_help'] = 'The dynamic courses menu item list will contain all courses of the user which match the selected completion status. For example, if you select \'In progress\' as the completion status, the dynamic courses menu item list will only contain courses that the current user is currently working on.'; $string['smartmenusdynamiccoursescompletionstatuscompleted'] = 'Completed'; @@ -1239,6 +1241,7 @@ $string['smartmenusrestrictbydateheader'] = 'Restrict visibility by date'; $string['smartmenusrestrictbylanguageheader'] = 'Restrict visibility by language'; $string['smartmenusrestrictbyrolesheader'] = 'Restrict visibility by roles'; +$string['smartmenusrestrictbyadminheader'] = 'Restrict visibility to admins'; $string['smartmenusrolecontext'] = 'Context'; $string['smartmenusrolecontext_help'] = 'Select the context for which the user\'s role should be checked (Any context or system context only)'; $string['smartmenussavechangesandconfigure'] = 'Save and configure items'; diff --git a/smartmenus/menulib.php b/smartmenus/menulib.php index 3eef818e154..20512088e5e 100644 --- a/smartmenus/menulib.php +++ b/smartmenus/menulib.php @@ -97,6 +97,11 @@ public function verify_access_restrictions() { // Restriction by roles. $this->restriction_byroles($query); + // Restricted to admins. + if (!$this->restriction_byadminrole()) { + return false; + } + // Restriction by cohorts. $this->restriction_bycohorts($query); @@ -180,6 +185,19 @@ public function restriction_byroles(&$query) { $query->params += array_merge($params, $inparam); } + /** + * Verify if the menu is restricted to admins. + * + * @return bool True if the user is a site admin + */ + public function restriction_byadminrole() { + // Check if the item is restricted to admins. + if ($this->data->byadmin) { + return is_siteadmin($this->userid); // Returns true if the user is a site admin. + } + return true; + } + /** * The purpose of this function is to check if a user is assigned to one or more cohorts that are specified in a menu. * For the operator "ALL" it gets the count of records and verfiy the records count is same as count of selected cohorts. diff --git a/tests/behat/theme_boost_union_smartmenusettings_menuitems_rules.feature b/tests/behat/theme_boost_union_smartmenusettings_menuitems_rules.feature index 9abd82f8a89..239ca4ba80f 100644 --- a/tests/behat/theme_boost_union_smartmenusettings_menuitems_rules.feature +++ b/tests/behat/theme_boost_union_smartmenusettings_menuitems_rules.feature @@ -101,6 +101,26 @@ Feature: Configuring the theme_boost_union plugin on the "Smart menus" page, app | 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 being site admin + When I navigate to smart menus + And I should see "Quick links" in the "smartmenues" "table" + And I should see smart menu "Quick links" item "Resources" in location "Main, Menu, User, Bottom" + And I click on ".action-list-items" "css_element" in the "Quick links" "table_row" + And I click on ".action-edit" "css_element" in the "Resources" "table_row" + And I expand all fieldsets + And I set the field "Only for site admin" to "" + And I click on "Save changes" "button" + And I see smart menu "Quick links" item "Resources" in location "Main, Menu, User, Bottom" + And I log out + And I log in as "student1" + Then I see smart menu "Quick links" item "Resources" in location "Main, Menu, User, Bottom" + + Examples: + | byadmin | adminshouldorshouldnot | student1shouldorshouldnot | + | 1 | should | should not | + | 0 | should | should not | + @javascript Scenario Outline: Smartmenu: Menu items: Rules - Show smart menu item based on the user assignment in single cohorts When I navigate to smart menus diff --git a/version.php b/version.php index ce64003aa83..7afb525a827 100644 --- a/version.php +++ b/version.php @@ -25,8 +25,9 @@ defined('MOODLE_INTERNAL') || die(); $plugin->component = 'theme_boost_union'; -$plugin->version = 2023102041; +$plugin->version = 2023102042; $plugin->release = 'v4.3-r14'; +>>>>>>> 3939234 (add restriction to site admins to smartmenu(items)) $plugin->requires = 2023100900; $plugin->supported = [403, 403]; $plugin->maturity = MATURITY_STABLE;