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

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.


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

def password
  @password
end

Class Method Details

.all_administrators_emailsObject

returns emails of all admins


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

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

.encrypt(password, salt) ⇒ Object

Encrypts some data with the salt.


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

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

.find_with_barcode_or_swipecard_code(user_code) ⇒ Object


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

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

.prefixObject


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

def self.prefix
  'ID'
end

.sequencescapeObject


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

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


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

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

#forget_meObject


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

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


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

def interesting_studies
  Study.of_interest_to(self)
end

#logout_pathObject


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

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

#manager_or_administrator?Boolean

Returns:

  • (Boolean)

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

def manager_or_administrator?
  administrator? || manager?
end

#nameObject


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

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

#name_and_loginObject


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

def 
  "#{first_name} #{last_name} (#{})".strip
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? || last_name.blank?
end

#new_api_key(length = 32) ⇒ Object


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

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)

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? || email.blank? || swipecard_code.blank?
end

#remember_meObject

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


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

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)

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

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

#sorted_study_names_and_idsObject


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

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

#study_and_project_rolesObject


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

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

#valid_projectsObject


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

def valid_projects
  projects.valid.alphabetical
end