Class: User

Inherits:
ApplicationRecord show all
Extended by:
EventfulRecord
Includes:
Role::UserRoleHelper, 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 Role::UserRoleHelper

#grant_role, #remove_role, #role?, #role_names

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

alias_association, convert_labware_to_receptacle_for, find_by_id_or_name, find_by_id_or_name!

Methods included from Squishify

extended

Instance Attribute Details

#passwordObject

Returns the value of attribute password.



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

def password
  @password
end

Class Method Details

.all_administrators_emailsObject

returns emails of all admins



77
78
79
# File 'app/models/user.rb', line 77

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

.encrypt(password, salt) ⇒ Object

Encrypts some data with the salt.



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

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

.find_with_barcode_or_swipecard_code(user_code) ⇒ Object



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

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

.prefixObject



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

def self.prefix
  'ID'
end

.sequencescapeObject



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

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

Instance Method Details

#encrypt(password) ⇒ Object

Encrypts the password with the user salt



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

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

#forget_meObject



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

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

#interesting_studiesObject

User has a relationship by role to these studies



163
164
165
# File 'app/models/user.rb', line 163

def interesting_studies
  Study.of_interest_to(self)
end

#logout_pathObject



94
95
96
# File 'app/models/user.rb', line 94

def logout_path
  configatron.authentication == 'sanger-sso' ? configatron.sso_logout_url.to_s : '/logout'
end

#manager_or_administrator?Boolean

Returns:

  • (Boolean)


130
131
132
# File 'app/models/user.rb', line 130

def manager_or_administrator?
  administrator? || manager?
end

#nameObject



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

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

#name_and_loginObject



118
119
120
# File 'app/models/user.rb', line 118

def 
  "#{first_name} #{last_name} (#{})".strip
end

#name_complete?Boolean

Returns:

  • (Boolean)


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

def name_complete?
  not name_incomplete?
end

#name_incomplete?Boolean

Returns:

  • (Boolean)


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

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

#new_api_key(length = 32) ⇒ Object



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

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

#profile_complete?Boolean

Returns:

  • (Boolean)


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

def profile_complete?
  not profile_incomplete?
end

#profile_incomplete?Boolean

Returns:

  • (Boolean)


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

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

#remember_meObject

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



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

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)


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

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

#sorted_study_names_and_idsObject



126
127
128
# File 'app/models/user.rb', line 126

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

#study_and_project_rolesObject



90
91
92
# File 'app/models/user.rb', line 90

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

#valid_projectsObject



122
123
124
# File 'app/models/user.rb', line 122

def valid_projects
  projects.valid.alphabetical
end