• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

pulibrary / tigerdata-app / 8d70f2ab-acc5-4aab-b64b-743d66ddd2eb

29 Aug 2025 06:22PM UTC coverage: 87.983% (-0.1%) from 88.118%
8d70f2ab-acc5-4aab-b64b-743d66ddd2eb

Pull #1801

circleci

JaymeeH
Merge branch '1586-request-mailer' of https://github.com/pulibrary/tiger-data-app into 1586-request-mailer
Pull Request #1801: 1586 request mailer

10 of 10 new or added lines in 2 files covered. (100.0%)

1173 existing lines in 56 files now uncovered.

2482 of 2821 relevant lines covered (87.98%)

317.98 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

85.19
/app/services/princeton_users.rb
1
# frozen_string_literal: true
2
class PrincetonUsers
2✔
3
  CHARS_AND_NUMS =  ('a'..'z').to_a + (0..9).to_a + ['-']
2✔
4
  RDSS_DEVELOPERS = %w[bs3097 jrg5 cac9 hc8719 rl3667 kl37 pp9425 jh6441].freeze
2✔
5

6
  class << self
2✔
7

8
    # Return the list of Users who are already in the database.
9
    def user_list
2✔
10
      Rails.cache.fetch("princeton_user_list", expires_in: 6.hours) do
60✔
11
        @user_list = User.all.map { |user| { uid: user.uid, name: user.display_name } }
131✔
12
      end
13
    end
14

15
    # Returns a list of Users that match the given query
16
    def user_list_query(query)
2✔
UNCOV
17
      tokens = query.downcase.strip.split(/[^a-zA-Z\d]/).compact_blank
6✔
UNCOV
18
      return [] if tokens.count == 0
6✔
UNCOV
19
      user_list.select { |user| user_match?(user, tokens) }
12✔
20
    end
21

22
    def load_rdss_developers
2✔
UNCOV
23
      RDSS_DEVELOPERS.each do |netid|
1✔
UNCOV
24
        create_user_from_ldap_by_uid(netid)
8✔
25
        rescue TigerData::LdapError
26
        raise TigerData::LdapError, "Unable to create user from LDAP. Are you connected to VPN?"
×
27
      end
28
    end
29

30
    # Creates users from LDAP data, starting with the given uid prefix.
31
    def create_users_from_ldap(current_uid_start: "", ldap_connection: default_ldap_connection)
2✔
UNCOV
32
      CHARS_AND_NUMS.each do |char|
5✔
UNCOV
33
        filter =(~ Net::LDAP::Filter.eq( "pustatus", "guest" )) & Net::LDAP::Filter.eq("uid", "#{current_uid_start}#{char}*")
185✔
UNCOV
34
        people = ldap_connection.search(filter:, attributes: [:pudisplayname, :givenname, :sn, :uid, :edupersonprincipalname]);
185✔
UNCOV
35
        if ldap_connection.get_operation_result.message == "Success"
185✔
UNCOV
36
          people.each{|person| user_from_ldap(person)}
188✔
37
        else
UNCOV
38
          create_users_from_ldap(current_uid_start: "#{current_uid_start}#{char}", ldap_connection:)
1✔
39
        end
40
      end
41
    end
42

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

51
    def user_from_ldap(ldap_person)
2✔
UNCOV
52
      return if ldap_person[:edupersonprincipalname].blank?
12✔
UNCOV
53
      uid = ldap_person[:uid].first.downcase
11✔
UNCOV
54
      current_entries = User.where(uid:)
11✔
UNCOV
55
      if current_entries.empty?
11✔
UNCOV
56
        User.create(uid: , display_name: ldap_person[:pudisplayname].first,
11✔
57
                    family_name: ldap_person[:sn].first, given_name: ldap_person[:givenname].first,
58
                    email: ldap_person[:edupersonprincipalname].first, provider: "cas")
59
      else
60
        user = current_entries.first
×
61
        if user.display_name.blank?
×
62
          user.display_name = ldap_person[:pudisplayname].first
×
63
          user.family_name = ldap_person[:sn].first
×
64
          user.given_name = ldap_person[:givenname].first
×
65
          user.provider = "cas"
×
66
          user.save
×
67
        end
68
      end
69
    end
70

71
    def default_ldap_connection
2✔
UNCOV
72
      @default_ldap_connection ||= Net::LDAP.new host: "ldap.princeton.edu", base: "o=Princeton University,c=US", port: 636,
9✔
73
                                                  encryption: {
74
                                                    method: :simple_tls,
75
                                                    tls_options: OpenSSL::SSL::SSLContext::DEFAULT_PARAMS
76
                                                  }
77
    end
78

79
    # Compares a user hash to the tokens to detect whether the user uid or name matches
80
    # Tokens must already be downcased
81
    def user_match?(user, tokens)
2✔
UNCOV
82
      uid = user[:uid].downcase
18✔
UNCOV
83
      uid_matches = tokens.select { |token| uid.include?(token) }
44✔
UNCOV
84
      return true if uid_matches.count == tokens.count  # match by uid
18✔
85

UNCOV
86
      name = user[:name]&.downcase
11✔
UNCOV
87
      return false if name.nil? # there is no name, nothing more to do
11✔
88

UNCOV
89
      name_matches = tokens.select { |token| name.include?(token) }
25✔
UNCOV
90
      return true if name_matches.count == tokens.count # match by name
9✔
91

UNCOV
92
      combined_matches = (name_matches + uid_matches).sort
5✔
UNCOV
93
      return true if combined_matches == tokens.sort # match by name and uid
5✔
94

UNCOV
95
      false
3✔
96
    end
97
  end
98
end
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc