<commit_before>require 'net/ldap'
require 'net/ldap/dn'
require_relative 'ldap_ext'

module GuildBook
  class UserRepo
    def initialize(uri)
      @uri = uri
    end

    def find(query = nil, include_inactive = true)
      filter = Net::LDAP::Filter.present('uid')

      if query and (query = query.strip) and !query.empty?
        filter &= SEARCH_ATTRS.collect {|attr| Net::LDAP::Filter.contains(attr, query) }.inject(:|)
      end

      if !include_inactive
        filter &= ~Net::LDAP::Filter.present('shadowExpire')
      end

      Net::LDAP.open_uri(@uri) do |conn|
        conn.search(filter: filter, attributes: ['*'])
      end.collect(&:fix_encoding!)
    end

    def get(uid)
      Net::LDAP.open_uri(@uri) do |conn|
        conn.search(filter: Net::LDAP::Filter.eq('uid', uid),
                    attributes: ['*']).first or raise Sinatra::NotFound
      end.fix_encoding!
    end

    def edit(uid, password, attrs)
      attrs['cn'] = "#{attrs['givenName']} #{attrs['sn']}"

      Net::LDAP.open_uri(@uri) do |conn|
        dn = Net::LDAP::DN.new('uid', uid, conn.base)

        conn.bind(method: :simple, username: dn, password: password)
        EDITABLE_ATTRS.each do |n|
          conn.replace_attribute(dn, n, attrs[n])
        end
      end
    end

    private

    SEARCH_ATTRS = %w[
      uid
      sn sn;lang-ja x-kmc-PhoneticSurname
      givenName givenName;lang-ja x-kmc-PhoneticGivenName
    ]

    EDITABLE_ATTRS = %w[
      cn
      sn sn;lang-ja x-kmc-PhoneticSurname
      givenName givenName;lang-ja x-kmc-PhoneticGivenName
      x-kmc-UnivesityDepartment x-kmc-UniversityStatus x-kmc-UniversityMatricYear
      alias title x-kmc-Generation description
      postalCode postalAddress
      mobile homePhone
    ]
  end
end<commit_msg>sophisticatedness<commit_after>require 'net/ldap'
require 'net/ldap/dn'
require_relative 'ldap_ext'

module GuildBook
  class UserRepo
    def initialize(uri)
      @uri = uri
    end

    def find(query = nil, include_inactive = true)
      filter = Net::LDAP::Filter.present('uid')

      if query and (query = query.strip) and !query.empty?
        filter &= SEARCH_ATTRS.collect {|attr| Net::LDAP::Filter.contains(attr, query) }.inject(:|)
      end

      if !include_inactive
        filter &= ~Net::LDAP::Filter.present('shadowExpire')
      end

      Net::LDAP.open_uri(@uri) do |conn|
        conn.search(filter: filter, attributes: ['*'])
      end.collect(&:fix_encoding!)
    end

    def get(uid)
      Net::LDAP.open_uri(@uri) do |conn|
        conn.search(filter: Net::LDAP::Filter.eq('uid', uid),
                    attributes: ['*']).first or raise Sinatra::NotFound
      end.fix_encoding!
    end

    def edit(uid, password, attrs)
      attrs['cn'] = "#{attrs['givenName']} #{attrs['sn']}"

      Net::LDAP.open_uri(@uri) do |conn|
        dn = Net::LDAP::DN.new('uid', uid, conn.base)

        conn.bind(method: :simple, username: dn, password: password)
        EDITABLE_ATTRS.each do |n|
          conn.replace_attribute(dn, n, attrs[n])
        end
      end
    end

    private

    SEARCH_ATTRS = %w[uid name]

    EDITABLE_ATTRS = %w[
      cn
      sn sn;lang-ja x-kmc-PhoneticSurname
      givenName givenName;lang-ja x-kmc-PhoneticGivenName
      x-kmc-UnivesityDepartment x-kmc-UniversityStatus x-kmc-UniversityMatricYear
      alias title x-kmc-Generation description
      postalCode postalAddress
      mobile homePhone
    ]
  end
end
{'fraction_non_alphanumeric': 0.1304726007538417, 'fraction_numerical': 0.0, 'mean_word_length': 2.7096774193548385, 'pattern_counts': {'":': 0, '<': 3, '<?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': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7265183', 'n_tokens_mistral': 1188, 'n_tokens_neox': 1147, 'n_words': 243}