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

pulibrary / pdc_describe / 64e42f72-7d76-486a-b44a-a33c65efaa6e

pending completion
64e42f72-7d76-486a-b44a-a33c65efaa6e

Pull #1098

circleci

jrgriffiniii
Renaming the Collection class to the Group class; Ensuring that legacy table names are preserved
Pull Request #1098: Renaming the Collection class to the Group class and ensures that legacy table names are preserved

114 of 114 new or added lines in 12 files covered. (100.0%)

1899 of 2157 relevant lines covered (88.04%)

161.82 hits per line

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

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

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

10
  devise :rememberable, :omniauthable
×
11

12
  # CollectionOptions model extensible options set for Collections and Users
13
  has_many :collection_options, dependent: :destroy
×
14
  has_many :collection_messaging_options, -> { where(option_type: CollectionOption::EMAIL_MESSAGES) }, class_name: "CollectionOption", dependent: :destroy
×
15

16
  has_many :collections_with_options, through: :collection_options, source: :group
×
17
  has_many :collections_with_messaging, through: :collection_messaging_options, source: :group
×
18

19
  after_create :assign_default_role
×
20

21
  attr_accessor :just_created
×
22

23
  def default_group_id=(value)
×
24
    self.default_collection_id = value
×
25
  end
×
26

27
  def default_group_id
×
28
    default_collection_id
×
29
  end
×
30

31
  validate do |user|
×
32
    user.orcid&.strip!
×
33
    if user.orcid.present? && Orcid.invalid?(user.orcid)
×
34
      user.errors.add :base, "Invalid format for ORCID"
×
35
    end
×
36
  end
×
37

38
  def self.from_cas(access_token)
×
39
    user = User.find_by(uid: access_token.uid)
×
40
    if user.nil?
×
41
      user = new_from_cas(access_token)
×
42
    elsif user.provider.blank?
×
43
      user.update_with_cas(access_token)
×
44
    end
×
45
    user
×
46
  end
×
47

48
  # Create a new user with some basic information from CAS.
49
  def self.new_from_cas(access_token)
×
50
    user = User.new
×
51
    user.provider = access_token.provider
×
52
    user.uid = access_token.uid # this is the netid
×
53
    user.email = access_token.extra.mail
×
54
    user.display_name = access_token.extra.givenname || access_token.uid # Harriet
×
55
    user.family_name = access_token.extra.sn || access_token.uid # Tubman
×
56
    user.full_name = access_token.extra.displayname || access_token.uid # "Harriet Tubman"
×
57
    user.default_group_id = Group.default_for_department(access_token.extra.departmentnumber)&.id
×
58
    user.save!
×
59
    user
×
60
  end
×
61

62
  # Updates an existing User record with some information from CAS. This is useful
63
  # for records created before the user ever logged in (e.g. to gran them permissions
64
  # to groups).
65
  def update_with_cas(access_token)
×
66
    self.provider = access_token.provider
×
67
    self.email = access_token.extra.mail
×
68
    self.display_name = access_token.extra.givenname || access_token.uid # Harriet
×
69
    self.family_name = access_token.extra.sn || access_token.uid # Tubman
×
70
    self.full_name = access_token.extra.displayname || access_token.uid # "Harriet Tubman"
×
71
    self.default_group_id = Group.default_for_department(access_token.extra.departmentnumber)&.id
×
72
    save!
×
73
  end
×
74

75
  # Creates a new user by uid. If the user already exists it returns the existing user.
76
  def self.new_for_uid(uid)
×
77
    user = User.find_by(uid: uid)
×
78
    if user.nil?
×
79
      user = User.new(uid: uid, email: "#{uid}@princeton.edu")
×
80
      user.save!
×
81
    end
×
82
    user
×
83
  end
×
84

85
  def self.new_super_admin(uid)
×
86
    user = new_for_uid(uid)
×
87
    user.add_role(:super_admin) unless user.has_role?(:super_admin)
×
88
    user.add_role(:group_admin) unless user.has_role?(:group_admin)
×
89
    user
×
90
  end
×
91

92
  # rubocop:disable Metrics/MethodLength
93
  def self.new_from_csv_params(csv_params)
×
94
    email = "#{csv_params['Net ID']}@princeton.edu"
×
95
    uid = csv_params["Net ID"]
×
96
    full_name = "#{csv_params['First Name']} #{csv_params['Last Name']}"
×
97
    display_name = csv_params["First Name"]
×
98
    orcid = csv_params["ORCID ID"]
×
99
    user = User.where(email: email).first_or_create
×
100
    params_hash = {
×
101
      email: email,
×
102
      uid: uid,
×
103
      orcid: orcid,
×
104
      full_name: (full_name if user.full_name.blank?),
×
105
      display_name: (display_name if user.display_name.blank?)
×
106
    }.compact
×
107

108
    user.update(params_hash)
×
109
    Rails.logger.info "Successfully created or updated #{user.email}"
