<commit_before>class Api::Json::UsersController < Api::ApplicationController
  skip_before_filter :api_authorization_required, only: [:get_authenticated_users]
  ssl_required :get_authenticated_users

  if Rails.env.production? || Rails.env.staging?
    ssl_required :show
  end

  def show
    user = current_user
    render json: user.data
  end

  def get_authenticated_users
    authenticated_users = request.session.select {|k,v| k.start_with?("warden.user")}.values
    referer = request.env["HTTP_REFERER"]
    referer_match = /https?:\/\/([\w\-\.]+)(:[\d]+)?(\/(u\/([\w\-\.]+)))?/.match(referer)
 
    if referer_match.nil?
      render status: 400
    else
      subdomain = referer_match[1].gsub(CartoDB.session_domain, '')
      organization_username = referer_match[5]
    end

    # This array is actually a hack. We will only return at most 1 url, but this way is compatible with the old endpoint
    dashboard_urls = []
    dashboard_base_url = ''

    if !authenticated_users.empty?
      # It doesn't have a organization username component
      # We assume it's not a organization referer
      if organization_username.nil?
        # The user is seeing its own dashboard
        if authenticated_users.include?(subdomain)
          dashboard_base_url = CartoDB.base_url(subdomain)
        # The user is authenticated but seeing another user dashboard
        else
          user_belongs_to_organization = CartoDB::UserOrganization.user_belongs_to_organization?(authenticated_users.first)
          # The first user in session does not belong to any organization
          if user_belongs_to_organization.nil?
            dashboard_base_url = CartoDB.base_url(authenticated_users.first)
          else
            dashboard_base_url = CartoDB.base_url(user_belongs_to_organization, authenticated_users.first)
          end
        end
      else
        # The user is seeing its own organization dashboard
        if authenticated_users.include?(organization_username)
          dashboard_base_url = CartoDB.base_url(subdomain, organization_username)
        # The user is seeing a organization dashboard, but not its one
        else
          # Get all users on the referer organization and intersect with the authenticated users list
          requested_organization_users = User.select(:username)
                                          .from('users', 'organizations')
                                          .where("organizations.id=users.organization_id and organizations.name='#{subdomain}'")
                                          .collect(&:username)
          users_intersection = requested_organization_users && authenticated_users<commit_msg>Fixed intersection in get authenticated users<commit_after>class Api::Json::UsersController < Api::ApplicationController
  skip_before_filter :api_authorization_required, only: [:get_authenticated_users]
  ssl_required :get_authenticated_users

  if Rails.env.production? || Rails.env.staging?
    ssl_required :show
  end

  def show
    user = current_user
    render json: user.data
  end

  def get_authenticated_users
    authenticated_users = request.session.select {|k,v| k.start_with?("warden.user")}.values
    referer = request.env["HTTP_REFERER"]
    referer_match = /https?:\/\/([\w\-\.]+)(:[\d]+)?(\/(u\/([\w\-\.]+)))?/.match(referer)
 
    if referer_match.nil?
      render status: 400
    else
      subdomain = referer_match[1].gsub(CartoDB.session_domain, '')
      organization_username = referer_match[5]
    end

    # This array is actually a hack. We will only return at most 1 url, but this way is compatible with the old endpoint
    dashboard_urls = []
    dashboard_base_url = ''

    if !authenticated_users.empty?
      # It doesn't have a organization username component
      # We assume it's not a organization referer
      if organization_username.nil?
        # The user is seeing its own dashboard
        if authenticated_users.include?(subdomain)
          dashboard_base_url = CartoDB.base_url(subdomain)
        # The user is authenticated but seeing another user dashboard
        else
          user_belongs_to_organization = CartoDB::UserOrganization.user_belongs_to_organization?(authenticated_users.first)
          # The first user in session does not belong to any organization
          if user_belongs_to_organization.nil?
            dashboard_base_url = CartoDB.base_url(authenticated_users.first)
          else
            dashboard_base_url = CartoDB.base_url(user_belongs_to_organization, authenticated_users.first)
          end
        end
      else
        # The user is seeing its own organization dashboard
        if authenticated_users.include?(organization_username)
          dashboard_base_url = CartoDB.base_url(subdomain, organization_username)
        # The user is seeing a organization dashboard, but not its one
        else
          # Get all users on the referer organization and intersect with the authenticated users list
          requested_organization_users = User.select(:username)
                                          .from('users', 'organizations')
                                          .where("organizations.id=users.organization_id and organizations.name='#{subdomain}'")
                                          .collect(&:username)
          users_intersection = requested_organization_users & authenticated_users
{'fraction_non_alphanumeric': 0.0796066060493598, 'fraction_numerical': 0.002226758211170904, 'mean_word_length': 2.806497175141243, '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': 12, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2756485', 'n_tokens_mistral': 1504, 'n_tokens_neox': 1436, 'n_words': 368}