Skip to content

Commit

Permalink
Improvement: Add restriction for site admins to smartmenu(item), reso…
Browse files Browse the repository at this point in the history
…lves #421 . (#656)
  • Loading branch information
NJahreis authored and abias committed Nov 19, 2024
1 parent 7cde736 commit 7154d81
Show file tree
Hide file tree
Showing 12 changed files with 154 additions and 1 deletion.
1 change: 1 addition & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ Changes

### Unreleased

* 2024-11-18 - Improvement: Add the possibility to restrict smart menus and smart menu items to site admins and non-site admins only, resolves #421.
* 2024-11-18 - Bugfix: Footer displacement on pages with minimal content, resolves #655.
* 2024-11-18 - Upstream change: Adopt changes from MDL-77732 ('Custom menu items do not receive active behaviour'), resolves #436 #620 #384 #715.
* 2024-11-13 - Upstream change: Adopt changes from MDL-78999 ('Site logo does not appear in mobile view'), resolves #753.
Expand Down
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -854,6 +854,7 @@ Moodle an Hochschulen e.V. would like to thank these main contributors (in alpha
* Solent University, Mark Sharp: Code
* ssystems GmbH, Alexander Bias: Code, Peer Review, Ideating, Funding
* Technische Universität Berlin, Lars Bonczek: Code
* University of Bayreuth, Nikolai Jahreis: Code
* University of Graz, André Menrath: Code
* University of Lübeck, Christian Wolters: Code, Peer Review, Ideating
* Zurich University of Applied Sciences (ZHAW): Funding, Ideating
Expand Down
18 changes: 18 additions & 0 deletions classes/form/smartmenu_edit_form.php
Original file line number Diff line number Diff line change
Expand Up @@ -208,6 +208,24 @@ 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 restriction as select element.
$rolecontext = [
smartmenu::BYADMIN_ALL => get_string('smartmenusbyadmin_all', 'theme_boost_union'),
smartmenu::BYADMIN_ADMINS => get_string('smartmenusbyadmin_admins', 'theme_boost_union'),
smartmenu::BYADMIN_NONADMINS => get_string('smartmenusbyadmin_nonadmins', 'theme_boost_union'),
];
$mform->addElement('select', 'byadmin', get_string('smartmenusbyadmin', 'theme_boost_union'), $rolecontext);
$mform->setDefault('byadmin', smartmenu::BYADMIN_ALL);
$mform->setType('byadmin', PARAM_INT);
$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
18 changes: 18 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,24 @@ 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 restriction as select element.
$rolecontext = [
smartmenu::BYADMIN_ALL => get_string('smartmenusbyadmin_all', 'theme_boost_union'),
smartmenu::BYADMIN_ADMINS => get_string('smartmenusbyadmin_admins', 'theme_boost_union'),
smartmenu::BYADMIN_NONADMINS => get_string('smartmenusbyadmin_nonadmins', 'theme_boost_union'),
];
$mform->addElement('select', 'byadmin', get_string('smartmenusbyadmin', 'theme_boost_union'), $rolecontext);
$mform->setDefault('byadmin', smartmenu::BYADMIN_ALL);
$mform->setType('byadmin', PARAM_INT);
$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
18 changes: 18 additions & 0 deletions classes/smartmenu.php
Original file line number Diff line number Diff line change
Expand Up @@ -259,6 +259,24 @@ class smartmenu {
*/
public const MODE_SUBMENU = 1;

/**
* Restrict to admins: Show to all users.
* @var int
*/
public const BYADMIN_ALL = 0;

/**
* Restrict to admins: Show only to admins.
* @var int
*/
public const BYADMIN_ADMINS = 1;

/**
* Restrict to admins: Show only to non-admins.
* @var int
*/
public const BYADMIN_NONADMINS = 2;

/**
* Cache key for the menus list.
*/
Expand Down
2 changes: 2 additions & 0 deletions db/install.xml
Original file line number Diff line number Diff line change
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 < 2024100702) {

// 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, 2024100702, 'theme', 'boost_union');
}

return true;
}
6 changes: 6 additions & 0 deletions lang/en/theme_boost_union.php
Original file line number Diff line number Diff line change
Expand Up @@ -1120,6 +1120,11 @@
$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'] = 'Show to';
$string['smartmenusbyadmin_help'] = 'Restrict the visibility based on the fact if the user is a site admin or not.';
$string['smartmenusbyadmin_all'] = 'All users';
$string['smartmenusbyadmin_admins'] = 'Site admins only';
$string['smartmenusbyadmin_nonadmins'] = 'Non-admins only';
$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 @@ -1277,6 +1282,7 @@
$string['smartmenusrestrictbydateheader'] = 'Restrict visibility by date';
$string['smartmenusrestrictbylanguageheader'] = 'Restrict visibility by language';
$string['smartmenusrestrictbyrolesheader'] = 'Restrict visibility by roles';
$string['smartmenusrestrictbyadminheader'] = 'Restrict visibility by site admin status';
$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
24 changes: 24 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 by site admin status.
if (!$this->restriction_byadmin()) {
return false;
}

// Restriction by cohorts.
$this->restriction_bycohorts($query);

Expand Down Expand Up @@ -180,6 +185,25 @@ public function restriction_byroles(&$query) {
$query->params += array_merge($params, $inparam);
}

/**
* Verify if the menu is restricted to site admins.
*
* @return bool True if the menu is available for this user, otherwise false.
*/
public function restriction_byadmin() {
// If the item is restricted to site admins only.
if ($this->data->byadmin == smartmenu::BYADMIN_ADMINS) {
return is_siteadmin($this->userid);

// Otherwise, if the item is restricted to non-site admins only.
} else if ($this->data->byadmin == smartmenu::BYADMIN_NONADMINS) {
return !is_siteadmin($this->userid);
}

// Allow the item to be viewed by the user.
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,27 @@ 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 "smartmenus" "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 "Show to" 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 |
| 0 | should | should |
| 1 | should | should not |
| 2 | should not | should |

@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
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,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: Menus: Rules - Show smart menu based on being site admin
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"
And I click on ".action-edit" "css_element" in the "Quick links" "table_row"
And I expand all fieldsets
And I set the field "Show to" to "<byadmin>"
And I click on "Save and return" "button"
And I <adminshouldorshouldnot> see smart menu "Quick links" in location "Main, Menu, User, Bottom"
And I log out
And I log in as "student1"
Then I <student1shouldorshouldnot> see smart menu "Quick links" in location "Main, Menu, User, Bottom"

Examples:
| byadmin | adminshouldorshouldnot | student1shouldorshouldnot |
| 0 | should | should |
| 1 | should | should not |
| 2 | should not | should |

@javascript
Scenario Outline: Smartmenu: Menus: Rules - Show smart menu based on the user assignment in single cohorts
When I navigate to smart menus
Expand Down
2 changes: 1 addition & 1 deletion version.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
defined('MOODLE_INTERNAL') || die();

$plugin->component = 'theme_boost_union';
$plugin->version = 2024100701;
$plugin->version = 2024100702;
$plugin->release = 'v4.5-r2';
$plugin->requires = 2024100700;
$plugin->supported = [405, 405];
Expand Down

0 comments on commit 7154d81

Please sign in to comment.