Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions app/controllers/users_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,8 @@ def update
flash[:notice] = I18n.t 'users.update.unconfirmed_email_changed'
end
else
# if this field is not cleared, the form will render as though the change were successful
@user.unconfirmed_email = nil
flash[:error] = I18n.t 'users.update.unconfirmed_email_not_changed'
end
elsif u[:auto_follow_back]
Expand Down
21 changes: 21 additions & 0 deletions app/models/user.rb
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@ class User < ActiveRecord::Base
validates :color_theme, inclusion: {in: AVAILABLE_COLOR_THEME_CODES}, allow_blank: true
validates_format_of :unconfirmed_email, :with => Devise.email_regexp, :allow_blank => true

validate :unconfirmed_email_quasiuniqueness

validates_presence_of :person, :unless => proc {|user| user.invitation_token.present?}
validates_associated :person
validate :no_person_with_same_username
Expand Down Expand Up @@ -83,6 +85,8 @@ class User < ActiveRecord::Base

before_save :guard_unconfirmed_email

after_save :remove_invalid_unconfirmed_emails

def self.all_sharing_with_person(person)
User.joins(:contacts).where(:contacts => {:person_id => person.id})
end
Expand Down Expand Up @@ -484,6 +488,13 @@ def mine?(target)
end


# Ensure that the unconfirmed email isn't already someone's email
def unconfirmed_email_quasiuniqueness
if User.exists?(["id != ? AND email = ?", id, unconfirmed_email])
errors.add(:unconfirmed_email, "is already in use")
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a user facing error, right? It should use a translation then :)

end
end

def guard_unconfirmed_email
self.unconfirmed_email = nil if unconfirmed_email.blank? || unconfirmed_email == email

Expand All @@ -492,6 +503,16 @@ def guard_unconfirmed_email
end
end

# Whenever email is set, clear all unconfirmed emails which match
def remove_invalid_unconfirmed_emails
if email_changed?
User.where("unconfirmed_email = ?", email).find_each do |problem_user|
problem_user.unconfirmed_email = nil
problem_user.save
end
end
end
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think setting it to nil can ever make a user fail validation, so User.where(unconfirmed_email: email).update_all(unconfirmed_email: nil) should be much, much faster.


# Generate public/private keys for User and associated Person
def generate_keys
key_size = (Rails.env == 'test' ? 512 : 4096)
Expand Down
12 changes: 12 additions & 0 deletions spec/models/user_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -305,6 +305,13 @@
alice.email = "somebody@anywhere"
expect(alice).not_to be_valid
end

it "resets a matching unconfirmed_email on save" do
eve.update_attribute :unconfirmed_email, "new@email.com"
alice.update_attribute :email, "new@email.com"
Copy link
Copy Markdown
Member

@jhass jhass Jul 5, 2016

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's use example.com rather than a potentially valid address. Also not sure why you use update_attribute over the direct accessors :) Oh to skip validation, nvm.

eve.reload
expect(eve.unconfirmed_email).to eql(nil)
end
end

describe "of unconfirmed_email" do
Expand All @@ -321,6 +328,11 @@
expect(alice).to be_valid
end

it "requires an unconfirmed_email address which is not another user's email address" do
alice.unconfirmed_email = eve.email
expect(alice).not_to be_valid
end

it "requires a valid unconfirmed_email address" do
alice.unconfirmed_email = "somebody@anywhere"
expect(alice).not_to be_valid
Expand Down