Class: User

Inherits:
ApplicationRecord show all
Extended by:
EventfulRecord
Includes:
Swipecardable, Authentication, Uuid::Uuidable
Defined in:
app/models/user.rb

Overview

Represents Sequencescape users, used to regulate login as well as provide tracking of who did what. While most users are internal, some are external.

Defined Under Namespace

Modules: Authentication

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from EventfulRecord

has_many_events, has_many_lab_events, has_one_event_with_family

Methods included from Swipecardable

#compare_swipecard_code, included, #swipecard_code, #swipecard_code=, #swipecard_code?

Methods included from Uuid::Uuidable

included, #unsaved_uuid!, #uuid

Methods included from Authentication

#authenticated?, included

Methods inherited from ApplicationRecord

convert_labware_to_receptacle_for, find_by_id_or_name, find_by_id_or_name!

Methods included from Warren::BroadcastMessages

#broadcast, included, #queue_associated_for_broadcast, #queue_for_broadcast, #warren

Instance Attribute Details

#passwordObject

Returns the value of attribute password


47
48
49
# File 'app/models/user.rb', line 47

def password
  @password
end

Class Method Details

.all_administrators_emailsObject

returns emails of all admins


58
59
60
# File 'app/models/user.rb', line 58

def self.all_administrators_emails
  all_administrators.uniq.pluck(:email).compact
end

.encrypt(password, salt) ⇒ Object

Encrypts some data with the salt.


63
64
65
# File 'app/models/user.rb', line 63

def self.encrypt(password, salt)
  Digest::SHA1.hexdigest("--#{salt}--#{password}--")
end

.find_with_barcode_or_swipecard_code(user_code) ⇒ Object


53
54
55
# File 'app/models/user.rb', line 53

def self.find_with_barcode_or_swipecard_code(user_code)
  with_user_code(user_code).first
end

.prefixObject


49
50
51
# File 'app/models/user.rb', line 49

def self.prefix
  'ID'
end

.sequencescapeObject


67
68
69
# File 'app/models/user.rb', line 67

def self.sequencescape
  find_or_create_by!(login: configatron.sequencescape_email, email: configatron.sequencescape_email)
end

Instance Method Details

#administrator?Boolean

Returns:

  • (Boolean)

200
201
202
# File 'app/models/user.rb', line 200

def administrator?
  is_administrator?
end

#authorized_projectsObject


128
129
130
# File 'app/models/user.rb', line 128

def authorized_projects
  Project.for_user(self)
end

#data_access_coordinator?Boolean

Returns:

  • (Boolean)

188
189
190
# File 'app/models/user.rb', line 188

def data_access_coordinator?
  has_role? 'data_access_coordinator'
end

#encrypt(password) ⇒ Object

Encrypts the password with the user salt


211
212
213
# File 'app/models/user.rb', line 211

def encrypt(password)
  self.class.encrypt(password, salt)
end

#following?(item) ⇒ Boolean

Returns:

  • (Boolean)

87
88
89
# File 'app/models/user.rb', line 87

def following?(item)
  has_role? 'follower', item
end

#forget_meObject


226
227
228
229
230
# File 'app/models/user.rb', line 226

def forget_me
  self.remember_token_expires_at = nil
  self.remember_token            = nil
  save(validate: false)
end

#has_preference_for(key) ⇒ Object


148
149
150
# File 'app/models/user.rb', line 148

def has_preference_for(key)
  setting_for?(key)
end

#interesting_studiesObject

User has a relationship by role to these studies


233
234
235
# File 'app/models/user.rb', line 233

def interesting_studies
  Study.of_interest_to(self)
end

#internal?Boolean

Returns:

  • (Boolean)

156
157
158
# File 'app/models/user.rb', line 156

def internal?
  has_role? 'internal'
end

#lab?Boolean

Returns:

  • (Boolean)

178
179
180
# File 'app/models/user.rb', line 178

def lab?
  has_role? 'lab'
end

#lab_manager?Boolean

Returns:

  • (Boolean)

164
165
166
167
168
# File 'app/models/user.rb', line 164

def lab_manager?
  return @lab_manager if instance_variable_defined?('@lab_manager')

  @lab_manager = has_role? 'lab_manager'
end

#logout_pathObject


91
92
93
94
95
96
97
# File 'app/models/user.rb', line 91

def logout_path
  if configatron.authentication == 'sanger-sso'
    (configatron.sso_logout_url).to_s
  else
    '/logout'
  end
end

#manager?Boolean

Returns:

  • (Boolean)

196
197
198
# File 'app/models/user.rb', line 196

def manager?
  is_manager?
end

#manager_or_administrator?Boolean

