<commit_before># Copyright (C) 2012-2013 Zammad Foundation, http://zammad-foundation.org/

require 'geoip'

class ApplicationController < ActionController::Base
  #  http_basic_authenticate_with :name => "test", :password => "<PASSWORD>"

  helper_method :current_user,
  :authentication_check,
  :config_frontend,
  :user_data_full,
  :is_role,
  :model_create_render,
  :model_update_render,
  :model_restory_render,
  :mode_show_rendeder,
  :model_index_render

  before_filter :set_user, :session_update
  before_filter :cors_preflight_check

  after_filter  :set_access_control_headers
  after_filter  :trigger_events

  # For all responses in this controller, return the CORS access control headers.
  def set_access_control_headers
    headers['Access-Control-Allow-Origin']      = '*'
    headers['Access-Control-Allow-Methods']     = 'POST, GET, PUT, DELETE, OPTIONS'
    headers['Access-Control-Max-Age']           = '1728000'
    headers['Access-Control-Allow-Headers']     = 'Content-Type, Depth, User-Agent, X-File-Size, X-Requested-With, If-Modified-Since, X-File-Name, Cache-Control'
    headers['Access-Control-Allow-Credentials'] = 'true'
  end

  # If this is a preflight OPTIONS request, then short-circuit the
  # request, return only the necessary headers and return an empty
  # text/plain.

  def cors_preflight_check
    if request.method == 'OPTIONS'
      headers['Access-Control-Allow-Origin']      = '*'
      headers['Access-Control-Allow-Methods']     = 'POST, GET, PUT, DELETE, OPTIONS'
      headers['Access-Control-Allow-Headers']     = 'Content-Type, Depth, User-Agent, X-File-Size, X-Requested-With, If-Modified-Since, X-File-Name, Cache-Control'
      headers['Access-Control-Max-Age']           = '1728000'
      headers['Access-Control-Allow-Credentials'] = 'true'
      render :text => '', :content_type => 'text/plain'
      return false
    end
  end

  private

  # execute events
  def trigger_events
    Observer::Ticket::Notification.transaction
  end

  # Finds the User with the ID stored in the session with the key
  # :current_user_id This is a common way to handle user login in
  # a Rails application; logging in sets the session value and
  # logging out removes it.
  def current_user
    return @_current_user if @_current_user
    return if !session[:user_id]
    @_current_user = User.find_by_id( session[:user_id] )
  end
  def current_user_set(user)
    @_current_user = user
    set_user
  end

  # Sets the current user into a named Thread location so that it can be accessed
  # by models and observers
  def set_user
    return if !current_user
    UserInfo.current_user_id = current_user.id
  end

  # update session updated_at
  def session_update
    session[:ping] = Time.now.utc.iso8601

    # check if remote ip need to be updated
    if !session[:remote_id] || session[:remote_id] != request.remote_ip
      session[:remote_id]  = request.remote_ip
      session[:geo]        = Geoip.location( request.remote_ip )
    end

    # fill user agent
    if !session[:user_agent]
      session[:user_agent] = request.env['HTTP_USER_AGENT']
    end
  end

  def authentication_check_only

    puts 'authentication_check'
    session[:request_type] = 1
    #puts params.inspect
    #puts session.inspect
    #puts cookies.inspect

    # check http basic auth
    authenticate_with_http_basic do |username, password|
      puts 'http basic auth check'
      session[:request_type] = 2

      userdata = User.authenticate( username, password )
      message = ''
      if !userdata
        message = 'authentication failed'
      end

      # remember user
      session[:user_id] = userdata.id

      # return auth ok
      if message == ''

        # set basic auth user to current user
        current_user_set(userdata)
        return {
          :auth => true
        }
      end

      # return auth not ok<commit_msg>Fixed set of user for basic auth.<commit_after># Copyright (C) 2012-2013 Zammad Foundation, http://zammad-foundation.org/

require 'geoip'

