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

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


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

def password
  @password
end

Class Method Details

.all_administrators_emailsObject

returns emails of all admins


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

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

.encrypt(password, salt) ⇒ Object

Encrypts some data with the salt.


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

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

.find_with_barcode_or_swipecard_code(user_code) ⇒ Object


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

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

.prefixObject


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

def self.prefix
  'ID'
end

.sequencescapeObject


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

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


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

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

#forget_meObject


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

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


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

def interesting_studies
  Study.of_interest_to(self)
end

#logout_pathObject


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

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

#manager_or_administrator?Boolean

Returns:

  • (Boolean)

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

def manager_or_administrator?
  administrator? || manager?
end

#nameObject


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

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

#name_and_loginObject


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

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

#name_complete?Boolean

Returns:

  • (Boolean)

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

def name_complete?
  not name_incomplete?
end

#name_incomplete?Boolean

Returns:

  • (Boolean)

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

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

#new_api_key(length = 32) ⇒ Object


137
138
139
140
141
# File 'app/models/user.rb', line 137

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

#profile_complete?Boolean

Returns:

  • (Boolean)

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

def profile_complete?
  not profile_incomplete?
end

#profile_incomplete?Boolean

Returns:

  • (Boolean)

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

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


153
154
155
156
157
# File 'app/models/user.rb', line 153

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)

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

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

#sorted_study_names_and_idsObject


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

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

#study_and_project_rolesObject


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

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

#valid_projectsObject


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

def valid_projects
  projects.valid.alphabetical
end