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

pulibrary / pdc_describe / cace366a-ffad-45f1-9b60-678e607fa527

14 May 2024 02:21PM UTC coverage: 60.862% (-35.0%) from 95.908%
cace366a-ffad-45f1-9b60-678e607fa527

push

circleci

jrgriffiniii
wip

1 of 3 new or added lines in 2 files covered. (33.33%)

1194 existing lines in 57 files now uncovered.

2076 of 3411 relevant lines covered (60.86%)

22.71 hits per line

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

53.49
/app/models/user.rb
1
# frozen_string_literal: true
2
require "csv"
1✔
3

4
# rubocop:disable Metrics/ClassLength
5
class User < ApplicationRecord
1✔
6
  rolify
1✔
7
  extend FriendlyId
1✔
8
  friendly_id :uid
1✔
9

10
  devise :rememberable, :omniauthable
1✔
11

12
  # GroupOptions model extensible options set for Groups and Users
13
  has_many :group_options, dependent: :destroy
1✔
14
  has_many :group_messaging_options, -> { where(option_type: GroupOption::EMAIL_MESSAGES) }, class_name: "GroupOption", dependent: :destroy
1✔
15

16
  has_many :groups_with_options, through: :group_options, source: :group
1✔
17
  has_many :groups_with_messaging, through: :group_messaging_options, source: :group
1✔
18

19
  after_create :assign_default_role
1✔
20

21
  attr_accessor :just_created
1✔
22

23
  validate do |user|
1✔
24
    user.orcid&.strip!
70✔
25
    if user.orcid.present? && Orcid.invalid?(user.orcid)
70✔
UNCOV
26
      user.errors.add :base, "Invalid format for ORCID"
×
27
    end
28
  end
29

30
  def self.from_cas(access_token)
1✔
UNCOV
31
    user = User.find_by(uid: access_token.uid)
×
UNCOV
32
    if user.nil?
×
UNCOV
33
      user = new_from_cas(access_token)
×
UNCOV
34
    elsif user.provider.blank?
×
UNCOV
35
      user.update_with_cas(access_token)
×
36
    end
UNCOV
37
    user
×
38
  end
39

40
  # Create a new user with some basic information from CAS.
41
  def self.new_from_cas(access_token)
1✔
UNCOV
42
    user = User.new
×
UNCOV
43
    user.provider = access_token.provider
×
UNCOV
44
    user.uid = access_token.uid # this is the netid
×
UNCOV
45
    user.email = access_token.extra.mail
×
UNCOV
46
    user.given_name = access_token.extra.givenname || access_token.uid # Harriet
×
UNCOV
47
    user.family_name = access_token.extra.sn || access_token.uid # Tubman
×
UNCOV
48
    user.full_name = access_token.extra.displayname || access_token.uid # "Harriet Tubman"
×
UNCOV
49
    user.default_group_id = Group.default_for_department(access_token.extra.departmentnumber)&.id
×
UNCOV
50
    user.save!
×
UNCOV
51
    user
×
52
  end
53

54
  # Updates an existing User record with some information from CAS. This is useful
55
  # for records created before the user ever logged in (e.g. to gran them permissions
56
  # to groups).
57
  def update_with_cas(access_token)
1✔
UNCOV
58
    self.provider = access_token.provider
×
UNCOV
59
    self.email = access_token.extra.mail
×
UNCOV
60
    self.given_name = access_token.extra.givenname || access_token.uid # Harriet
×
UNCOV
61
    self.family_name = access_token.extra.sn || access_token.uid # Tubman
×
UNCOV
62
    self.full_name = access_token.extra.displayname || access_token.uid # "Harriet Tubman"
×
UNCOV
63
    self.default_group_id ||= Group.default_for_department(access_token.extra.departmentnumber)&.id
×
UNCOV
64
    save!
×
65
  end
66

67
  # Creates a new user by uid. If the user already exists it returns the existing user.
68
  def self.new_for_uid(uid)
1✔
69
    user = User.find_by(uid:)
5✔
70
    if user.nil?
5✔
UNCOV
71
      user = User.new(uid:, email: "#{uid}@princeton.edu")
×
UNCOV
72
      user.save!
×
73
    end
74
    user
5✔
75
  end
76

77
  def self.new_super_admin(uid)
1✔
78
    user = new_for_uid(uid)
5✔
79
    user.add_role(:super_admin) unless user.has_role?(:super_admin)
5✔
80
    user.add_role(:group_admin) unless user.has_role?(:group_admin)
5✔
81
    user
5✔
82
  end
83

84
  # rubocop:disable Metrics/MethodLength
85
  def self.new_from_csv_params(csv_params)
1✔
UNCOV
86
    email = "#{csv_params['Net ID']}@princeton.edu"
×
UNCOV
87
    uid = csv_params["Net ID"]
×
UNCOV
88
    given_name = csv_params["First Name"]
×
UNCOV
89
    family_name = csv_params["Last Name"]
×
UNCOV
90
    full_name = "#{given_name} #{family_name}"
×
UNCOV
91
    orcid = csv_params["ORCID ID"]
×
UNCOV
92
    user = User.where(email:).first_or_create
×
93
    params_hash = {
UNCOV
94
      email:,
×
95
      uid:,
96
      orcid:,
UNCOV
97
      full_name: (full_name if user.full_name.blank?),
×
UNCOV
98
      family_name: (family_name if user.family_name.blank?),
×
UNCOV
99
      given_name: (given_name if user.given_name.blank?)
×
100
    }.compact
