Class: User

Inherits:
ApplicationRecord show all
Defined in:
app/models/user.rb

Constant Summary collapse

@@per_page =
25

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#passwordObject

Virtual attribute for the unencrypted password



6
7
8
# File 'app/models/user.rb', line 6

def password
  @password
end

Class Method Details

.authenticate(login, pass) ⇒ Object



132
133
134
135
136
137
138
139
140
141
142
143
# File 'app/models/user.rb', line 132

def self.authenticate(, pass)
  return nil if .blank?
  candidate = where("login = ?", ).first
  return nil if candidate.nil?

  if Tracks::Config.auth_schemes.include?('database')

    return candidate if (candidate.auth_type == 'database' && candidate.password_matches?(pass))
  end

  return nil
end

.find_adminObject



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

def self.find_admin
  where(:is_admin => true).first
end

.no_users_yet?Boolean

Returns:

  • (Boolean)


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

def self.no_users_yet?
  count == 0
end

Instance Method Details

#change_password(pass, pass_confirm) ⇒ Object



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

def change_password(pass, pass_confirm)
  self.password = pass
  self.password_confirmation = pass_confirm
  save!
end

#create_hash(s) ⇒ Object



203
204
205
# File 'app/models/user.rb', line 203

def create_hash(s)
  BCrypt::Password.create(s)
end

#crypt_passwordObject (protected)



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

def crypt_password
  return if password.blank?
  write_attribute("crypted_password", create_hash(password)) if password == password_confirmation
end

#dateObject



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

def date
  Date.current
end

#delete_taggingsObject (protected)



224
225
226
227
228
# File 'app/models/user.rb', line 224

def delete_taggings
  ids = todos.pluck(:id)
  taggings = Tagging.where(taggable_id: ids).pluck(:id)
  Tagging.where(id: taggings).delete_all
end

#destroy_dependenciesObject (protected)



218
219
220
221
222
# File 'app/models/user.rb', line 218

def destroy_dependencies
  ids = todos.pluck(:id)
  pred_deps = Dependency.where(predecessor_id: ids).destroy_all
  succ_deps = Dependency.where(predecessor_id: ids).destroy_all
end

#display_nameObject



157
158
159
160
161
162
163
164
165
166
# File 'app/models/user.rb', line 157

def display_name
  if first_name.blank? && last_name.blank?
    return 
  elsif first_name.blank?
    return last_name
  elsif last_name.blank?
    return first_name
  end
  "#{first_name} #{last_name}"
end

#forget_meObject



193
194
195
196
197
# File 'app/models/user.rb', line 193

def forget_me
  self.remember_token_expires_at = nil
  self.remember_token            = nil
  save
end

#generate_tokenObject



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

def generate_token
  self.token = Digest::SHA1.hexdigest "#{Time.zone.now.to_i}#{rand}"
end

#password_matches?(pass) ⇒ Boolean

Returns:

  • (Boolean)


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

def password_matches?(pass)
  BCrypt::Password.new(crypted_password) == pass
end

#password_required?Boolean (protected)

Returns:

  • (Boolean)


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

def password_required?
  auth_type == 'database' && crypted_password.blank? || password.present?
end

#remember_meObject

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



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

def remember_me
  self.remember_token_expires_at = 2.weeks.from_now.utc
  self.remember_token ||= Digest::SHA1.hexdigest("#{}--#{remember_token_expires_at}")
  save
end

#remember_token?Boolean

Returns:

  • (Boolean)


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

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

#to_paramObject



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

def to_param
  
end

#validate_auth_typeObject



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

def validate_auth_type
  unless Tracks::Config.auth_schemes.include?(auth_type)
    errors.add("auth_type", "not a valid authentication type (#{auth_type})")
  end
end