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

pulibrary / tigerdata-app / d27a7c46-ce49-42be-9862-7ca11851ded4

08 Jul 2025 02:30PM UTC coverage: 72.481% (-0.006%) from 72.487%
d27a7c46-ce49-42be-9862-7ca11851ded4

Pull #1597

circleci

JaymeeH
Update continuous deploy
Pull Request #1597: Update continuous deploy

4 of 18 branches covered (22.22%)

3021 of 4168 relevant lines covered (72.48%)

497.6 hits per line

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

84.44
/app/models/user.rb
1
# frozen_string_literal: true
2

3
require "csv"
1✔
4
class User < ApplicationRecord
1✔
5
  # Include default devise modules. Others available are:
6
  # :confirmable, :lockable, :timeoutable, :trackable and :omniauthable
7
  devise :rememberable, :omniauthable
1✔
8

9
  has_many :user_requests, dependent: :destroy
1✔
10

11
  USER_REGISTRATION_LIST = Rails.root.join("data", "user_registration_list_#{Rails.env}.csv")
1✔
12

13
  attr_accessor :mediaflux_session
1✔
14

15
  def self.from_cas(access_token)
1✔
16
    user = User.find_by(provider: access_token.provider, uid: access_token.uid)
9✔
17
    if user.present? && user.given_name.nil? # fix any users that do not have the name information loaded
9✔
18
      user.initialize_name_values(access_token.extra)
7✔
19
      user.save
7✔
20
    end
21
    user
9✔
22
  end
23

24
  # Users that can be project sponsors
25
  def self.sponsor_users
1✔
26
    if Rails.env.development? || Rails.env.staging?
33✔
27
      User.where(eligible_sponsor: true).or(User.where(superuser: true))
1✔
28
    else
29
      User.where(eligible_sponsor: true)
32✔
30
    end
31
  end
32

33
  # Users that can be data managers
34
  def self.manager_users
1✔
35
    if Rails.env.development? || Rails.env.staging?
29✔
36
      User.where(eligible_manager: true).or(User.where(superuser: true))
×
37
    else
38
      User.where(eligible_manager: true)
29✔
39
    end
40
  end
41

42
  def clear_mediaflux_session(session)
1✔
43
    Rails.logger.debug("!!!!!!! Clearing Mediaflux session !!!!!!!!")
3✔
44
    @mediaflux_session = nil
3✔
45
    session[:mediaflux_session] = nil
3✔
46
  end
47

48
  def mediaflux_from_session(session)
1✔
49
    logger.debug "Session Get #{session[:mediaflux_session]} cas: #{session[:active_web_user]}  user: #{uid}"
520✔
50
    if session[:mediaflux_session].blank?
520✔
51
      logger.debug("!!!! Creating a new session !!! #{uid}")
210✔
52
      session[:mediaflux_session] = SystemUser.mediaflux_session
210✔
53
      session[:active_web_user] = false
208✔
54
    end
55
    @active_web_user = session[:active_web_user]
518✔
56
    @mediaflux_session = session[:mediaflux_session]
518✔
57
  end
58

59
  def medaiflux_login(token, session)
1✔
60
    logger.debug("mediaflux session created for #{uid}")
×
61
    logon_request = Mediaflux::LogonRequest.new(identity_token: token, token_type: "cas")
×
62
    if logon_request.error?
×
63
      raise "Invalid Logon #{logon_request.response_error}"
×
64
    end
65
    @mediaflux_session = logon_request.session_token
×
66
    @active_web_user = true
×
67
    session[:mediaflux_session] = @mediaflux_session
×
68
    session[:active_web_user] = @active_web_user
×
69
    logger.debug "Login Session #{session[:mediaflux_session]} cas: #{session[:active_web_user]}  user: #{uid}"
×
70
  end
71

72
  def terminate_mediaflux_session
1✔
73
    return if @mediaflux_session.nil? # nothing to terminate
×
74
    logger.debug "!!!! Terminating mediaflux session"
×
75

76
    Mediaflux::LogoutRequest.new(session_token: @mediaflux_session).response_body
×
77
    @mediaflux_session = nil
×
78
  end
79

80
  # Initialize the name values from the CAS information.
81
  # Our name fields do not match their name fields, so we need to translate.
82
  def initialize_name_values(extra_cas_info)
1✔
83
    self.given_name = extra_cas_info.givenname
7✔
84
    self.family_name =  extra_cas_info.sn
