Class: User

Inherits:
Object
  • Object
show all
Includes:
ActiveModel::Model, ActiveModel::SecurePassword
Defined in:
app/models/user.rb

Overview

NOTE: Application has “admin” user only

admin's password can be changed from browser, but user name "admin" can't be changed.
many clients can login at the same time (App has multiple active sessions)
raw password shouldn't be compromised (except default password)
you may find detail at https://github.com/treasure-data/fluentd-ui/pull/34

Constant Summary collapse

ENCRYPTED_PASSWORD_FILE =
Rails.root + "db/#{Rails.env}-user.txt"

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#current_passwordObject

Returns the value of attribute current_password.



15
16
17
# File 'app/models/user.rb', line 15

def current_password
  @current_password
end

#nameObject

Returns the value of attribute name.



15
16
17
# File 'app/models/user.rb', line 15

def name
  @name
end

#passwordObject

Returns the value of attribute password.



15
16
17
# File 'app/models/user.rb', line 15

def password
  @password
end

#password_confirmationObject

Returns the value of attribute password_confirmation.



15
16
17
# File 'app/models/user.rb', line 15

def password_confirmation
  @password_confirmation
end

#password_digestObject



22
23
24
25
26
27
28
29
30
# File 'app/models/user.rb', line 22

def password_digest
  @password_digest ||
    begin
      hash = File.read(ENCRYPTED_PASSWORD_FILE).rstrip
      BCrypt::Password.new(hash) # raise BCrypt::Errors::InvalidHash if hash is invalid
    rescue Errno::ENOENT, BCrypt::Errors::InvalidHash
      BCrypt::Password.create(Settings.default_password, cost: cost)
    end
end

Instance Method Details

#costObject



43
44
45
# File 'app/models/user.rb', line 43

def cost
  Rails.env.test? ? BCrypt::Engine::MIN_COST : BCrypt::Engine.cost
end

#update_attributes(params) ⇒ Object



32
33
34
35
36
37
38
39
40
41
# File 'app/models/user.rb', line 32

def update_attributes(params)
  params.each_pair do |key, value|
    send("#{key}=", value)
  end
  return false unless valid?

  File.open(ENCRYPTED_PASSWORD_FILE, "w") do |f|
    f.write BCrypt::Password.create(password, cost: cost)
  end
end

#valid_current_passwordObject



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

def valid_current_password
  unless authenticate(current_password)
    errors.add(:current_password, :wrong_password)
  end
end