DRIVER=serial8250
MODALIAS=platform:serial8250
  7   +ˆl–Ãa¾”*i?u8 …p/Üiå1hß�þBó�,ã€²2óm–7¿ŸÝƒpM5ÜÈ¿Ú d    +# Copyright (C) 2012-2026 Zammad Foundation, https://zammad-foundation.org/

# Revokes what a deactivated or deleted account still holds:
#
# - Its stored sessions, for a deactivated account. They would otherwise outlive the
#   deactivation, so re-activating the account - a compromised one that is disabled and restored
#   afterwards, for instance - would make every session cookie that existed beforehand valid
#   again, an attacker's included. A session the account switched into another user from is
#   dropped even when the account is deleted, since it is served as the switched-to account.
# - Its established real-time connections. A connection keeps the user it was authenticated as
#   for its whole lifetime (see ApplicationCable::Connection), so dropping the session alone
#   would not stop one that is already open from being served.
#
# Drops only the established real-time connections when the organizations or roles of an
#   account change: the account keeps its stored sessions, and the client reconnects with
#   the current memberships.
module User::TerminatesSessions
  extend ActiveSupport::Concern

  included do
    after_update :remember_saved_changes
    after_commit :terminate_sessions, on: :destroy
  end

  # For the membership changes that leave no trace in saved_changes, like the ones written through
  #   an association.
  def note_membership_change(_record = nil)
    # A new record cannot hold a connection yet, and the records it is built with would otherwise
    #   count as a change on its first update.
    return if !persisted?

    note_change(:membership_changed)
  end

  private

  # Noted on save: a later touch or nested save in the same transaction replaces saved_changes.
  def remember_saved_changes
    note_change(:access_revoked) if saved_change_to_active?(from: true, to: false)
    note_membership_change if saved_change_to_organization_id?
  end

  # Hooked into the transaction, not the commit callbacks of the record: Rails runs those only on
  #   the last instance of it saved in the transaction, and not at all on an association write.
  def note_change(change)
    transaction = self.class.current_transaction
    hooked      = noted_changes.key?(transaction)

    (noted_changes[transaction] ||= []) << change
    return if hooked

    # A rolled back savepoint discards only what was noted inside it.
    transaction.after_rollback { noted_changes.delete(transaction) }
    transaction.after_commit { act_on_changes }
  end

  # A deactivation terminates the connections along with the sessions.
  def act_on_changes
    changes = noted_changes.values.flatten

    if changes.include?(:access_revoked)
      terminate_sessions
    elsif changes.include?(:membership_changed)
      renew_realtime_connections
    end
  end

  def terminate_sessions
    # Covers a membership change of the same transaction as well, which must not be acted on again.
    forget_changes

    destroy_stored_sessions

    terminate_realtime_connections(self)
  end

  def renew_realtime_connections
    forget_changes

    terminate_realtime_connections(self)
  end

  def forget_changes
    noted_changes.clear
  end

  def noted_changes = @noted_changes ||= {}

  # The sessions table has no user column, and its data is stored as base64 encoded Marshal, so
  #   there is nothing to filter on in SQL: every row has to be deserialized to find the ones
  #   belonging to this user. Acceptable because revoking an account is rare, but it does make
  #   bulk revocation - an LDAP sync, a CSV import, a data privacy task - scale with the number
  #   of sessions.
  def destroy_stored_sessions
    Session.find_each do |session|
      next if session_owner_id(session) != id

      # This account owns the row, so a different user in 'user_id' is the one it switched to.
      switched_to_user_id = session.data['user_id'] if session.data['user_id'] != id

      # A deleted account's own sessions can be left to SessionTimeout