X-Git-Url: https://git.openstreetmap.org./rails.git/blobdiff_plain/88297a682cd76ab9290cd8bf278424bf5c8458e5..84f3668fc4c49f4d3d6faa89990aed5e03b8592d:/app/models/user.rb?ds=inline diff --git a/app/models/user.rb b/app/models/user.rb index 72752078e..589546e2d 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -3,20 +3,41 @@ require 'digest/md5' class User < ActiveRecord::Base has_many :traces - validates_confirmation_of :pass_crypt + validates_confirmation_of :pass_crypt, :message => 'Password must match the confirmation password' + validates_uniqueness_of :display_name + validates_uniqueness_of :email + validates_length_of :pass_crypt, :minimum => 8 + validates_length_of :display_name, :minimum => 3 + validates_format_of :email, :with => /^([^@\s]+)@((?:[-a-z0-9]+\.)+[a-z]{2,})$/i -# def password=(str) -# write_attribute("pass_crypt", Digest::MD5.hexdigest(str)) -# end + def set_defaults + self.creation_time = Time.now + self.timeout = Time.now + self.token = make_token() + end + + def pass_crypt=(str) + write_attribute("pass_crypt", Digest::MD5.hexdigest(str)) + end + def pass_crypt_confirmation=(str) + write_attribute("pass_crypt_confirm", Digest::MD5.hexdigest(str)) + end -# def password -# return self.pass_crypt -# end + def self.authenticate(email, passwd) + find_first([ "email = ? AND pass_crypt =?", email, Digest::MD5.hexdigest(passwd) ]) + end + + private + def make_token + chars = 'abcdefghijklmnopqrtuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789' + confirmstring = '' + + 30.times do + confirmstring += chars[(rand * chars.length).to_i].chr + end + + return confirmstring + end -# def self.authenticate(username, passwd) -# find_first([ "display_name = ? AND pass_crypt =?", -# username, -# Digest::MD5.hexdigest(passwd) ]) -# end end