×
110
    user
×
111
  end
×
112
  # rubocop:enable Metrics/MethodLength
113

114
  def self.create_users_from_csv(csv)
×
115
    users = []
×
116
    CSV.foreach(csv, headers: true) do |row|
×
117
      next if row["Net ID"] == "N/A"
×
118
      users << new_from_csv_params(row.to_hash)
×
119
    end
×
120
    users
×
121
  end
×
122

123
  # Creates the default users as indicated in the super_admin config file
124
  # and the default administrators and submitters for each group.
125
  # It only creates missing records, i.e. if the records already exist it
126
  # will not create a duplicate. It also does _not_ remove already configured
127
  # access to other groups.
128
  def self.create_default_users
×
129
    update_super_admins
×
130

131
    Group.find_each do |group|
×
132
      Rails.logger.info "Setting up admins for group #{group.title}"
×
133
      group.default_admins_list.each do |uid|
×
134
        user = User.new_for_uid(uid)
×
135
        user.add_role :group_admin, group
×
136
      end
×
137

138
      Rails.logger.info "Setting up submitters for group #{group.title}"
×
139
      group.default_submitters_list.each do |uid|
×
140
        user = User.new_for_uid(uid)
×
141
        user.add_role :submitter, group
×
142
      end
×
143
    end
×
144
  end
×
145

146
  def self.update_super_admins
×
147
    Rails.logger.info "Setting super administrators"
×
148
    Rails.configuration.super_admins.each do |uid|
×
149
      new_super_admin(uid)
×
150
    end
×
151
  end
×
152

153
  # Returns a string with the UID (netid) for all the users.
154
  # We use this string to power the JavaScript @mention functionality when adding messages to works.
155
  def self.all_uids_string
×
156
    User.all.map { |user| '"' + user.uid + '"' }.join(", ")
×
157
  end
×
158

159
  ##
160
  # Is this user a super_admin? super_admins automatically get admin status in every
161
  # group, and they can make new groups.
162
  # @return [Boolean]
163
  def super_admin?
×
164
    has_role? :super_admin
×
165
  rescue => ex
×
166
    Rails.logger.error("Unexpected error checking super_admin: #{ex}")
×
167
    false
×
168
  end
×
169

170
  # Returns a display name that always has a value
171
  # This is needed because we have records in the Users table that are created automatically
172
  # in which the only value we have for sure its their uid (aka NetID).
173
  def display_name_safe
×
174
    display_name.presence || uid
×
175
  end
×
176

177
  def moderator?
×
178
    admin_groups.count > 0
×
179
  end
×
180

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

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

196
  # Returns true if the user can admin the group
197
  def can_admin?(group)
×
198
    return true if super_admin?
×
199
    has_role? :group_admin, group
×
200
  end
×
201

202
  # Returns the list of groups where the user can submit datasets
203
  def submitter_groups
×
204
    @submitter_groups = if super_admin?
×
205
                          Group.all.to_a
×
206
                        else
×
207
                          Group.with_role(:submitter, self)
×
208
                        end
×
209
  end
×
210

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

220
  def submitter_or_admin_groups
×
221
    submitter_groups | admin_groups
×
222
  end
×
223

224
  def pending_notifications_count
×
225
    WorkActivityNotification.where(user_id: id, read_at: nil).count
×
226
  end
×
227

228
  def assign_default_role
×
229
    @just_created = true
×
230
    add_role(:submitter, default_group) unless has_role?(:submitter, default_group)
×
231
    enable_messages_from(group: default_group)
×
232
  end
×
233

234
  # Returns true if the user has notification e-mails enabled
235
  # @return [Boolean]
236
  def email_messages_enabled?
×
237
    email_messages_enabled
×
238
  end
×
239

240
  # Permit this user to receive notification messages for members of a given Group
241
  # @param group [Group]
242
  def enable_messages_from(group:)
×
243
    raise(ArgumentError, "User #{uid} is not an administrator or depositor for the group #{group.title}") unless can_admin?(group) || can_submit?(group)
×
244
    collection_messaging_options << CollectionOption.new(option_type: CollectionOption::EMAIL_MESSAGES, user: self, group: group)
×
245
  end
×
246

247
  # Disable this user from receiving notification messages for members of a given Group
248
  # @param group [Group]
249
  def disable_messages_from(group:)
×
250
    raise(ArgumentError, "User #{uid} is not an administrator or depositor for the group #{group.title}") unless can_admin?(group) || can_submit?(group)
×
251
    collections_with_messaging.destroy(group)
×
252
  end
×
253

254
  # Returns true if the user has notification e-mails enabled for a given group
255
  # @param group [Group]
256
  # @return [Boolean]
257
  def messages_enabled_from?(group:)
×
258
    found = collection_messaging_options.find_by(group: group)
×
259

260
    !found.nil?
×
261
  end
×
262
end
×
263
# 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