Returns:

  • (Boolean)

192
193
194
# File 'app/models/user.rb', line 192

def manager_or_administrator?
  is_administrator? || is_manager?
end

#nameObject


115
116
117
# File 'app/models/user.rb', line 115

def name
  name_incomplete? ?  : "#{first_name} #{last_name}"
end

#name_complete?Boolean

Returns:

  • (Boolean)

111
112
113
# File 'app/models/user.rb', line 111

def name_complete?
  not name_incomplete?
end

#name_incomplete?Boolean

Returns:

  • (Boolean)

107
108
109
# File 'app/models/user.rb', line 107

def name_incomplete?
  first_name.blank? or last_name.blank?
end

#new_api_key(length = 32) ⇒ Object


204
205
206
207
208
# File 'app/models/user.rb', line 204

def new_api_key(length = 32)
  u = Digest::SHA1.hexdigest()[0..12]
  k = Digest::SHA1.hexdigest(Time.now.to_s + rand(12341234).to_s)[1..length]
  self.api_key = "#{u}-#{k}"
end

#owner?(item) ⇒ Boolean

Returns:

  • (Boolean)

182
183
184
185
186
# File 'app/models/user.rb', line 182

def owner?(item)
  return false if item.nil?

  has_role? 'owner', item
end

#privileged?(item = nil) ⇒ Boolean

Returns:

  • (Boolean)

152
153
154
# File 'app/models/user.rb', line 152

def privileged?(item = nil)
  manager_or_administrator? || owner?(item)
end

#profile_complete?Boolean

Returns:

  • (Boolean)

103
104
105
# File 'app/models/user.rb', line 103

def profile_complete?
  not profile_incomplete?
end

#profile_incomplete?Boolean

Returns:

  • (Boolean)

99
100
101
# File 'app/models/user.rb', line 99

def profile_incomplete?
  name_incomplete? or email.blank? or swipecard_code.blank?
end

#project_rolesObject


75
76
77
# File 'app/models/user.rb', line 75

def project_roles
  user_roles('Project')
end

#projectsObject


119
120
121
122
123
124
125
126
# File 'app/models/user.rb', line 119

def projects
  return Project.all if is_administrator?

  atuhorized = authorized_projects
  return Project.all if ((atuhorized.blank?) && (privileged?))

  atuhorized
end

#qa_manager?Boolean

Returns:

  • (Boolean)

160
161
162
# File 'app/models/user.rb', line 160

def qa_manager?
  has_role? 'qa_manager'
end

#remember_meObject

These create and unset the fields required for remembering users between browser closes


220
221
222
223
224
# File 'app/models/user.rb', line 220

def remember_me
  self.remember_token_expires_at = 2.weeks.from_now.utc
  self.remember_token            = encrypt("#{email}--#{remember_token_expires_at}")
  save(validate: false)
end

#remember_token?Boolean

Returns:

  • (Boolean)

215
216
217
# File 'app/models/user.rb', line 215

def remember_token?
  remember_token_expires_at && Time.now.utc < remember_token_expires_at
end

#slf_gel?Boolean

Returns:

  • (Boolean)

174
175
176
# File 'app/models/user.rb', line 174

def slf_gel?
  has_role? 'slf_gel'
end

#slf_manager?Boolean

Returns:

  • (Boolean)

170
171
172
# File 'app/models/user.rb', line 170

def slf_manager?
  has_role? 'slf_manager'
end

#sorted_project_names_and_idsObject


132
133
134
# File 'app/models/user.rb', line 132

def sorted_project_names_and_ids
  projects.alphabetical.pluck(:name, :id)
end

#sorted_study_names_and_idsObject


144
145
146
# File 'app/models/user.rb', line 144

def sorted_study_names_and_ids
  interesting_studies.alphabetical.pluck(:name, :id)
end

#sorted_valid_project_names_and_idsObject


136
137
138
# File 'app/models/user.rb', line 136

def sorted_valid_project_names_and_ids
  valid_projects.pluck(:name, :id)
end

#study_and_project_rolesObject


79
80
81
# File 'app/models/user.rb', line 79

def study_and_project_roles
  roles.where(authorizable_type: %w[Study Project])
end

#study_rolesObject


71
72
73
# File 'app/models/user.rb', line 71

def study_roles
  user_roles('Study')
end

#user_roles(authorizable_class_name) ⇒ Object


83
84
85
# File 'app/models/user.rb', line 83

def user_roles(authorizable_class_name)
  roles.where(authorizable_type: authorizable_class_name)
end

#valid_projectsObject


140
141
142
# File 'app/models/user.rb', line 140

def valid_projects
  projects.valid.alphabetical
end