Class: UcbRailsUser::LdapPerson::Finder

Inherits:
Object
  • Object
show all
Defined in:
app/models/ucb_rails_user/ldap_person/finder.rb

Constant Summary collapse

BlankSearchTermsError =
Class.new(StandardError)
PersonNotFound =
Class.new(StandardError)

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.klassObject



73
74
75
76
77
78
79
# File 'app/models/ucb_rails_user/ldap_person/finder.rb', line 73

def klass
  if Rails.env.test? || UcbRailsUser[:omniauth_provider] == :developer
    UcbRailsUser::LdapPerson::TestFinder
  else
    UcbRailsUser::LdapPerson::Finder
  end
end

.method_missing(method, *args) ⇒ Object



81
82
83
# File 'app/models/ucb_rails_user/ldap_person/finder.rb', line 81

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

Instance Method Details

#benchmark(message = "Benchmarking") ⇒ Object



6
7
8
9
10
11
# File 'app/models/ucb_rails_user/ldap_person/finder.rb', line 6

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

#build_filter(attrs, options = {}) ⇒ Object



58
59
60
61
62
63
64
65
# File 'app/models/ucb_rails_user/ldap_person/finder.rb', line 58

def build_filter(attrs, options={})
  operator = options[:operator] || :&
  filter_parts = attrs.map { |k, values|
    Array(values).map{|v| build_filter_part(k, v) }
  }.flatten
  filter = filter_parts.inject { |accum, filter| accum.send(operator, filter) }
  filter
end

#build_filter_part(key, value) ⇒ Object



67
68
69
70
# File 'app/models/ucb_rails_user/ldap_person/finder.rb', line 67

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

#find_by_affiliate_id(affiliate_id) ⇒ Object



35
36
37
# File 'app/models/ucb_rails_user/ldap_person/finder.rb', line 35

def find_by_affiliate_id(affiliate_id)
  find_by_attributes("berkeleyEduAffID" => affiliate_id)
end

#find_by_attributes(attributes, options = {}) ⇒ Object



39
40
41
42
43
44
45
46
47
48
49
# File 'app/models/ucb_rails_user/ldap_person/finder.rb', line 39

def find_by_attributes(attributes, options={})
  attributes.each { |k, v| attributes.delete(k) if v.blank?  }

  search_opts = { :filter => build_filter(attributes, options) }
  search_opts[:return_result] = options[:return_result] if options.has_key?(:return_result)
  search_opts[:size] = options[:size] if options.has_key?(:size)

  UCB::LDAP::Person.
    search(search_opts).
    map { |ldap_entry| Entry.new_from_ldap_entry(ldap_entry) }
end

#find_by_first_last(first_name, last_name, options = {}) ⇒ Object



24
25
26
27
28
29
30
31
32
33
# File 'app/models/ucb_rails_user/ldap_person/finder.rb', line 24

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}, options).tap do |entries|
    if options[:sort]
      sort_symbol = options[:sort]
      entries.sort_by!(&options[:sort])
    end
  end
end

#find_by_uid(uid) ⇒ Object



13
14
15
16
17
18
# File 'app/models/ucb_rails_user/ldap_person/finder.rb', line 13

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

#find_by_uid!(uid) ⇒ Object



20
21
22
# File 'app/models/ucb_rails_user/ldap_person/finder.rb', line 20

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

#find_expired_by_attributes(attributes) ⇒ Object



51
52
53
54
55
56
# File 'app/models/ucb_rails_user/ldap_person/finder.rb', line 51

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