<commit_before>require 'netaddr'
require 'uri'

class ApplicationController < ActionController::Base
  include Blacklight::Controller

  rescue_from ActionController::RoutingError, :with => :render_not_found

  protect_from_forgery

  before_filter :set_locale
  before_filter :log_user_info
  before_filter :authenticate
  before_filter :set_google_analytics_dimensions_and_metrics
  before_filter :set_search_history

  def set_locale
    I18n.locale = params[:locale] || I18n.default_locale
  end

  def current_locale
    I18n.locale
  end

  helper_method :current_locale

  def log_user_info
    logger.info "session_id: #{request.session_options[:id]}"
    logger.info "current_user.id: #{current_user.id}"

    logger.info "current_user: #{current_user.inspect}" if current_user.authenticated?
    logger.info "original_user: #{original_user.inspect}" if current_user.impersonating?
  rescue Exception => e
    logger.warn "Could not log user info: #{e.class} #{e.message}"
  end

  # Authenticate users if certain criteria are met.
  # - No authentication will be done if user is already logged in.
  # - Force authentication if the shunting cookie indicates
  #   that last successful login was via CAS or
  #   if the user originates from a DTU Campus ip address
  # - Otherwise authentication is optional
  def authenticate

    # This suppresses the log in suggestion on subsequent
    # request if the user clicks "No"
    if params[:stay_anonymous]
      cookies[:shunt_hint] = 'anonymous'
      logger.info "Suppressing log in suggestion"
      redirect_to url_for(params.except!(:stay_anonymous))
    end

    if params[:public]
      cookies[:shunt_hint] = 'public'
      redirect_to url_for(params.except!(:public))
    end

    if should_force_authentication
      force_authentication
    end
  end


  def should_force_authentication
    # We force authentication in two situations:
    #  - The user has successfully logged in via DTU Cas before (i.e. the shunt cookie is set to 'dtu')
    #  - The user has never been logged in before  (i.e. the shunt hint cookie is not set), and originates from an identified campus IP
    can?(:login, User) && (cookies[:shunt] == 'dtu') || (!cookies[:shunt_hint] && campus_request? && !params[:dlib])
  end

  def force_authentication(params = {})
    logger.info "Forcing authentication: cookies[:shunt]:#{cookies[:shunt]}, cookies[:shunt_hint]:#{cookies[:shunt_hint]}, campus_request?:#{campus_request?}"
    params[:url] = request.url
    logger.info "params: #{params}"
    redirect_to new_user_session_path(params)
  end

  def require_authentication
    logger.info "Require authentication for params:#{params}. cookies[:shunt]:#{cookies[:shunt]}, cookies[:shunt_hint]:#{cookies[:shunt_hint]}"
    if cookies[:shunt]<commit_msg>Never force authentication for walk-in users.<commit_after>require 'netaddr'
require 'uri'

class ApplicationController < ActionController::Base
  include Blacklight::Controller

  rescue_from ActionController::RoutingError, :with => :render_not_found

  protect_from_forgery

  before_filter :set_locale
  before_filter :log_user_info
  before_filter :authenticate
  before_filter :set_google_analytics_dimensions_and_metrics
  before_filter :set_search_history

  def set_locale
    I18n.locale = params[:locale] || I18n.default_locale
  end

  def current_locale
    I18n.locale
  end

  helper_method :current_locale

  def log_user_info
    logger.info "session_id: #{request.session_options[:id]}"
    logger.info "current_user.id: #{current_user.id}"

    logger.info "current_user: #{current_user.inspect}" if current_user.authenticated?
    logger.info "original_user: #{original_user.inspect}" if current_user.impersonating?
  rescue Exception => e
    logger.warn "Could not log user info: #{e.class} #{e.message}"
  end

  # Authenticate users if certain criteria are met.
  # - No authentication will be done if user is already logged in.
  # - Force authentication if the shunting cookie indicates
  #   that last successful login was via CAS or
  #   if the user originates from a DTU Campus ip address
  # - Otherwise authentication is optional
  def authenticate

    # This suppresses the log in suggestion on subsequent
    # request if the user clicks "No"
    if params[:stay_anonymous]
      cookies[:shunt_hint] = 'anonymous'
      logger.info "Suppressing log in suggestion"
      redirect_to url_for(params.except!(:stay_anonymous))
    end

    if params[:public]
      cookies[:shunt_hint] = 'public'
      redirect_to url_for(params.except!(:public))
    end

    if should_force_authentication
      force_authentication
    end
  end


  def should_force_authentication
    # We force authentication in two situations:
    #  - The user has successfully logged in via DTU Cas before (i.e. the shunt cookie is set to 'dtu')
    #  - The user has never been logged in before  (i.e. the shunt hint cookie is not set), and originates from an identified campus IP
    return false if walk_in_request?
    can?(:login, User) && (cookies[:shunt] == 'dtu') || (!cookies[:shunt_hint] && campus_request? && !params[:dlib])
  end

  def force_authentication(params = {})
    logger.info "Forcing authentication: cookies[:shunt]:#{cookies[:shunt]}, cookies[:shunt_hint]:#{cookies[:shunt_hint]}, campus_request?:#{campus_request?}"
    params[:url] = request.url
    logger.info "params: #{params}"
    redirect_to new_user_session_path(params)
  end

  def require_authentication
    logger.info "Require authentication for params:#{params}. cookies[:shunt]:#{cookies[:shunt]}, cookies[:shunt_hint]:#{cookies[:shunt_hint]}"
    if cookies[:shunt]
{'fraction_non_alphanumeric': 0.09630676798020851, 'fraction_numerical': 0.002120515992224775, 'mean_word_length': 4.349716446124764, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '920084', 'n_tokens_mistral': 1718, 'n_tokens_neox': 1679, 'n_words': 482}