<commit_before>
require 'digest/md5'
require 'exception_notifier'
require Rails.root.join('@server', 'permissions')

class ApplicationController < ActionController::Base
  protect_from_forgery with: :exception
  skip_before_action :verify_authenticity_token, if: :csrf_skippable?

  set_current_tenant_through_filter
  prepend_before_action :get_current_subdomain
  before_action :init_thread_globals
  after_action :allow_iframe_requests

  rescue_from PermissionDenied do |exception|
    result = { :permission_denied => exception.reason } 
    result[:key] = exception.key if exception.key
    render :json => result
  end


  def application
    dirty_key '/application'
    render :json => []
  end

  def app_index
    render :json => [{
      :key => '/apps',
      :apps => ['franklin', 'product_page']
    }]
  end

  def render(*args)
    unless @oembed_request 
      # dirty_key '/application'

      # if there are dirtied keys, we'll append the corresponding data to the response
      if current_subdomain && Thread.current[:dirtied_keys].keys.length > 0
        for arg in args
          if arg.is_a?(::Hash) && arg.has_key?(:json)
            if arg[:json].is_a?(::Hash)
              arg[:json] = [arg[:json]]
            end
            arg[:json] += compile_dirty_objects()
          end
        end
      end
    end

    super

  end

  def authorize!(action, object = nil, key = nil)
    permitted = permit(action, object)
    if permitted < 0
      raise PermissionDenied.new permitted, key
    end
  end

protected
  def csrf_skippable?
    request.format.json? && request.content_type != "text/plain" && (!!request.xml_http_request?)
  end

  def write_to_log(options)
    begin
      Log.create!({
        :subdomain_id => current_subdomain.id,
        :who => current_user.id,
        :what => options[:what],
        :where => options[:where],
        :when => Time.current,
        :details => options.has_key?(:details) ? JSON.dump(options[:details]) : nil
      })
    rescue => e
      ExceptionNotifier.notify_exception(e)      
    end
  end

  def get_current_subdomain
    rq = request

    # when to display a considerit homepage
    can_display_homepage = (Rails.env.production? && rq.host.include?('consider.it')) || session[:app] == 'product_page'
    if (rq.subdomain.nil? || rq.subdomain.length == 0) && can_display_homepage 
      candidate_subdomain = Subdomain.find_by_name('homepage')
    else
      default_subdomain = session.has_key?(:default_subdomain) ? session[:default_subdomain] : 1
      if rq.subdomain.nil? || rq.subdomain.length == 0
        begin
          candidate_subdomain = Subdomain.find(default_subdomain)
        rescue ActiveRecord::RecordNotFound
          # create a subdomain if one doesn't yet exist
          if Subdomain.count == 0
            new_subdomain = Subdomain.new name: "test", app_title: "test"
            new_subdomain.save
          end
          candidate_subdomain = Subdomain.first
        end
      else 
        candidate_subdomain = Subdomain.find_by_name(rq.subdomain)
      end
    end

    set_current_tenant(candidate_subdomain) if candidate_subdomain
    current_subdomain<commit_msg>ability to access {subdomain}.localhost:3000 in development<commit_after>
require 'digest/md5'
require 'exception_notifier'
require Rails.root.join('@server', 'permissions')

class ApplicationController < ActionController::Base
  protect_from_forgery with: :exception
  skip_before_action :verify_authenticity_token, if: :csrf_skippable?

  set_current_tenant_through_filter
  prepend_before_action :get_current_subdomain
  before_action :init_thread_globals
  after_action :allow_iframe_requests

  rescue_from PermissionDenied do |exception|
    result = { :permission_denied => exception.reason } 
    result[:key] = exception.key if exception.key
    render :json => result
  end


  def application
    dirty_key '/application'
    render :json => []
  end

  def app_index
    render :json => [{
      :key => '/apps',
      :apps => ['franklin', 'product_page']
    }]
  end

  def render(*args)
    unless @oembed_request 
      # dirty_key '/application'

      # if there are dirtied keys, we'll append the corresponding data to the response
      if current_subdomain && Thread.current[:dirtied_keys].keys.length > 0
        for arg in args
          if arg.is_a?(::Hash) && arg.has_key?(:json)
            if arg[:json].is_a?(::Hash)
              arg[:json] = [arg[:json]]
            end
            arg[:json] += compile_dirty_objects()
          end
        end
      end
    end

    super

  end

  def authorize!(action, object = nil, key = nil)
    permitted = permit(action, object)
    if permitted < 0
      raise PermissionDenied.new permitted, key
    end
  end

protected
  def csrf_skippable?
    request.format.json? && request.content_type != "text/plain" && (!!request.xml_http_request?)
  end

  def write_to_log(options)
    begin
      Log.create!({
        :subdomain_id => current_subdomain.id,
        :who => current_user.id,
        :what => options[:what],
        :where => options[:where],
        :when => Time.current,
        :details => options.has_key?(:details) ? JSON.dump(options[:details]) : nil
      })
    rescue => e
      ExceptionNotifier.notify_exception(e)      
    end
  end

  def get_current_subdomain
    rq = request

    # when to display a considerit homepage
    can_display_homepage = (Rails.env.production? && rq.host.include?('consider.it')) || session[:app] == 'product_page'
    if (rq.subdomain.nil? || rq.subdomain.length == 0) && can_display_homepage 
      candidate_subdomain = Subdomain.find_by_name('homepage')
    else
      default_subdomain = session.has_key?(:default_subdomain) ? session[:default_subdomain] : 1

      if rq.subdomain.nil? || rq.subdomain.length == 0
        candidate_subdomain = nil 

        if Rails.env.development? && rq.host.split('.').length > 1
          candidate_subdomain = Subdomain.find_by_name(rq.host.split('.')[0])
        else 
          begin
            candidate_subdomain = Subdomain.find(default_subdomain)
          rescue ActiveRecord::RecordNotFound
            # create a subdomain if one doesn't yet exist
            if Subdomain.count == 0
              new_subdomain = Subdomain.new name: "test", app_title: "test"
              new_subdomain.save
            end
            candidate_subdomain = Subdomain.first
          end
        end
      elsif !candidate_subdomain
        candidate_subdomain = Subdomain.find_by_name(rq.subdomain)
      end

    end

    set_current_tenant(candidate_subdomain) if candidate_subdomain
    current_subdomain
{'fraction_non_alphanumeric': 0.10309123649459784, 'fraction_numerical': 0.003001200480192077, 'mean_word_length': 2.8260619977037886, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 32, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 2}, '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': '360916', 'n_tokens_mistral': 2053, 'n_tokens_neox': 1996, 'n_words': 447}