Commit a6b5dfe60d1ecd1e4025467ee0cfdcf7886232fe

Authored by Braulio Bhavamitra
1 parent b5ecb6a4

Normalize email before save

app/models/user.rb
... ... @@ -98,6 +98,7 @@ class User < ActiveRecord::Base
98 98 validates_length_of :email, :within => 3..100, :if => (lambda {|user| !user.email.blank?})
99 99 validates_uniqueness_of :login, :email, :case_sensitive => false, :scope => :environment_id
100 100 before_save :encrypt_password
  101 + before_save :normalize_email
101 102 validates_format_of :email, :with => Noosfero::Constants::EMAIL_FORMAT, :if => (lambda {|user| !user.email.blank?})
102 103  
103 104 validates_inclusion_of :terms_accepted, :in => [ '1' ], :if => lambda { |u| ! u.terms_of_use.blank? }, :message => N_('{fn} must be checked in order to signup.').fix_i18n
... ... @@ -328,6 +329,11 @@ class User < ActiveRecord::Base
328 329 end
329 330  
330 331 protected
  332 +
  333 + def normalize_email
  334 + self.email = self.email.squish.downcase
  335 + end
  336 +
331 337 # before filter
332 338 def encrypt_password
333 339 return if password.blank?
... ...
db/migrate/20150112233715_normalize_users_email.rb 0 → 100644
... ... @@ -0,0 +1,11 @@
  1 +class NormalizeUsersEmail < ActiveRecord::Migration
  2 + def up
  3 + User.find_each do |u|
  4 + u.update_column :email, u.send(:normalize_email)
  5 + end
  6 + end
  7 +
  8 + def down
  9 + say "this migration can't be reverted"
  10 + end
  11 +end
... ...