Guest User

Untitled

a guest
Feb 20th, 2018
56
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.37 KB | None | 0 0
  1. class Individual < ActiveRecord::Base
  2. belongs_to :title
  3. before_save :set_user_data
  4.  
  5. # validates_length_of :login, :within => 3..20
  6. # validates_length_of :password, :within => 8..40
  7. # validates_presence_of :login, :email, :password, :password_confirmation, :salt
  8. validates_uniqueness_of :username#, :scope => 'username', :message => "Username already in use."
  9. # validates_confirmation_of :password
  10. # validates_format_of :email, :with => /^([^@\s]+)@((?:[-a-z0-9]+\.)+[a-z]{2,})$/i, :message => "Invalid email"
  11.  
  12.  
  13. def set_user_data
  14. salt = Time.now.strftime("%Y-%m-%d %H:%M:%S")
  15. self.birth_month = self.date_of_birth.month
  16. self.birth_day = self.date_of_birth.day
  17. self.created_on = salt
  18. newpassword = random_string(8)
  19. self.salutation = newpassword
  20. newpassword = Digest::SHA1.hexdigest(newpassword+salt)
  21. self.password = newpassword
  22. end
  23.  
  24. attr_protected :individual_id
  25.  
  26. def random_string(len)
  27. #generate a random password consisting of strings and digits
  28. chars = ("a".."z").to_a + ("A".."Z").to_a + ("0".."9").to_a
  29. newpass = ""
  30. 1.upto(len) { |i| newpass << chars[rand(chars.size-1)] }
  31. return newpass
  32. end
  33.  
  34. def self.authenticate(login, pass)
  35. u=find(:first, :conditions=>["login = ?", login])
  36. return nil if u.nil?
  37. return u if User.encrypt(pass, u.salt)==u.hashed_password
  38. nil
  39. end
Add Comment
Please, Sign In to add comment