Skip to content

Commit

Permalink
add restriction to site admins to smartmenu(items)
Browse files Browse the repository at this point in the history
  • Loading branch information
NJahreis committed Jul 1, 2024
1 parent 2722501 commit 3d85a3f
Show file tree
Hide file tree
Showing 8 changed files with 90 additions and 2 deletions.
10 changes: 10 additions & 0 deletions classes/form/smartmenu_edit_form.php
Original file line number Diff line number Diff line change
Expand Up @@ -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'));
Expand Down
10 changes: 10 additions & 0 deletions classes/form/smartmenu_item_edit_form.php
Original file line number Diff line number Diff line change
Expand Up @@ -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'));
Expand Down
4 changes: 3 additions & 1 deletion db/install.xml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<?xml version="1.0" encoding="UTF-8" ?>
<XMLDB PATH="theme/boost_union/db" VERSION="20200525" COMMENT="XMLDB file for Moodle theme/boost_union"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="../../../../lib/xmldb/xmldb.xsd"
xsi:noNamespaceSchemaLocation="../../../lib/xmldb/xmldb.xsd"
>
<TABLES>
<TABLE NAME="theme_boost_union_flavours" COMMENT="Flavour definitions">
Expand Down Expand Up @@ -50,6 +50,7 @@
<FIELD NAME="start_date" TYPE="int" LENGTH="18" NOTNULL="false" SEQUENCE="false"/>
<FIELD NAME="end_date" TYPE="int" LENGTH="18" NOTNULL="false" SEQUENCE="false"/>
<FIELD NAME="visible" TYPE="int" LENGTH="9" NOTNULL="true" DEFAULT="1" SEQUENCE="false"/>
<FIELD NAME="byadmin" TYPE="int" LENGTH="9" NOTNULL="true" DEFAULT="0" SEQUENCE="false"/>
</FIELDS>
<KEYS>
<KEY NAME="primary" TYPE="primary" FIELDS="id"/>
Expand Down Expand Up @@ -93,6 +94,7 @@
<FIELD NAME="end_date" TYPE="int" LENGTH="18" NOTNULL="false" SEQUENCE="false"/>
<FIELD NAME="visible" TYPE="int" LENGTH="9" NOTNULL="true" DEFAULT="1" SEQUENCE="false"/>
<FIELD NAME="timemodified" TYPE="int" LENGTH="18" NOTNULL="false" SEQUENCE="false"/>
<FIELD NAME="byadmin" TYPE="int" LENGTH="9" NOTNULL="true" DEFAULT="0" SEQUENCE="false"/>
</FIELDS>
<KEYS>
<KEY NAME="primary" TYPE="primary" FIELDS="id"/>
Expand Down
24 changes: 24 additions & 0 deletions db/upgrade.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
3 changes: 3 additions & 0 deletions lang/en/theme_boost_union.php
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -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';
Expand Down
18 changes: 18 additions & 0 deletions smartmenus/menulib.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down Expand Up @@ -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.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 "<byadmin>"
And I click on "Save changes" "button"
And I <adminshouldorshouldnot> 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 <student1shouldorshouldnot> 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
Expand Down
3 changes: 2 additions & 1 deletion version.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down

0 comments on commit 3d85a3f

Please sign in to comment.