Advertisement
Guest User

Untitled

a guest
May 31st, 2017
110
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Ruby 0.67 KB | None | 0 0
  1. require 'dm'
  2.  
  3. class User  
  4.  
  5.  
  6.   attr_accessor :password
  7.   attr_accessor :password_confirmation
  8.  
  9.   def after_create
  10.     self.crypted_password = encrypt(password)
  11.     @new = false
  12.     save
  13.   end
  14.  
  15.   def authenticated?(password)
  16.       crypted_password == encrypt(password)
  17.   end
  18.  
  19.   def encrypt(password)
  20.     self.class.encrypt(password, salt)
  21.   end
  22.  
  23.   def self.encrypt(password, salt)
  24.     Digest::SHA1.hexdigest("--#{salt}--#{password}--")
  25.   end
  26.  
  27.   def self.authenticate(hash)
  28.     email, pass = hash['login'], hash['password']
  29.  
  30.     if user = User[:email => email]
  31.       return user unless pass
  32.       user if user.authenticated?(pass)
  33.     end
  34.   end
  35.  
  36. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement