Skip to content

Commit 655d22d

Browse files
committed
MDL-72188 quizaccess_seb: Auto redirect should only happen from where config key is checked.
1 parent ec42a56 commit 655d22d

File tree

5 files changed

+77
-49
lines changed

5 files changed

+77
-49
lines changed

mod/quiz/accessrule/seb/classes/access_manager.php

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -370,4 +370,34 @@ public function clear_session_access(): void {
370370
global $SESSION;
371371
unset($SESSION->quizaccess_seb_access[$this->quiz->get_cmid()]);
372372
}
373+
374+
/**
375+
* Redirect to SEB config link. This will force Safe Exam Browser to be reconfigured.
376+
*/
377+
public function redirect_to_seb_config_link() {
378+
global $PAGE;
379+
380+
$seblink = \quizaccess_seb\link_generator::get_link($this->quiz->get_cmid(), true, is_https());
381+
$PAGE->requires->js_amd_inline("document.location.replace('" . $seblink . "')");
382+
}
383+
384+
/**
385+
* Check if we need to redirect to SEB config link.
386+
* @param bool $checkheaders Flag for whether the config key header should be checked. This should be true when not
387+
* using the new SEB JS API.
388+
*
389+
* @return bool
390+
*/
391+
public function should_redirect_to_seb_config_link(bool $checkheaders = false) : bool {
392+
// If $checkheaders is true, we check if there is an existing config key header. If there is none, we assume that
393+
// the SEB application is not using header verification so auto redirect should not proceed.
394+
$headercheck = true;
395+
if ($checkheaders) {
396+
$headercheck = !is_null($this->get_received_config_key());
397+
}
398+
399+
return $this->is_using_seb()
400+
&& get_config('quizaccess_seb', 'autoreconfigureseb')
401+
&& $headercheck;
402+
}
373403
}

