Module: UserAuthModel::InstanceMethods

Defined in:
lib/user_auth_model.rb

Instance Method Summary collapse

Instance Method Details

#change_password(password_new) ⇒ Object



37
38
39
40
41
# File 'lib/user_auth_model.rb', line 37

def change_password password_new
  pp self
  pp self.args[:salt]
  self.args[:password] = encrypt(password_new, self.salt)
end

#encrypt(password, salt = nil) ⇒ Object



43
44
45
46
47
48
49
# File 'lib/user_auth_model.rb', line 43

def encrypt(password, salt = nil)
  if salt
    Digest::SHA1.hexdigest(password+salt)
  else
    Digest::SHA1.hexdigest(password+self.salt)
  end
end

#forgot_passwordObject



32
33
34
35
# File 'lib/user_auth_model.rb', line 32

def forgot_password
  self.reset_token = random_string(30)
  save
end

#initialize(args =  {}) ⇒ Object



25
26
27
28
29
30
# File 'lib/user_auth_model.rb', line 25

def initialize(args = {})
  args.symbolize_keys!
  args[:salt] = random_string(10) if !args[:salt] && args[:password_new]
  args[:password] = encrypt(args[:password_new], args[:salt]) if args[:password_new]
  super(args)
end

#random_string(len) ⇒ Object



51
52
53
54
55
56
57
# File 'lib/user_auth_model.rb', line 51

def random_string(len)
  #generate a random password consisting of strings and digits
  chars = ("a".."z").to_a + ("A".."Z").to_a + ("0".."9").to_a
  newpass = ""
  1.upto(len) { |i| newpass << chars[rand(chars.size-1)] }
  return newpass
end