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

pulibrary / tigerdata-app / ddee6371-727a-419d-be10-3fd2132d4b80

31 Oct 2025 05:33PM UTC coverage: 91.394% (+3.7%) from 87.65%
ddee6371-727a-419d-be10-3fd2132d4b80

push

circleci

web-flow
Change layout of draft request details (#2125)

ref #2107

3 of 14 new or added lines in 1 file covered. (21.43%)

665 existing lines in 31 files now uncovered.

2825 of 3091 relevant lines covered (91.39%)

488.44 hits per line

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

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

UNCOV
6
  class << self
2✔
7

8

9
    # Returns a list of Users that match the given query
UNCOV
10
    def user_list_query(query)
2✔
UNCOV
11
      tokens = query.downcase.strip.split(/[^a-zA-Z\d]/).compact_blank
29✔
UNCOV
12
      return [] if tokens.count == 0
29✔
UNCOV
13
      if (tokens.count == 1)
28✔
14
        # if I have a single token I might be trying a uid search, so put all the uid matches at the top
UNCOV
15
        uid_query(tokens[0]) | name_query(tokens)
16✔
UNCOV
16
      else
UNCOV
17
        name_query(tokens)
12✔
UNCOV
18
      end.map{|user| { uid: user.uid, name: user.display_name, display_name: user.display_name_safe } }
30✔
UNCOV
19
    end
20

UNCOV
21
    def uid_query(token)
2✔
UNCOV
22
      order_sql = User.sanitize_sql_for_order("LENGTH(uid)-LENGTH('#{token}')")
16✔
UNCOV
23
      search_token = User.sanitize_sql_like(token)+'%'
16✔
UNCOV
24
      User.where("(uid like ?)",search_token).order(Arel.sql(order_sql)).order(:uid)
16✔
UNCOV
25
    end
26

UNCOV
27
    def name_query(tokens)
2✔
UNCOV
28
      tokens.inject(User.all) do |partial_query, token|
28✔
UNCOV
29
        search_token = '%'+User.sanitize_sql_like(token)+'%'
46✔
UNCOV
30
        partial_query.where("(LOWER(display_name) like ?) OR (LOWER(uid) like ?)", search_token, search_token)
46✔
UNCOV
31
      end.order(:given_name).order(:family_name)
UNCOV
32
    end
33

UNCOV
34
    def load_rdss_developers
2✔
UNCOV
35
      RDSS_DEVELOPERS.each do |netid|
2✔
UNCOV
36
        create_user_from_ldap_by_uid(netid)
9✔
UNCOV
37
        rescue TigerData::LdapError
UNCOV
38
        raise TigerData::LdapError, "Unable to create user from LDAP. Are you connected to VPN?"
1✔
UNCOV
39
      end
UNCOV
40
    end
41

42
    # Creates users from LDAP data, starting with the given uid prefix.
UNCOV
43
    def create_users_from_ldap(current_uid_start: "", ldap_connection: default_ldap_connection)
2✔
UNCOV
44
      CHARS_AND_NUMS.each do |char|
5✔
UNCOV
45
        filter =(~ Net::LDAP::Filter.eq( "pustatus", "guest" )) & Net::LDAP::Filter.eq("uid", "#{current_uid_start}#{char}*")
185✔
UNCOV
46
        people = ldap_connection.search(filter:, attributes: [:pudisplayname, :givenname, :sn, :uid, :edupersonprincipalname]);
185✔
UNCOV
47
        if ldap_connection.get_operation_result.message == "Success"
185✔
UNCOV
48
          people.each{|person| user_from_ldap(person)}
188✔
UNCOV
49
        else
UNCOV
50
          create_users_from_ldap(current_uid_start: "#{current_uid_start}#{char}", ldap_connection:)
1✔
UNCOV
51
        end
UNCOV
52
      end
UNCOV
53
    end
54

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

63
  # Creates or updates a User from an LDAP entry.
64
  # @param ldap_person [Net::LDAP::Entry] an LDAP entry representing a person
65
  # @return [User, nil] the created or updated User, or nil if the LDAP entry is missing a edupersonprincipalname
UNCOV
66
    def user_from_ldap(ldap_person)
2✔
UNCOV
67
      return if check_for_malformed_ldap_entries(ldap_person)
16✔
UNCOV
68
      uid = ldap_person[:uid].first.downcase
13✔
UNCOV
69
      current_entries = User.where(uid:)
13✔
UNCOV
70
      if current_entries.empty?
13✔
UNCOV
71
        User.create(uid: , display_name: ldap_person[:pudisplayname].first,
12✔
UNCOV
72
                    family_name: ldap_person[:sn].first, given_name: ldap_person[:givenname].first,
UNCOV
73
                    email: ldap_person[:edupersonprincipalname].first, provider: "cas")
UNCOV
74
      else
UNCOV
75
        user = current_entries.first
1✔
UNCOV
76
        if user.display_name.blank?
1✔
UNCOV
77
          user.display_name = ldap_person[:pudisplayname].first
1✔
UNCOV
78
          user.family_name = ldap_person[:sn].first
1✔
UNCOV
79
          user.given_name = ldap_person[:givenname].first
1✔
UNCOV
80
          user.provider = "cas"
1✔
UNCOV
81
          user.save
1✔
UNCOV
82
        end
UNCOV
83
        user
1✔
UNCOV
84
      end
UNCOV
85
    end
86

87
    # If any required LDAP fields are missing, return true
88
    # @param ldap_person [Net::LDAP::Entry] an LDAP entry representing a person
89
    # @return [Boolean] true if the LDAP entry is missing required fields, false otherwise
UNCOV
90
    def check_for_malformed_ldap_entries(ldap_person)
2✔
UNCOV
91
      uid_blank = ldap_person[:uid].blank?
17✔
UNCOV
92
      edupersonprincipalname_blank = ldap_person[:edupersonprincipalname].blank?
17✔
UNCOV
93
      malformed = uid_blank || edupersonprincipalname_blank
17✔
UNCOV
94
      malformed
17✔
UNCOV
95
    end
96

UNCOV
97
    def default_ldap_connection
2✔
UNCOV
98
      @default_ldap_connection ||= Net::LDAP.new host: "ldap.princeton.edu", base: "o=Princeton University,c=US", port: 636,
9✔
UNCOV
99
                                                  encryption: {
UNCOV
100
                                                    method: :simple_tls,
UNCOV
101
                                                    tls_options: OpenSSL::SSL::SSLContext::DEFAULT_PARAMS
UNCOV
102
                                                  }
UNCOV
103
    end
UNCOV
104
  end
UNCOV
105
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