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 #571 (#640)
  • Loading branch information
prasanna-lmsace authored May 11, 2024
1 parent af0be32 commit 909bbab
Show file tree
Hide file tree
Showing 6 changed files with 72 additions and 19 deletions.
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-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
Expand Down
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
27 changes: 24 additions & 3 deletions smartmenus/menulib.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand All @@ -156,7 +178,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 @@ -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"
Expand All @@ -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 "<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 +85,21 @@ 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 <visitorshouldorshouldnot> 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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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 "<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 +79,22 @@ 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 <visitorshouldorshouldnot> 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
Expand Down

0 comments on commit 909bbab

Please sign in to comment.