Class: PrincetonUsers

Inherits:
Object
  • Object
show all
Defined in:
app/services/princeton_users.rb

Constant Summary collapse

CHARS_AND_NUMS =
('a'..'z').to_a + (0..9).to_a + ['-']
RDSS_DEVELOPERS =
%w[bs3097 jrg5 cac9 hc8719 rl3667 kl37 pp9425 jh6441].freeze

Class Method Summary collapse

Class Method Details

.create_user_from_ldap_by_uid(uid, ldap_connection: default_ldap_connection) ⇒ Object



48
49
50
51
52
53
54
# File 'app/services/princeton_users.rb', line 48

def create_user_from_ldap_by_uid(uid, ldap_connection: default_ldap_connection)
  filter = Net::LDAP::Filter.eq('uid', uid)
  person = ldap_connection.search(filter:, attributes: [:pudisplayname, :givenname, :sn, :uid, :edupersonprincipalname]);
  raise TigerData::LdapError, "More than one user matches supplied uid: #{uid}" if person.length > 1
  raise TigerData::LdapError, "No user with uid #{uid} found" if person.empty?
  user_from_ldap(person.first)
end

.create_users_from_ldap(current_uid_start: "", ldap_connection: default_ldap_connection) ⇒ Object

Creates users from LDAP data, starting with the given uid prefix.



36
37
38
39
40
41
42
43
44
45
46
# File 'app/services/princeton_users.rb', line 36

def create_users_from_ldap(current_uid_start: "", ldap_connection: default_ldap_connection)
  CHARS_AND_NUMS.each do |char|
    filter =(~ Net::LDAP::Filter.eq( "pustatus", "guest" )) & Net::LDAP::Filter.eq("uid", "#{current_uid_start}#{char}*")
    people = ldap_connection.search(filter:, attributes: [:pudisplayname, :givenname, :sn, :uid, :edupersonprincipalname]);
    if ldap_connection.get_operation_result.message == "Success"
      people.each{|person| user_from_ldap(person)}
    else
      create_users_from_ldap(current_uid_start: "#{current_uid_start}#{char}", ldap_connection:)
    end
  end
end

.default_ldap_connectionObject



76
77
78
79
80
81
82
# File 'app/services/princeton_users.rb', line 76

def default_ldap_connection
  @default_ldap_connection ||= Net::LDAP.new host: "ldap.princeton.edu", base: "o=Princeton University,c=US", port: 636,
                                              encryption: {
                                                method: :simple_tls,
                                                tls_options: OpenSSL::SSL::SSLContext::DEFAULT_PARAMS
                                              }
end

.load_rdss_developersObject



27
28
29
30
31
32
33
# File 'app/services/princeton_users.rb', line 27

def load_rdss_developers
  RDSS_DEVELOPERS.each do |netid|
    create_user_from_ldap_by_uid(netid)
    rescue TigerData::LdapError
    raise TigerData::LdapError, "Unable to create user from LDAP. Are you connected to VPN?"
  end
end

.user_from_ldap(ldap_person) ⇒ Object



56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'app/services/princeton_users.rb', line 56

def user_from_ldap(ldap_person)
  return if ldap_person[:edupersonprincipalname].blank?
  uid = ldap_person[:uid].first.downcase
  current_entries = User.where(uid:)
  if current_entries.empty?
    User.create(uid: , display_name: ldap_person[:pudisplayname].first,
                family_name: ldap_person[:sn].first, given_name: ldap_person[:givenname].first,
                email: ldap_person[:edupersonprincipalname].first, provider: "cas")
  else
    user = current_entries.first
    if user.display_name.blank?
      user.display_name = ldap_person[:pudisplayname].first
      user.family_name = ldap_person[:sn].first
      user.given_name = ldap_person[:givenname].first
      user.provider = "cas"
      user.save
    end
  end
end

.user_listObject

Return the list of Users who are already in the database.



9
10
11
12
13
# File 'app/services/princeton_users.rb', line 9

def user_list
  Rails.cache.fetch("princeton_user_list", expires_in: 6.hours) do
    @user_list = User.all.map { |user| { uid: user.uid, name: user.display_name } }
  end
end

.user_list_query(query) ⇒ Object

Returns a list of Users that match the given query



16
17
18
19
20
21
22
23
24
25
# File 'app/services/princeton_users.rb', line 16

def user_list_query(query)
  tokens = query.downcase.strip.split(/[^a-zA-Z\d]/).compact_blank
  return [] if tokens.count == 0

  user_query = tokens.inject(User.all) do |partial_query, token| 
                 search_token = '%'+User.sanitize_sql_like(token)+'%'
                 partial_query.where("(LOWER(display_name) like ?) OR (LOWER(uid) like ?)", search_token, search_token)
               end
  user_query.map{|user| { uid: user.uid, name: user.display_name } }
end