Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class Individual < ActiveRecord::Base
- belongs_to :title
- before_save :set_user_data
- # validates_length_of :login, :within => 3..20
- # validates_length_of :password, :within => 8..40
- # validates_presence_of :login, :email, :password, :password_confirmation, :salt
- validates_uniqueness_of :username#, :scope => 'username', :message => "Username already in use."
- # validates_confirmation_of :password
- # validates_format_of :email, :with => /^([^@\s]+)@((?:[-a-z0-9]+\.)+[a-z]{2,})$/i, :message => "Invalid email"
- def set_user_data
- salt = Time.now.strftime("%Y-%m-%d %H:%M:%S")
- self.birth_month = self.date_of_birth.month
- self.birth_day = self.date_of_birth.day
- self.created_on = salt
- newpassword = random_string(8)
- self.salutation = newpassword
- newpassword = Digest::SHA1.hexdigest(newpassword+salt)
- self.password = newpassword
- end
- attr_protected :individual_id
- def random_string(len)
- #generate a random password consisting of strings and digits
- chars = ("a".."z").to_a + ("A".."Z").to_a + ("0".."9").to_a
- newpass = ""
- 1.upto(len) { |i| newpass << chars[rand(chars.size-1)] }
- return newpass
- end
- def self.authenticate(login, pass)
- u=find(:first, :conditions=>["login = ?", login])
- return nil if u.nil?
- return u if User.encrypt(pass, u.salt)==u.hashed_password
- nil
- end
Add Comment
Please, Sign In to add comment