7✔
85
    self.display_name = extra_cas_info.pudisplayname
7✔
86
  end
87

88
  # Return the display name if it exists, otherwise return the uid
89
  # @return [String]
90
  def display_name_safe
1✔
91
    return uid if display_name.blank?
336✔
92

93
    display_name
334✔
94
  end
95

96
  # Is this user eligible to be a data sponsor in this environment?
97
  # @return [Boolean]
98
  def eligible_sponsor?
1✔
99
    return true if superuser
524✔
100
    super
504✔
101
  end
102

103
  # Is this user eligible to be a data manger in this environment?
104
  # @return [Boolean]
105
  def eligible_manager?
1✔
106
    return true if superuser
207✔
107
    super
199✔
108
  end
109

110
  # Is this user eligible to be a data user in this environment?
111
  # @return [Boolean]
112
  def eligible_data_user?
1✔
113
    return true if superuser
71✔
114
    return true if !eligible_sponsor? && !eligible_manager
71✔
115
  end
116

117
  # Is this user eligible to be a sysadmin in this environment?
118
  # @return [Boolean]
119
  def eligible_sysadmin?
1✔
120
    return true if superuser || sysadmin
803✔
121
  end
122

123
  def eligible_to_create_new?
1✔
124
    return true if eligible_sysadmin?
139✔
125

126
    !Rails.env.production? && (eligible_sponsor? && trainer?)
112✔
127
  end
128

129
  # Parse the USER_REGISTRATION_LIST csv
130
  # @return [CSV::Table]
131
  def self.csv_data
1✔
132
    CSV.parse(File.read(USER_REGISTRATION_LIST), headers: true)
6✔
133
  end
134

135
  # Load the user registration list from the CSV file.
136
  # Select the file that matches the rails environment.
137
  def self.load_registration_list
1✔
138
    User.csv_data.each do |line|
7✔
139
      user = User.find_by(uid: line["uid"]) || User.new
1,572✔
140
      user.uid = line["uid"]
1,572✔
141
      user.family_name = line["family_name"]
1,572✔
142
      user.display_name = line["display_name"]
1,572✔
143
      user.email = user.uid + "@princeton.edu"
1,572✔
144
      # If we don't say that this is a cas user, they won't be able to log in with CAS
145
      user.provider = "cas"
1,572✔
146
      user.eligible_sponsor = line["eligible_sponsor"] == "TRUE"
1,572✔
147
      user.eligible_manager = line["eligible_manager"] == "TRUE"
1,572✔
148
      user.superuser = line["superuser"] == "TRUE"
1,572✔
149
      user.sysadmin = line["sysadmin"] == "TRUE"
1,572✔
150
      user.trainer = line["tester_trainer"] == "TRUE"
1,572✔
151
      user.save
1,572✔
152
    end
153
  end
154

155
  # Methods serialize_into_session() and serialize_from_session() are called by Warden/Devise
156
  # to calculate what information will be stored in the session and to serialize an object
157
  # back from the session.
158
  #
159
  # By default Warden/Devise store the database ID of the record (e.g. User.id) but this causes
160
  # problems if we repopulate our User table and the IDs change. The implementation provided below
161
  # uses the User.uid field (which is unique, does not change, and it's required) as the value to
162
  # store in the session to prevent this issue.
163
  #
164
  # References:
165
  #   https://stackoverflow.com/questions/23597718/what-is-the-warden-data-in-a-rails-devise-session-composed-of/23683925#23683925
166
  #   https://web.archive.org/web/20211028103224/https://tadas-s.github.io/ruby-on-rails/2020/08/02/devise-serialize-into-session-trick/
167
  #   https://github.com/wardencommunity/warden/wiki/Setup
168
  def self.serialize_into_session(record)
1✔
169
    # The return value _must_ have at least two elements since the serialize_from_session() requires
170
    # two arguments (see below)
171
    [record.uid, ""]
262✔
172
  end
173

174
  def self.serialize_from_session(key, _salt, _opts = {})
1✔
175
    User.where(uid: key)&.first
320✔
176
  end
177

178
  # Fetches the most recent download jobs for the user
179
  def latest_downloads(limit: 10)
1✔
180
    @latest_downloads ||= UserRequest.where(user_id: id).where(["completion_time > ?", 7.days.ago]).order(created_at: "DESC").limit(limit)
91✔
181
  end
182
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