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



34
35
36
37
38
39
40
# File 'app/services/princeton_users.rb', line 34

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.



22
23
24
25
26
27
28
29
30
31
32
# File 'app/services/princeton_users.rb', line 22

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



62
63
64
65
66
67
68
# File 'app/services/princeton_users.rb', line 62

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



15
16
17
18
19
# File 'app/services/princeton_users.rb', line 15

def load_rdss_developers
  RDSS_DEVELOPERS.each do |netid|
    create_user_from_ldap_by_uid(netid)
  end
end

.user_from_ldap(ldap_person) ⇒ Object



42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'app/services/princeton_users.rb', line 42

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