Guest User

Untitled

a guest
Apr 12th, 2018
106
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.94 KB | None | 0 0
  1. require 'digest/sha2'
  2.  
  3. class User < ActiveRecord::Base
  4. before_save :encrypt_password
  5.  
  6. validates_length_of :username, :within => 3..100
  7. validates_presence_of :password, :on => :create
  8. validates_uniqueness_of :username
  9. validates_confirmation_of :password
  10. validates_length_of :first_name, :maximum => 100, :allow_nil => true
  11. validates_length_of :last_name, :maximum => 100, :allow_nil => true
  12. validates_length_of :email, :maximum => 100, :allow_nil => true
  13.  
  14. def self.login(username, password)
  15. return nil if password.empty?
  16. find(:first, :conditions => ['username = ? AND password = ? AND active=1', username, Digest::SHA256.hexdigest(password)] )
  17. end
  18.  
  19. def password=(val)
  20. self[:password] = val unless val.blank?
  21. end
  22.  
  23. private
  24. def encrypt_password
  25. unless self.password.nil? || self.password.length == 64
  26. self.password = Digest::SHA256.hexdigest(self.password)
  27. end
  28. end
  29.  
  30. end
Add Comment
Please, Sign In to add comment