Skip to content

Commit

Permalink
MDL-72325 user: Do not show user tours without site policy agreed
Browse files Browse the repository at this point in the history
Fetching user tours used to fail on external_api::validate_context() and
require_login() calls if the user did not have the site policy agreed.

The patch introduces a check to see if the user is fully set up and
ready to use the site before attempting to load the tours.
  • Loading branch information
mudrd8mz committed Aug 12, 2021
1 parent 605ce27 commit 3188b6f
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 1 deletion.
21 changes: 20 additions & 1 deletion admin/tool/usertours/classes/manager.php
Expand Up @@ -622,7 +622,26 @@ public static function get_current_tours($reset = false): array {
* @return array
*/
public static function get_matching_tours(\moodle_url $pageurl): array {
global $PAGE;
global $PAGE, $USER;

// The following three checks make sure that the user is fully ready to use the site. If not, we do not show any tours.
// We need the user to get properly set up so that all require_login() and other bits work as expected.

if (user_not_fully_set_up($USER)) {
return [];
}

if (get_user_preferences('auth_forcepasswordchange', false)) {
return [];
}

if (empty($USER->policyagreed) && !is_siteadmin()) {
$manager = new \core_privacy\local\sitepolicy\manager();

if ($manager->is_defined(isguestuser())) {
return [];
}
}

$tours = cache::get_matching_tourdata($pageurl);

Expand Down
32 changes: 32 additions & 0 deletions admin/tool/usertours/tests/manager_test.php
Expand Up @@ -325,6 +325,8 @@ public function get_matching_tours_provider() {
public function test_get_matching_tours(array $alltours, string $url, array $expected) {
$this->resetAfterTest();

$this->setGuestUser();

foreach ($alltours as $tourconfig) {
$tour = $this->helper_create_tour((object) $tourconfig);
$this->helper_create_step((object) ['tourid' => $tour->get_id()]);
Expand All @@ -336,4 +338,34 @@ public function test_get_matching_tours(array $alltours, string $url, array $exp
$this->assertEquals($expected[$i], $matches[$i]->get_name());
}
}

/**
* Test that no matching tours are returned if there is pending site policy agreement.
*/
public function test_get_matching_tours_for_user_without_site_policy_agreed() {
global $CFG;

$this->resetAfterTest();
$this->setGuestUser();

$tour = $this->helper_create_tour((object) [
'pathmatch' => '/%',
'enabled' => true,
'name' => 'Test tour',
'description' => '',
'configdata' => '',
]);

$this->helper_create_step((object) [
'tourid' => $tour->get_id(),
]);

$matches = \tool_usertours\manager::get_matching_tours(new moodle_url('/'));
$this->assertEquals(1, count($matches));

$CFG->sitepolicyguest = 'https://example.com';

$matches = \tool_usertours\manager::get_matching_tours(new moodle_url('/'));
$this->assertEmpty($matches);
}
}

0 comments on commit 3188b6f

Please sign in to comment.