Skip to content

Commit

Permalink
Improvement: Enhance smart menu restrictions for authenticated and gu…
Browse files Browse the repository at this point in the history
…est roles, resolves moodle-an-hochschulen#571
  • Loading branch information
prasanna-lmsace authored and abias committed May 11, 2024
1 parent af0be32 commit 83fd30f
Show file tree
Hide file tree
Showing 5 changed files with 49 additions and 18 deletions.
4 changes: 3 additions & 1 deletion classes/form/smartmenu_edit_form.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
4 changes: 3 additions & 1 deletion classes/form/smartmenu_item_edit_form.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
18 changes: 16 additions & 2 deletions smartmenus/menulib.php
Original file line number Diff line number Diff line change
Expand Up @@ -137,14 +137,29 @@ 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 ($roles == '' || empty($roles)) {
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)
Expand All @@ -156,7 +171,6 @@ public function restriction_byroles(&$query) {
'systemcontext' => context_system::instance()->id,
];
$query->params += array_merge($params, $inparam);

}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ Feature: Configuring the theme_boost_union plugin on the "Smart menus" page, app
And I set the field "By role" to "<byrole>"
And I set the field "Context" to "<context>"
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 <adminshouldorshouldnot> 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 <managershouldorshouldnot> see smart menu "Quick links" item "Resources" in location "Main, Menu, User, Bottom"
Expand All @@ -79,14 +79,20 @@ Feature: Configuring the theme_boost_union plugin on the "Smart menus" page, app
Then I <teachershouldorshouldnot> 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 <systemshouldorshouldnot> see smart menu "Quick links" item "Resources" in location "Main, Menu, User, Bottom"
And I log in as "guest"
Then I <guestshouldorshouldnot> 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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 "<byrole>"
And I set the field "Context" to "<context>"
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 <adminshouldorshouldnot> see smart menu "Quick links" in location "Main, Menu, User, Bottom"
And I log out
And I log in as "coursemanager"
Then I <managershouldorshouldnot> see smart menu "Quick links" in location "Main, Menu, User, Bottom"
Expand All @@ -73,14 +73,21 @@ Feature: Configuring the theme_boost_union plugin on the "Smart menus" page, app
Then I <teachershouldorshouldnot> 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 <systemshouldorshouldnot> see smart menu "Quick links" in location "Main, Menu, User, Bottom"
And I log out
And I log in as "guest"
Then I <guestshouldorshouldnot> 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
Expand Down

0 comments on commit 83fd30f

Please sign in to comment.