diff --git a/app/models/user.rb b/app/models/user.rb --- a/app/models/user.rb +++ b/app/models/user.rb @@ -19,7 +19,7 @@ belongs_to :site belongs_to :country - named_scope :activated, :conditions => {:activated => true} + named_scope :activated_users, :conditions => {:activated => true} validates_presence_of :login validates_uniqueness_of :login @@ -36,6 +36,7 @@ validates_format_of :email, :with => /\A([^@\s]+)@((?:[-a-z0-9]+\.)+[a-z]{2,})\Z/i, :allow_blank => true validate :uniqueness_of_email_from_activated_users + validate :enough_time_interval_between_same_email_registrations attr_accessor :password @@ -87,6 +88,9 @@ end def activation_key + if self.hashed_password==nil + encrypt_new_password + end Digest::SHA1.hexdigest(self.hashed_password)[0..7] end @@ -117,8 +121,18 @@ end def uniqueness_of_email_from_activated_users - if User.activated.find_by_email(self.email)!=nil + user = User.activated_users.find_by_email(self.email) + if user and (user.login != self.login) self.errors.add_to_base("Email has already been taken") end end + + def enough_time_interval_between_same_email_registrations + open_user = User.find_by_email(self.email, + :order => 'created_at DESC') + if open_user and open_user.created_at and + (open_user.created_at > Time.now.gmtime - 5.minutes) + self.errors.add_to_base("There are already unactivated registrations with this e-mail address (please wait for 5 minutes)") + end + end end