Sha256: dcccafd4a5ea025d5f450f35c847e5151ba90d5f90599b2436cc2eeb4b8d1a18

Contents?: true

Size: 1.83 KB

Versions: 12

Compression:

Stored size: 1.83 KB

Contents

module UcbRails::LdapPerson
  class Finder
    BlankSearchTermsError = Class.new(StandardError)
    PersonNotFound = Class.new(StandardError)

    def benchmark(message = "Benchmarking")
      result = nil
      ms = Benchmark.ms { result = yield }
      Rails.logger.debug "#{message} #{ms}"
      result
    end

    def find_by_uid(uid)
      benchmark("find_by_uid(#{uid})") do
        find_by_attributes(:uid => uid.to_s).first
      end
    end

    def find_by_uid!(uid)
      find_by_uid(uid) or raise(PersonNotFound, "uid=#{uid.inspect}")
    end

    def find_by_first_last(first_name, last_name, options={})
      raise BlankSearchTermsError unless first_name.present? || last_name.present?
      
      find_by_attributes(:givenname => first_name, :sn => last_name).tap do |entries|
        if options[:sort]
          sort_symbol = options[:sort]
          entries.sort_by!(&options[:sort])
        end
      end
    end

    def find_by_attributes(attributes)
      attributes.each { |k, v| attributes.delete(k) if v.blank?  }
      UCB::LDAP::Person.
        search(:filter => build_filter(attributes)).
        map { |ldap_entry| Entry.new_from_ldap_entry(ldap_entry) }
    end

    def build_filter(attrs)
      filter_parts = attrs.map { |k, v| build_filter_part(k, v) }
      filter = filter_parts.inject { |accum, filter| accum.send(:&, filter) }
      filter
    end

    def build_filter_part(key, value)
      value = key.to_s == 'uid' ? value : "#{value}*"
      Net::LDAP::Filter.eq(key.to_s, value)
    end

    class << self
      def klass
        if RailsEnvironment.test? || UcbRails[:omniauth_provider] == :developer
          UcbRails::LdapPerson::TestFinder
        else
          UcbRails::LdapPerson::Finder
        end
      end

      def method_missing(method, *args)
        klass.new.send(method, *args)
      end
    end
    
  end
end

Version data entries

12 entries across 12 versions & 1 rubygems

Version Path
ucb_rails-0.0.13 app/models/ucb_rails/ldap_person/finder.rb
ucb_rails-0.0.12 app/models/ucb_rails/ldap_person/finder.rb
ucb_rails-0.0.11 app/models/ucb_rails/ldap_person/finder.rb
ucb_rails-0.0.10 app/models/ucb_rails/ldap_person/finder.rb
ucb_rails-0.0.9 app/models/ucb_rails/ldap_person/finder.rb
ucb_rails-0.0.8 app/models/ucb_rails/ldap_person/finder.rb
ucb_rails-0.0.7 app/models/ucb_rails/ldap_person/finder.rb
ucb_rails-0.0.6 app/models/ucb_rails/ldap_person/finder.rb
ucb_rails-0.0.5 app/models/ucb_rails/ldap_person/finder.rb
ucb_rails-0.0.4 app/models/ucb_rails/ldap_person/finder.rb
ucb_rails-0.0.3 app/models/ucb_rails/ldap_person/finder.rb
ucb_rails-0.0.2 app/models/ucb_rails/ldap_person/finder.rb