101

UNCOV
102
    user.update(params_hash)
×
UNCOV
103
    Rails.logger.info "Successfully created or updated #{user.email}"
×
UNCOV
104
    user
×
105
  end
106
  # rubocop:enable Metrics/MethodLength
107

108
  def self.create_users_from_csv(csv)
1✔
UNCOV
109
    users = []
×
UNCOV
110
    CSV.foreach(csv, headers: true) do |row|
×
UNCOV
111
      next if row["Net ID"] == "N/A"
×
UNCOV
112
      users << new_from_csv_params(row.to_hash)
×
113
    end
UNCOV
114
    users
×
115
  end
116

117
  # Creates the default users as indicated in the super_admin config file
118
  # and the default administrators and submitters for each group.
119
  # It only creates missing records, i.e. if the records already exist it
120
  # will not create a duplicate. It also does _not_ remove already configured
121
  # access to other groups.
122
  def self.create_default_users
1✔
UNCOV
123
    update_super_admins
×
124

UNCOV
125
    Group.find_each do |group|
×
UNCOV
126
      Rails.logger.info "Setting up admins for group #{group.title}"
×
UNCOV
127
      group.default_admins_list.each do |uid|
×
UNCOV
128
        user = User.new_for_uid(uid)
×
UNCOV
129
        user.add_role :group_admin, group
×
130
      end
131

UNCOV
132
      Rails.logger.info "Setting up submitters for group #{group.title}"
×
UNCOV
133
      group.default_submitters_list.each do |uid|
×
UNCOV
134
        user = User.new_for_uid(uid)
×
UNCOV
135
        user.add_role :submitter, group
×
136
      end
137
    end
138
  end
139

140
  def self.update_super_admins
1✔
UNCOV
141
    Rails.logger.info "Setting super administrators"
×
UNCOV
142
    Rails.configuration.super_admins.each do |uid|
×
UNCOV
143
      new_super_admin(uid)
×
144
    end
145
  end
146

147
  # Returns a string with the UID (netid) for all the users.
148
  # We use this string to power the JavaScript @mention functionality when adding messages to works.
149
  def self.all_uids_string
1✔
150
    User.all.map { |user| '"' + user.uid + '"' }.join(", ")
23✔
151
  end
152

153
  ##
154
  # Is this user a super_admin? super_admins automatically get admin status in every
155
  # group, and they can make new groups.
156
  # @return [Boolean]
157
  def super_admin?
1✔
158
    has_role? :super_admin
546✔
159
  rescue => ex
UNCOV
160
    Rails.logger.error("Unexpected error checking super_admin: #{ex}")
×
UNCOV
161
    false
×
162
  end
163

164
  # Returns a display name that always has a value
165
  # This is needed because we have records in the Users table that are created automatically
166
  # in which the only value we have for sure its their uid (aka NetID).
167
  def given_name_safe
1✔
168
    given_name.presence || uid
29✔
169
  end
170

171
  # Returns a full name that always has a value
172
  # This is needed because we have records in the Users table that are created automatically
173
  # in which the only value we have for sure its their uid (aka NetID).
174
  def full_name_safe
1✔
175
    full_name&.strip.presence || uid
2✔
176
  end
177

178
  def moderator?
1✔
179
    admin_groups.count > 0
113✔
180
  end
181

182
  # Returns a reference to the user's default group.
183
  def default_group
1✔
184
    if default_group_id.nil?
200✔
185
      Group.default
132✔
186
    else
187
      Group.find(default_group_id)
68✔
188
    end
189
  end
190

191
  # True if the user can submit datasets to the group
192
  def can_submit?(group)
1✔
193
    return true if super_admin?
121✔
194
    has_role?(:submitter, group)
121✔
195
  end
196

197
  # Returns true if the user can admin the group
198
  def can_admin?(group)
1✔
199
    return true if super_admin?
240✔
200
    has_role? :group_admin, group
175✔
201
  end
202

203
  # Returns the list of groups where the user can submit datasets
204
  def submitter_groups
1✔
205
    @submitter_groups = if super_admin?
27✔
206
                          Group.all.to_a
6✔
207
                        else
208
                          (Group.with_role(:submitter, self) + Group.with_role(:group_admin, self)).uniq
21✔
209
                        end
210
  end
211

212
  # Returns the list of groups where the user is an administrator
213
  def admin_groups
1✔
214
    @admin_groups ||= if super_admin?
157✔
215
                        Group.all.to_a
16✔
216
                      else
217
                        Group.with_role(:group_admin, self)
52✔
218
                      end
219
  end
220

221
  def submitter_or_admin_groups
1✔
222
    submitter_groups | admin_groups
7✔
223
  end
224

225
  def pending_notifications_count
1✔
226
    WorkActivityNotification.where(user_id: id, read_at: nil).count
9✔
227
  end
228

229
  def assign_default_role
1✔
230
    @just_created = true
64✔
231
    add_role(:submitter, default_group) unless has_role?(:submitter, default_group)
64✔
232
    default_group.enable_messages_for(user: self)
64✔
233
  end
234

235
  # Returns true if the user has notification e-mails enabled
236
  # @return [Boolean]
237
  def email_messages_enabled?
1✔
238
    email_messages_enabled
4✔
239
  end
240
end
241
# rubocop:enable Metrics/ClassLength
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