Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- require 'digest/sha2'
- class User < ActiveRecord::Base
- validates_presence_of :username, :first_name, :last_name, :email
- validates_length_of :username, :within => 6..20, :too_long => "pick a shorter username", :too_short => "pick a longer username"
- validates_uniqueness_of :username, :message => "Another user by the same username exists please try another username"
- validates_format_of :email, :with => /^([^@\s]+)@((?:[-a-z0-9]+\.)+[a-z]{2,})$/i, :on => :create
- has_many :tasks
- def self.authenticate(username, password)
- find(:first, :conditions => ["username = ? and password_hash = ?", username, Digest::SHA2.hexdigest(password + self.password_salt)])
- end
- def password=(pass)
- salt = [Array.new(6){rand(256).chr}.join].pack("m").chomp
- write_attribute("password_hash", Digest::SHA256.hexdigest(pass + salt))
- write_attribute("password_salt", salt)
- #self.password_salt, self.password_hash = salt, Digest::SHA256.hexdigest(pass + salt)
- end
- def password_is?(pass)
- Digest::SHA2.hexdigest(pass + self.password_salt) == self.password_hash
- end
- end
Add Comment
Please, Sign In to add comment