<commit_before>require 'net/ldap'
require 'ldap/user'

module LDAP
  class Connection
    @config

    def initialize(config)
      @config = config

      options = {
        base: config.base,
        host: config.host,
        port: config.port || 389 # SSL port: 636
      }

      if config.auth_dn && !config.auth_dn.empty?
        options[:auth] = {
          method: :simple,
          username: config.auth_dn,
          password: config.auth_password
        }
      end

      # Enable encryption if ldaps port is used.
      options[:encryption] = :simple_tls if options[:port] == 636
      options[:encryption] = :simple_tls if config.tls == true

      @ldap = Net::LDAP.new(options)
    end

    def find_user(login, password)
      filter = Net::LDAP::Filter.eq(@config.uid, login)
      begin
        response = @ldap.bind_as(:filter => filter, :password => password)
        response ? LDAP::User.new(response.first) : false
      rescue Exception => e
        # typically thrown if bind did not return result
        Rails.logger.error e
        false
      end
    end

    def is_admin?(login)
      if @config.admin_group.blank?
        return true
      else
        begin
          if @ldap.bind
            # check membership in admin group
            filter = Net::LDAP::Filter.eq(@config.group_membership_attribute, login)
            result = @ldap.search(base: @config.base, filter: filter)
            result.each do |entry|
              return true if entry.dn == @config.admin_group
            end
          else
            Rails.logger.error "error on LDAP bind: " + @ldap.get_operation_result
          end
        rescue Exception => e
          # typically thrown if bind did not return result
          Rails.logger.error e
        end
      end
      false
    end
  end
end<commit_msg>#4841: Improve LDAP group membership retrieval.<commit_after>require 'net/ldap'
require 'ldap/user'

module LDAP
  class Connection
    @config

    def initialize(config)
      @config = config

      options = {
        base: config.base,
        host: config.host,
        port: config.port || 389 # SSL port: 636
      }

      if config.auth_dn && !config.auth_dn.empty?
        options[:auth] = {
          method: :simple,
          username: config.auth_dn,
          password: config.auth_password
        }
      end

      # Enable encryption if ldaps port is used.
      options[:encryption] = :simple_tls if options[:port] == 636
      options[:encryption] = :simple_tls if config.tls == true

      @ldap = Net::LDAP.new(options)
    end

    def find_user(login, password)
      filter = Net::LDAP::Filter.eq(@config.uid, login)
      begin
        response = @ldap.bind_as(:filter => filter, :password => password)
        response ? LDAP::User.new(response.first) : false
      rescue Exception => e
        # typically thrown if bind did not return result
        Rails.logger.error e
        false
      end
    end

    def is_admin?(login)
      if @config.admin_group.blank?
        return true
      else
        begin
          if @ldap.bind
            # check if admin group exists
            result = @ldap.search(base: @config.admin_group)
            if result.blank?
              Rails.logger.error "LDAP admin group #{@config.admin_group} not found"
              return true
            else
              # check group membership of the logged in user
              result.first[@config.group_membership_attribute].each do |entry|
                return true if entry.to_s == login
              end
            end
          else
            Rails.logger.error "error on LDAP bind: " + @ldap.get_operation_result
          end
        rescue Exception => e
          # typically thrown if bind did not return result
          Rails.logger.error e
        end
      end
      false
    end
  end
end
{'fraction_non_alphanumeric': 0.08829621957534955, 'fraction_numerical': 0.005696530295183843, 'mean_word_length': 1.8404411764705881, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 11, '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': '4091911', 'n_tokens_mistral': 1107, 'n_tokens_neox': 1073, 'n_words': 310}