mod/quiz/accessrule/seb/classes/external/validate_quiz_access.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,10 @@ public static function execute(string $cmid, string $url, ?string $configkey = n
9898

9999
// Check if there is a valid config key.
100100
if (!$accessmanager->validate_config_key($configkey, $url)) {
101+
if ($accessmanager->should_redirect_to_seb_config_link()) {
102+
$accessmanager->redirect_to_seb_config_link();
103+
}
104+
101105
access_prevented::create_strict($accessmanager, get_string('invalid_config_key', 'quizaccess_seb'),
102106
$configkey, $browserexamkey)->trigger();
103107
return ['valid' => false];

mod/quiz/accessrule/seb/rule.php

Lines changed: 2 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -309,8 +309,8 @@ public function prevent_access() {
309309
}
310310

311311
if (!$this->accessmanager->validate_config_key()) {
312-
if ($this->should_redirect_to_seb_config_link()) {
313-
$this->redirect_to_seb_config_link();
312+
if ($this->accessmanager->should_redirect_to_seb_config_link(true)) {
313+
$this->accessmanager->redirect_to_seb_config_link();
314314
}
315315

316316
access_prevented::create_strict($this->accessmanager, $this->get_reason_text('invalid_config_key'))->trigger();
@@ -594,23 +594,4 @@ private function get_seb_download_url() : string {
594594
private function should_display_download_seb_link() : bool {
595595
return !empty($this->quiz->seb_showsebdownloadlink);
596596
}
597-
598-
/**
599-
* Redirect to SEB config link. This will force Safe Exam Browser to be reconfigured.
600-
*/
601-
private function redirect_to_seb_config_link() {
602-
global $PAGE;
603-
604-
$seblink = \quizaccess_seb\link_generator::get_link($this->quiz->cmid, true, is_https());
605-
$PAGE->requires->js_amd_inline("document.location.replace('" . $seblink . "')");
606-
}
607-
608-
/**
609-
* Check if we need to redirect to SEB config link.
610-
* @return bool
611-
*/
612-
private function should_redirect_to_seb_config_link() : bool {
613-
return $this->accessmanager->is_using_seb() && get_config('quizaccess_seb', 'autoreconfigureseb');
614-
}
615-
616597
}

mod/quiz/accessrule/seb/tests/access_manager_test.php

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -576,4 +576,45 @@ public function test_clear_session_access() {
576576

577577
$this->assertTrue(empty($SESSION->quizaccess_seb_access[$this->quiz->cmid]));
578578
}
579+
580+
/**
581+
* Test we can decide if need to redirect to SEB config link.
582+
*/
583+
public function test_should_redirect_to_seb_config_link_without_checking_header() {
584+
$this->quiz = $this->create_test_quiz($this->course, settings_provider::USE_SEB_CONFIG_MANUALLY);
585+
$accessmanager = $this->get_access_manager();
586+
587+
set_config('autoreconfigureseb', '0', 'quizaccess_seb');
588+
$_SERVER['HTTP_USER_AGENT'] = 'TEST';
589+
$this->assertFalse($accessmanager->should_redirect_to_seb_config_link());
590+
591+
set_config('autoreconfigureseb', '0', 'quizaccess_seb');
592+
$_SERVER['HTTP_USER_AGENT'] = 'SEB';
593+
$this->assertFalse($accessmanager->should_redirect_to_seb_config_link());
594+
595+
set_config('autoreconfigureseb', '1', 'quizaccess_seb');
596+
$_SERVER['HTTP_USER_AGENT'] = 'TEST';
597+
$this->assertFalse($accessmanager->should_redirect_to_seb_config_link());
598+
599+
set_config('autoreconfigureseb', '1', 'quizaccess_seb');
600+
$_SERVER['HTTP_USER_AGENT'] = 'SEB';
601+
$this->assertTrue($accessmanager->should_redirect_to_seb_config_link());
602+
}
603+
604+
/**
605+
* Test we can decide if need to redirect to SEB config link when also checking for config key header.
606+
*/
607+
public function test_should_redirect_to_seb_config_link_with_checking_header() {
608+
$this->quiz = $this->create_test_quiz($this->course, settings_provider::USE_SEB_CONFIG_MANUALLY);
609+
$accessmanager = $this->get_access_manager();
610+
611+
set_config('autoreconfigureseb', '1', 'quizaccess_seb');
612+
$_SERVER['HTTP_USER_AGENT'] = 'SEB';
613+
$this->assertFalse($accessmanager->should_redirect_to_seb_config_link(true));
614+
615+
set_config('autoreconfigureseb', '1', 'quizaccess_seb');
616+
$_SERVER['HTTP_USER_AGENT'] = 'SEB';
617+
$_SERVER['HTTP_X_SAFEEXAMBROWSER_CONFIGKEYHASH'] = hash('sha256', 'configkey');
618+
$this->assertTrue($accessmanager->should_redirect_to_seb_config_link(true));
619+
}
579620
}

mod/quiz/accessrule/seb/tests/rule_test.php

Lines changed: 0 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1265,34 +1265,6 @@ public function test_blocks_display_after_attempt_finished() {
12651265
$this->assertFalse($property->getValue($PAGE->blocks));
12661266
}
12671267

1268-
/**
1269-
* Test we can decide if need to redirect to SEB config link.
1270-
*/
1271-
public function test_should_redirect_to_seb_config_link() {
1272-
$this->setAdminUser();
1273-
$this->quiz = $this->create_test_quiz($this->course, settings_provider::USE_SEB_CONFIG_MANUALLY);
1274-
1275-
$reflection = new \ReflectionClass('quizaccess_seb');
1276-
$method = $reflection->getMethod('should_redirect_to_seb_config_link');
1277-
$method->setAccessible(true);
1278-
1279-
set_config('autoreconfigureseb', '0', 'quizaccess_seb');
1280-
$_SERVER['HTTP_USER_AGENT'] = 'TEST';
1281-
$this->assertFalse($method->invoke($this->make_rule()));
1282-
1283-
set_config('autoreconfigureseb', '0', 'quizaccess_seb');
1284-
$_SERVER['HTTP_USER_AGENT'] = 'SEB';
1285-
$this->assertFalse($method->invoke($this->make_rule()));
1286-
1287-
set_config('autoreconfigureseb', '1', 'quizaccess_seb');
1288-
$_SERVER['HTTP_USER_AGENT'] = 'TEST';
1289-
$this->assertFalse($method->invoke($this->make_rule()));
1290-
1291-
set_config('autoreconfigureseb', '1', 'quizaccess_seb');
1292-
$_SERVER['HTTP_USER_AGENT'] = 'SEB';
1293-
$this->assertTrue($method->invoke($this->make_rule()));
1294-
}
1295-
12961268
/**
12971269
* Test cleanup when quiz is completed.
12981270
*/

0 commit comments

Comments
 (0)