Downcase emails upon inviting

This commit is contained in:
Raphael Sofaer 2011-05-13 12:14:55 -07:00
parent 5f531f14f1
commit f8b7307441
7 changed files with 34 additions and 13 deletions

View file

@ -32,7 +32,7 @@ class UsersController < ApplicationController
if u[:email_preferences]
@user.update_user_preferences(u[:email_preferences])
flash[:notice] = I18n.t 'users.update.email_notifications_changed'
# change passowrd
# change password
elsif u[:current_password] && u[:password] && u[:password_confirmation]
if @user.update_with_password(u)
flash[:notice] = I18n.t 'users.update.password_changed'

View file

@ -11,6 +11,7 @@ class Invitation < ActiveRecord::Base
validates_presence_of :sender, :recipient, :aspect
def self.invite(opts = {})
opts[:identifier].downcase!
return false if opts[:identifier] == opts[:from].email
existing_user = self.find_existing_user(opts[:service], opts[:identifier])

View file

@ -15,7 +15,7 @@ class User < ActiveRecord::Base
:recoverable, :rememberable, :trackable, :validatable,
:timeoutable
before_validation :strip_and_downcase_username, :on => :create
before_validation :strip_and_downcase_username
before_validation :set_current_language, :on => :create
validates_presence_of :username

View file

@ -17,6 +17,7 @@ set :use_sudo, false
set :scm_verbose, true
set :repository_cache, "remote_cache"
set :deploy_via, :checkout
set :branch, 'devise_invitable-update'
namespace :deploy do
task :symlink_config_files do

View file

@ -1,10 +0,0 @@
require 'db/migrate/20110421120744_downcase_usernames'
class DowncaseUsernamesAgain < ActiveRecord::Migration
def self.up
DowncaseUsernames.up
end
def self.down
DowncaseUsernames.down
end
end

View file

@ -0,0 +1,29 @@
class EliminateStrayUserRecords < ActiveRecord::Migration
def self.up
duplicated_emails = execute("SELECT LOWER(email) from users GROUP BY LOWER(email) HAVING COUNT(*) > 1").to_a
duplicated_emails.each do |email|
records = execute("SELECT users.id, users.username, users.created_at from users WHERE LOWER(users.email) = '#{email}'").to_a
with_username = records.select { |r| !r[1].blank? }
if with_username.length == 1
execute("DELETE FROM users WHERE LOWER(users.email) = '#{email}' AND users.username IS NULL")
end
if with_username.length == 0
newest_record = records.sort_by{|r| r[2].to_i}.last
execute("DELETE FROM users WHERE LOWER(users.email) = '#{email}' AND users.id != #{newest_record[0]}")
end
end
execute <<SQL
UPDATE users
SET users.username = LOWER(users.username)
WHERE users.username != LOWER(users.username)
SQL
execute <<SQL
UPDATE users
SET users.email = LOWER(users.email)
WHERE users.email != LOWER(users.email)
SQL
end
def self.down
end
end

View file

@ -102,7 +102,7 @@ describe Invitation do
end
end
context 'invitated user' do
context 'invited user' do
it 'by email' do
@identifier = @users[3].email
@type = 'email'