Module: Clearance::App::Models::User::InstanceMethods

Defined in:
lib/clearance/app/models/user.rb

Instance Method Summary collapse

Instance Method Details

#authenticated?(password) ⇒ Boolean

Returns:

  • (Boolean)


45
46
47
# File 'lib/clearance/app/models/user.rb', line 45

def authenticated?(password)
  crypted_password == encrypt(password)
end

#change_password(password_hash) ⇒ Object



90
91
92
93
94
95
96
97
# File 'lib/clearance/app/models/user.rb', line 90

def change_password(password_hash)
  if authenticated?(password_hash[:old_password])
    update_attributes(password_hash.slice(:password, :password_confirmation))
  else
    errors.add(:old_password, "does not match")
    return false
  end
end

#confirm!Object



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

def confirm!
  self.update_attribute :confirmed, true
  update_attribute(:confirmation_code, nil)
end

#encrypt(password) ⇒ Object



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

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

#facebook_user?Boolean

Returns:

  • (Boolean)


99
100
101
# File 'lib/clearance/app/models/user.rb', line 99

def facebook_user?
  not normal_user?
end

#forget_me!Object



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

def forget_me!
  self.update_attribute :remember_token_expires_at, nil
  self.update_attribute :remember_token, nil
end

#generate_confirmation_codeObject



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

def generate_confirmation_code
  update_attribute(:confirmation_code, Digest::SHA1.hexdigest(Time.now.to_s.split(//).sort_by {rand}.join))
end

#generate_reset_password_codeObject



80
81
82
# File 'lib/clearance/app/models/user.rb', line 80

def generate_reset_password_code
  update_attribute(:reset_password_code, Digest::SHA1.hexdigest(Time.now.to_s.split(//).sort_by {rand}.join))
end

#normal_user?Boolean

Returns:

  • (Boolean)


103
104
105
# File 'lib/clearance/app/models/user.rb', line 103

def normal_user?
  self.respond_to?(:facebook_id) ? facebook_id.nil? : true
end

#remember_me!Object



57
58
59
# File 'lib/clearance/app/models/user.rb', line 57

def remember_me!
  remember_me_until 2.weeks.from_now.utc
end

#remember_me_until(time) ⇒ Object



61
62
63
64
# File 'lib/clearance/app/models/user.rb', line 61

def remember_me_until(time)
  self.update_attribute :remember_token_expires_at, time
  self.update_attribute :remember_token, encrypt("#{email}--#{remember_token_expires_at}")
end

#remember_token?Boolean

Returns:

  • (Boolean)


53
54
55
# File 'lib/clearance/app/models/user.rb', line 53

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

#reset_password(password_hash) ⇒ Object



84
85
86
87
88
# File 'lib/clearance/app/models/user.rb', line 84

def reset_password(password_hash)
  if update_attributes(password_hash.slice(:password, :password_confirmation))
    update_attribute(:reset_password_code, nil)
  end
end