Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions changelog/fix-teacher-login-redirect-two-factor
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
Significance: patch
Type: fixed

Fixed a login loop for teachers using a two-factor plugin who log in on the My Courses page.
13 changes: 6 additions & 7 deletions includes/class-sensei-teacher.php
Original file line number Diff line number Diff line change
Expand Up @@ -1724,6 +1724,9 @@ public function limit_teacher_edit_screen_post_types( $wp_query ) {
/**
* Redirect teachers (and admins) to the WordPress dashboard after logging in on the My Courses page.
*
* Only sets the requested redirect. The login flow redirects once it finishes, so later
* `wp_login` handlers, such as a two-factor code prompt, still run and keep the destination.
*
* @since 1.8.7
* @access public
*
Expand Down Expand Up @@ -1752,14 +1755,10 @@ public function teacher_login_redirect( $user_login, $user ) {
return;
}

// Keep a redirect the login request already asks for.
// phpcs:ignore WordPress.Security.NonceVerification -- We are not making any changes based on this.
if ( isset( $_REQUEST['redirect_to'] ) ) {
// phpcs:ignore WordPress.Security.NonceVerification -- We are not making any changes based on this.
wp_safe_redirect( wp_unslash( $_REQUEST['redirect_to'] ), 303 );
exit;
} else {
wp_redirect( admin_url(), 303 );
exit;
if ( ! isset( $_REQUEST['redirect_to'] ) ) {
$_REQUEST['redirect_to'] = admin_url();
}
}

Expand Down
113 changes: 113 additions & 0 deletions tests/unit-tests/test-class-teacher.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ public function setUp(): void {
*/
public function tearDown(): void {
parent::tearDown();
unset( $_REQUEST['redirect_to'], $_SERVER['HTTP_REFERER'] );
$this->factory->tearDown();
unset( $GLOBALS['wp_rest_server'] );

Expand Down Expand Up @@ -698,6 +699,99 @@ public function testLimitReportDownloadToTeacher_WhenExportingAsTeacher_LimitsLe
$this->assertSame( array( $own_learner ), $learner_ids, 'Only learners from the teacher-owned course should be exported.' );
}

/**
* Teachers who log in on the My Courses page are sent to the dashboard.
*
* @covers Sensei_Teacher::teacher_login_redirect
*/
public function testTeacherLoginRedirect_TeacherLoggedInOnMyCoursesPageGiven_RequestsDashboardRedirect() {
/* Arrange. */
$teacher = $this->factory->user->create_and_get( array( 'role' => 'teacher' ) );
$this->set_login_referrer_to_my_courses_page();

/* Act. */
do_action( 'wp_login', $teacher->user_login, $teacher );

/* Assert. */
$this->assertSame( admin_url(), $this->get_requested_login_redirect() );
}

/**
* A redirect the login request already asks for is kept.
*
* @covers Sensei_Teacher::teacher_login_redirect
*/
public function testTeacherLoginRedirect_RedirectAlreadyRequestedGiven_KeepsRequestedRedirect() {
/* Arrange. */
$teacher = $this->factory->user->create_and_get( array( 'role' => 'teacher' ) );
$this->set_login_referrer_to_my_courses_page();
$_REQUEST['redirect_to'] = home_url( '/requested/' );

/* Act. */
do_action( 'wp_login', $teacher->user_login, $teacher );

/* Assert. */
$this->assertSame( home_url( '/requested/' ), $this->get_requested_login_redirect() );
}

/**
* Students who log in on the My Courses page keep the default redirect.
*
* @covers Sensei_Teacher::teacher_login_redirect
*/
public function testTeacherLoginRedirect_StudentLoggedInOnMyCoursesPageGiven_RequestsNoRedirect() {
/* Arrange. */
$student = $this->factory->user->create_and_get( array( 'role' => 'subscriber' ) );
$this->set_login_referrer_to_my_courses_page();

/* Act. */
do_action( 'wp_login', $student->user_login, $student );

/* Assert. */
$this->assertNull( $this->get_requested_login_redirect() );
}

/**
* Teachers who log in elsewhere than the My Courses page keep the default redirect.
*
* @covers Sensei_Teacher::teacher_login_redirect
*/
public function testTeacherLoginRedirect_TeacherLoggedInOnAnotherPageGiven_RequestsNoRedirect() {
/* Arrange. */
$teacher = $this->factory->user->create_and_get( array( 'role' => 'teacher' ) );
$this->set_login_referrer_to_my_courses_page();
$_SERVER['HTTP_REFERER'] = home_url( '/another-page/' );

/* Act. */
do_action( 'wp_login', $teacher->user_login, $teacher );

/* Assert. */
$this->assertNull( $this->get_requested_login_redirect() );
}

/**
* Other `wp_login` handlers, such as a two-factor code prompt, still run after a teacher logs in on the My Courses page.
*
* @covers Sensei_Teacher::teacher_login_redirect
*/
public function testTeacherLoginRedirect_TeacherLoggedInOnMyCoursesPageGiven_RunsLaterLoginHandlers() {
/* Arrange. */
$teacher = $this->factory->user->create_and_get( array( 'role' => 'teacher' ) );
$this->set_login_referrer_to_my_courses_page();
$later_handler_ran = false;
$later_handler = function () use ( &$later_handler_ran ) {
$later_handler_ran = true;
};
add_action( 'wp_login', $later_handler, PHP_INT_MAX );

/* Act. */
do_action( 'wp_login', $teacher->user_login, $teacher );

/* Assert. */
remove_action( 'wp_login', $later_handler, PHP_INT_MAX );
$this->assertTrue( $later_handler_ran );
}

public function testRestrictCommentModerationCapability_TeacherOutsideTheAdminGiven_DeniesModerateComments() {
/* Arrange. */
$this->login_as_teacher();
Expand Down Expand Up @@ -806,4 +900,23 @@ public function testRestrictCommentModerationCapability_TeacherApprovedCommentOn
/* Assert. */
$this->assertSame( 200, $response->get_status() );
}

/**
* Get the redirect the current login request asks for.
*
* @return string|null The requested redirect, or null when none is requested.
*/
private function get_requested_login_redirect() {
// phpcs:ignore WordPress.Security -- Reads the test request state.
return isset( $_REQUEST['redirect_to'] ) ? $_REQUEST['redirect_to'] : null;
}

/**
* Make the current login request come from the My Courses page.
*/
private function set_login_referrer_to_my_courses_page() {
$my_courses_page_id = $this->factory->post->create( array( 'post_type' => 'page' ) );
Sensei()->settings->set( 'my_course_page', $my_courses_page_id );
$_SERVER['HTTP_REFERER'] = get_permalink( $my_courses_page_id );
}
}
Loading