Problem/Motivation

Trying to use both the legal module and simplesamlphp_auth causes an infinite login loop due to Legal destroying and recreating the users session in legal_user_login. The issue is that simplesamlphp_auth is configured to redirect all users to the SSO login before they can access the site and when logging in simplesaml uses hook_user_login to set a session value tied to simplesaml. When Legal invokes hook_user_logout simplesaml deletes the simplesaml session value so when the user is redirected to /legal_accept they no longer have an active simplesaml session and are redirected to the SSO login. This repeats forever.

Issue code:
legal/legal.module:456

>
  // Log the user out and regenerate the Drupal session.
  \Drupal::logger('user')
    ->notice('Session closed for %name.', array('%name' => $account->getAccountName()));

  \Drupal::moduleHandler()->invokeAll('user_logout', array($account));

  // Destroy the current session, and reset $user to the anonymous user.
  \Drupal::service('session_manager')->destroy();
<

Proposed resolution

Instead of logging users in and out during hook_user_login we should create an event subscriber that subscribes to kernel requests and if the user needs to accept a T&C then redirect them.

Comments

ctrlADel created an issue. See original summary.

ctrladel’s picture

Issue summary: View changes
ctrladel’s picture

Here's a first go at changing over to use a kernel::request subscriber instead of hooking into user_login. I recognize this represents a significant change in how the module currently works but using a redirect on requests feels a lot cleaner and more Symfony friendly than the current approach of manipulating Drupal's login/session management.

Quick summary of the patch:
Most of the logic remains the same for determining if T&C needs to be accepted or not. I made use of the existing "legal_login" session value to help determine if the redirect to "legal_accept" should be active. I removed the hash/token code since we are now working with a fully authenticated user session so Drupal's form handling should prevent most nefarious behavior.

Feedback is welcome.

ctrladel’s picture

Status: Active » Needs review

Status: Needs review » Needs work

The last submitted patch, 3: legal-event_based_tc_redirect-3005208-2.patch, failed testing. View results
- codesniffer_fixes.patch Interdiff of automated coding standards fixes only.

mstrelan’s picture

A couple issues with the patch.

  1. Blocks that should be restricted by user role are visible when viewing the Terms & Conditions.
  2. Doesn't seem to work with the password reset link as the user just ends up viewing their profile and can't update their password.
radelson’s picture

StatusFileSize
new4.82 KB
new15.92 KB

I made some modifications to ctrlADel's patch to make it work with password reset links

Let me know if I can improve it.

avpaderno’s picture

Version: 8.x-1.0-rc1 » 8.x-1.x-dev

Bugs are fixed in the development snapshot.

mstrelan’s picture

Status: Needs work » Closed (duplicate)
Related issues: +#2897486: Don't log out users who do not accept the T&C

This seems to be a duplicate of #2897486: Don't log out users who do not accept the T&C. The patch in that issue seems to work better for me, specifically in the handling of the redirect after accepting the T&C's.