<commit_before>require_relative '../../lib/shared/properties'
require 'set'

class LdapConfig
  attr_accessor :config

  def initialize(root_dir=nil)
    set_root_dir(root_dir)
    ldap_config = {}
    @config = Properties.load_file(config_file_path) if File.exists?(config_file_path)
    check_configuration_validity
  end

  def self.exists?
    File.exists?(config_file_path) && @config && @config['ldap'] && @config['ldap']['enable'].present?
  end

  def [](key_string)
    keys = key_string.split('.')
    keys.inject(@config) do |hash, key|
      hash.fetch(key)
    end
  rescue IndexError
    nil
  end

   def with_temporary_config(new_config_hash)
     old_config = @config.deep_dup
     @config.deep_merge! new_config_hash.stringify_keys
     yield
   ensure
     @config = old_config
   end

  def self.config_file_path(root_dir=nil)
    root_dir = Rails.root unless root_dir
    File.join root_dir, 'config/ldap.properties'
  end

  def config_file_path
    self.class.config_file_path(@root_dir)
  end

  def self.deep_merge(hash, other_hash)
    deep_merge!(hash.dup, other_hash)
  end

  def self.deep_merge!(hash, other_hash)
    other_hash.each_pair do |k,v|
      tv = hash[k]
      hash[k] = tv.is_a?(Hash) && v.is_a?(Hash) ? deep_merge(tv, v) : v
    end
    hash
  end

  def self.instance
    @instance ||= LdapConfig.new
  end

  private

  def set_root_dir(root_dir)
    @root_dir = root_dir || Rails.root
  end

  def check_configuration_validity

    return if @config['ldap']['enable'].nil? || @config['ldap']['enable'] == false

    # will raise error if one of these properties is missing
    required_properties = [
        'enable',
        'host',
        'port',
        { 'bind' => ['username', 'password' ] },
        { 'user' => ['search_base', 'filter'] }
    ]


    mutually_dependent_properties = [
        { 'group' => ['names', 'search_base', 'filter'] }
    ]

    required_properties.each do |prop|
      if prop.is_a? String
        raise LdapClient::LdapNotCorrectlyConfigured.new("Missing value for property ldap.#{prop} in ldap.properties file") if @config["ldap"][prop].nil?
      end

      if prop.is_a? Hash
        attribute = prop.keys.first
        nested_props = prop.values.first
        nested_props.each do |nested_prop|
          raise LdapClient::LdapNotCorrectlyConfigured.new("Missing value for property ldap.#{attribute}.#{nested_prop} in ldap.properties file") if @config["ldap"][attribute][nested_prop].nil?
        end
      end
    end

    mutually_dependent_properties.each do |prop|
      if prop.is_a? Hash
        attribute = prop.keys.first
        nested_props = prop.values.first

        # If the whole group is missing, it's ok.
        return if @config['ldap'][attribute].nil?

        # Otherwise, make sure all of the properties exist
        is_valid = nested_props.all? do |nested_prop|
          @config['ldap'][attribute].key?(nested_prop) && !@config["ldap"][attribute][nested_prop].nil?
        end

        all_props_string = nested_props.inject("") {|result, nested_prop| "#{result} #{attribute}.#{nested_prop}"}
        raise LdapClient::LdapNotCorrectlyConfigured.new("One or more of these LDAP settings are mis-configured: (#{all_props_string}). Please contact your system administrator.") unless is_valid
      end
    end
  end

end
<commit_msg>DEV-8647 fixed bug in ldap_config that would sometimes prevent new ldap.properties from overwriting old chorus.properties<commit_after>require_relative '../../lib/shared/properties'
require 'set'

class LdapConfig
  attr_accessor :config

  def initialize(root_dir=nil)
    set_root_dir(root_dir)
    ldap_config = {}
    @config = Properties.load_file(config_file_path) if File.exists?(config_file_path)
    check_configuration_validity
  end

  def self.exists?
    config = self.instance
    File.exists?(config_file_path) && config && config['ldap'] && config['ldap']['enable'].present?
  end

  def [](key_string)
    keys = key_string.split('.')
    keys.inject(@config) do |hash, key|
      hash.fetch(key)
    end
  rescue IndexError
    nil
  end

   def with_temporary_config(new_config_hash)
     old_config = @config.deep_dup
     @config.deep_merge! new_config_hash.stringify_keys
     yield
   ensure
     @config = old_config
   end

  def self.config_file_path(root_dir=nil)
    root_dir = Rails.root unless root_dir
    File.join root_dir, 'config/ldap.properties'
  end

  def config_file_path
    self.class.config_file_path(@root_dir)
  end

  def self.deep_merge(hash, other_hash)
    deep_merge!(hash.dup, other_hash)
  end

  def self.deep_merge!(hash, other_hash)
    other_hash.each_pair do |k,v|
      tv = hash[k]
      hash[k] = tv.is_a?(Hash) && v.is_a?(Hash) ? deep_merge(tv, v) : v
    end
    hash
  end

  def self.instance
    @instance ||= LdapConfig.new
  end

  private

  def set_root_dir(root_dir)
    @root_dir = root_dir || Rails.root
  end

  def check_configuration_validity

    return if @cofig.nil? || @config['ldap'].nil? || @config['ldap']['enable'].nil? || @config['ldap']['enable'] == false

    # will raise error if one of these properties is missing
    required_properties = [
        'enable',
        'host',
        'port',
        { 'bind' => ['username', 'password' ] },
        { 'user' => ['search_base', 'filter'] }
    ]


    mutually_dependent_properties = [
        { 'group' => ['names', 'search_base', 'filter'] }
    ]

    required_properties.each do |prop|
      if prop.is_a? String
        raise LdapClient::LdapNotCorrectlyConfigured.new("Missing value for property ldap.#{prop} in ldap.properties file") if @config["ldap"][prop].nil?
      end

      if prop.is_a? Hash
        attribute = prop.keys.first
        nested_props = prop.values.first
        nested_props.each do |nested_prop|
          raise LdapClient::LdapNotCorrectlyConfigured.new("Missing value for property ldap.#{attribute}.#{nested_prop} in ldap.properties file") if @config["ldap"][attribute][nested_prop].nil?
        end
      end
    end

    mutually_dependent_properties.each do |prop|
      if prop.is_a? Hash
        attribute = prop.keys.first
        nested_props = prop.values.first

        # If the whole group is missing, it's ok.
        return if @config['ldap'][attribute].nil?

        # Otherwise, make sure all of the properties exist
        is_valid = nested_props.all? do |nested_prop|
          @config['ldap'][attribute].key?(nested_prop) && !@config["ldap"][attribute][nested_prop].nil?
        end

        all_props_string = nested_props.inject("") {|result, nested_prop| "#{result} #{attribute}.#{nested_prop}"}
        raise LdapClient::LdapNotCorrectlyConfigured.new("One or more of these LDAP settings are mis-configured: (#{all_props_string}). Please contact your system administrator.") unless is_valid
      end
    end
  end

end

{'fraction_non_alphanumeric': 0.12049598832968636, 'fraction_numerical': 0.0005835156819839533, 'mean_word_length': 3.2061349693251535, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 9, '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': '1971154', 'n_tokens_mistral': 2230, 'n_tokens_neox': 2212, 'n_words': 492}