class ApplicationController < ActionController::Base
  #  http_basic_authenticate_with :name => "test", :password => "<PASSWORD>"

  helper_method :current_user,
  :authentication_check,
  :config_frontend,
  :user_data_full,
  :is_role,
  :model_create_render,
  :model_update_render,
  :model_restory_render,
  :mode_show_rendeder,
  :model_index_render

  before_filter :set_user, :session_update
  before_filter :cors_preflight_check

  after_filter  :set_access_control_headers
  after_filter  :trigger_events

  # For all responses in this controller, return the CORS access control headers.
  def set_access_control_headers
    headers['Access-Control-Allow-Origin']      = '*'
    headers['Access-Control-Allow-Methods']     = 'POST, GET, PUT, DELETE, OPTIONS'
    headers['Access-Control-Max-Age']           = '1728000'
    headers['Access-Control-Allow-Headers']     = 'Content-Type, Depth, User-Agent, X-File-Size, X-Requested-With, If-Modified-Since, X-File-Name, Cache-Control'
    headers['Access-Control-Allow-Credentials'] = 'true'
  end

  # If this is a preflight OPTIONS request, then short-circuit the
  # request, return only the necessary headers and return an empty
  # text/plain.

  def cors_preflight_check
    if request.method == 'OPTIONS'
      headers['Access-Control-Allow-Origin']      = '*'
      headers['Access-Control-Allow-Methods']     = 'POST, GET, PUT, DELETE, OPTIONS'
      headers['Access-Control-Allow-Headers']     = 'Content-Type, Depth, User-Agent, X-File-Size, X-Requested-With, If-Modified-Since, X-File-Name, Cache-Control'
      headers['Access-Control-Max-Age']           = '1728000'
      headers['Access-Control-Allow-Credentials'] = 'true'
      render :text => '', :content_type => 'text/plain'
      return false
    end
  end

  private

  # execute events
  def trigger_events
    Observer::Ticket::Notification.transaction
  end

  # Finds the User with the ID stored in the session with the key
  # :current_user_id This is a common way to handle user login in
  # a Rails application; logging in sets the session value and
  # logging out removes it.
  def current_user
    return @_current_user if @_current_user
    return if !session[:user_id]
    @_current_user = User.find_by_id( session[:user_id] )
  end
  def current_user_set(user)
    @_current_user = user
    set_user
  end

  # Sets the current user into a named Thread location so that it can be accessed
  # by models and observers
  def set_user
    return if !current_user
    UserInfo.current_user_id = current_user.id
  end

  # update session updated_at
  def session_update
    session[:ping] = Time.now.utc.iso8601

    # check if remote ip need to be updated
    if !session[:remote_id] || session[:remote_id] != request.remote_ip
      session[:remote_id]  = request.remote_ip
      session[:geo]        = Geoip.location( request.remote_ip )
    end

    # fill user agent
    if !session[:user_agent]
      session[:user_agent] = request.env['HTTP_USER_AGENT']
    end
  end

  def authentication_check_only

    puts 'authentication_check'
    session[:request_type] = 1
    #puts params.inspect
    #puts session.inspect
    #puts cookies.inspect

    # check http basic auth
    authenticate_with_http_basic do |username, password|
      puts 'http basic auth check'
      session[:request_type] = 2

      userdata = User.authenticate( username, password )
      message = ''
      if !userdata
        message = 'authentication failed'
      end

      # return auth ok
      if message == ''

        # remember user
        session[:user_id] = userdata.id

        # set basic auth user to current user
        current_user_set(userdata)
        return {
          :auth => true
        }
      end

      # return auth not ok
{'fraction_non_alphanumeric': 0.09532974082627663, 'fraction_numerical': 0.00718501411342058, 'mean_word_length': 3.1998922413793105, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 15, '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': '4216551', 'n_tokens_mistral': 2477, 'n_tokens_neox': 2368, 'n_words': 633}