Guest User

Untitled

a guest
Feb 20th, 2018
87
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.11 KB | None | 0 0
  1. require 'digest/sha2'
  2. class User < ActiveRecord::Base
  3. validates_presence_of :username, :first_name, :last_name, :email
  4. validates_length_of :username, :within => 6..20, :too_long => "pick a shorter username", :too_short => "pick a longer username"
  5. validates_uniqueness_of :username, :message => "Another user by the same username exists please try another username"
  6.  
  7. validates_format_of :email, :with => /^([^@\s]+)@((?:[-a-z0-9]+\.)+[a-z]{2,})$/i, :on => :create
  8.  
  9. has_many :tasks
  10.  
  11. def self.authenticate(username, password)
  12. find(:first, :conditions => ["username = ? and password_hash = ?", username, Digest::SHA2.hexdigest(password + self.password_salt)])
  13. end
  14.  
  15. def password=(pass)
  16. salt = [Array.new(6){rand(256).chr}.join].pack("m").chomp
  17. write_attribute("password_hash", Digest::SHA256.hexdigest(pass + salt))
  18. write_attribute("password_salt", salt)
  19. #self.password_salt, self.password_hash = salt, Digest::SHA256.hexdigest(pass + salt)
  20. end
  21.  
  22. def password_is?(pass)
  23. Digest::SHA2.hexdigest(pass + self.password_salt) == self.password_hash
  24. end
  25. end
Add Comment
Please, Sign In to add comment