Guest User

Untitled

a guest
Feb 28th, 2018
103
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.57 KB | None | 0 0
  1. class User < ActiveRecord::Base
  2. has_and_belongs_to_many :recipes
  3. has_one :author
  4. validates_uniqueness_of :username
  5.  
  6. def password=(pass)
  7. salt = [Array.new(6){rand(256).chr}.join].pack("m").chomp
  8. self.password_salt, self.password_hash = salt, Digest::SHA256.hexdigest(pass + salt)
  9. end
  10.  
  11. def self.authenticate(username, password)
  12. user = User.find(:first, :conditions => ['username = ?', username])
  13.  
  14. if user.blank? || Digest::SHA256.hexdigest(password + user.password_salt) != user.password_hash
  15. raise "Username or password invalid"
  16. end
  17. user
  18. end
  19. end
Add Comment
Please, Sign In to add comment