<commit_before>require 'application_responder'

class ApplicationController < ActionController::Base
  self.responder = ApplicationResponder
  respond_to :html

  before_action :load_site_configuration
  before_action :logout_inactive
  before_action :authenticate_user!
  before_action :intercept_sensitive_params!
  protect_from_forgery prepend: true, with: :exception

protected

  def admin?
    user_signed_in? && current_user.is_a?(Admin)
  end

  def load_site_configuration
    @site = Site.load
  end

  def logout_inactive
    return true if 'sessions' == controller_name
    return true if controller_name == 'self_registrations'
    return true unless user_signed_in?
    return true if current_user.state == 'accepted' && !current_user.inactive?

    sign_out current_user
    redirect_to root_url, alert: 'Benutzer/in inaktiv'
  end

  # some parameters should only be set by admins.
  # school_id: setting this would allow access to other school's kids
  def intercept_sensitive_params!
    return true unless %w(update create).include?(action_name)
    return true if current_user.is_a?(Admin)
    return true if params.nil? || params.empty?
    # inactive is a sensitive param that is only manageable by admins
    if params.inspect =~ /inactive/
      fail SecurityError.new("User #{current_user.id} not allowed to change inactive flag")
    end
    # school_id may allow users for principals and teachers access to kids outside their school
    # this has to be protected (unless for kids controller, there it is
    # managed by its own method #intercept_school_id)
    # for mentors controller it is unproblematic, since it has no influence on access rights<commit_msg>feat: after login redirect based on step and user<commit_after>require 'application_responder'

class ApplicationController < ActionController::Base
  self.responder = ApplicationResponder
  respond_to :html

  before_action :load_site_configuration
  before_action :logout_inactive
  before_action :authenticate_user!
  before_action :intercept_sensitive_params!
  protect_from_forgery prepend: true, with: :exception

protected

  def admin?
    user_signed_in? && current_user.is_a?(Admin)
  end

  def load_site_configuration
    @site = Site.load
  end

  def after_sign_in_path_for(resource)
    # we are redirecitng user to specific pages based on step in which they are
    if current_user.is_a?(Mentor) && current_user.kids.empty?
      # if a menotr has no kids yet assigned, go to available kids
      available_kids_path
    elsif current_user.is_a?(Teacher) && current_user.mentor_matchings.pluck(:state).include?('pending')
      # if teacher has some pending requests from mentors
      mentor_matchings_path
    else
      root_url
    end
  end

  def logout_inactive
    return true if 'sessions' == controller_name
    return true if controller_name == 'self_registrations'
    return true unless user_signed_in?
    return true if current_user.state == 'accepted' && !current_user.inactive?

    sign_out current_user
    redirect_to root_url, alert: 'Benutzer/in inaktiv'
  end

  # some parameters should only be set by admins.
  # school_id: setting this would allow access to other school's kids
  def intercept_sensitive_params!
    return true unless %w(update create).include?(action_name)
    return true if current_user.is_a?(Admin)
    return true if params.nil? || params.empty?
    # inactive is a sensitive param that is only manageable by admins
    if params.inspect =~ /inactive/
      fail SecurityError.new("User #{current_user.id} not allowed to change inactive flag")
    end
    # school_id may allow users for principals and teachers access to kids outside their school
    # this has to be protected (unless for kids controller, there it is
    # managed by its own method #intercept_school_id)
    # for mentors controller it is unproblematic, since it has no influence on access rights
{'fraction_non_alphanumeric': 0.06096938775510204, 'fraction_numerical': 0.0, 'mean_word_length': 4.172823218997362, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 3, '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': '3387489', 'n_tokens_mistral': 1145, 'n_tokens_neox': 1114, 'n_words': 389}