Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- require 'digest/sha2'
- class User < ActiveRecord::Base
- before_save :encrypt_password
- validates_length_of :username, :within => 3..100
- validates_presence_of :password, :on => :create
- validates_uniqueness_of :username
- validates_confirmation_of :password
- validates_length_of :first_name, :maximum => 100, :allow_nil => true
- validates_length_of :last_name, :maximum => 100, :allow_nil => true
- validates_length_of :email, :maximum => 100, :allow_nil => true
- def self.login(username, password)
- return nil if password.empty?
- find(:first, :conditions => ['username = ? AND password = ? AND active=1', username, Digest::SHA256.hexdigest(password)] )
- end
- def password=(val)
- self[:password] = val unless val.blank?
- end
- private
- def encrypt_password
- unless self.password.nil? || self.password.length == 64
- self.password = Digest::SHA256.hexdigest(self.password)
- end
- end
- end
Add Comment
Please, Sign In to add comment