diff options
author | Yorick Peterse <yorickpeterse@gmail.com> | 2018-07-30 15:15:56 +0300 |
---|---|---|
committer | Yorick Peterse <yorickpeterse@gmail.com> | 2018-07-30 15:15:56 +0300 |
commit | 94ea835e55e5a32817432c12045395de71b1507a (patch) | |
tree | 8d6dc80ef181a5c2042604958d5d302e673ca0a5 | |
parent | 2ca8219a20f16636b7a0ffa899a1a04ab8e84782 (diff) | |
parent | 45ff4e31094a8b79ed603ae506990b6fa0e7c96e (diff) |
Merge branch 'sh-support-users-find-by-confirmed-emails' into 'master'
Add support for searching users by confirmed e-mails
See merge request gitlab-org/gitlab-ce!20893
-rw-r--r-- | app/models/user.rb | 10 | ||||
-rw-r--r-- | changelogs/unreleased/sh-support-users-find-by-confirmed-emails.yml | 5 | ||||
-rw-r--r-- | spec/models/user_spec.rb | 15 |
3 files changed, 27 insertions, 3 deletions
diff --git a/app/models/user.rb b/app/models/user.rb index 58429f8d607..03549872924 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -248,6 +248,7 @@ class User < ActiveRecord::Base scope :todo_authors, ->(user_id, state) { where(id: Todo.where(user_id: user_id, state: state).select(:author_id)) } scope :order_recent_sign_in, -> { reorder(Gitlab::Database.nulls_last_order('current_sign_in_at', 'DESC')) } scope :order_oldest_sign_in, -> { reorder(Gitlab::Database.nulls_last_order('current_sign_in_at', 'ASC')) } + scope :confirmed, -> { where.not(confirmed_at: nil) } def self.with_two_factor_indistinct joins("LEFT OUTER JOIN u2f_registrations AS u2f ON u2f.user_id = users.id") @@ -293,14 +294,17 @@ class User < ActiveRecord::Base end # Find a User by their primary email or any associated secondary email - def find_by_any_email(email) - by_any_email(email).take + def find_by_any_email(email, confirmed: false) + by_any_email(email, confirmed: confirmed).take end # Returns a relation containing all the users for the given Email address - def by_any_email(email) + def by_any_email(email, confirmed: false) users = where(email: email) + users = users.confirmed if confirmed + emails = joins(:emails).where(emails: { email: email }) + emails = emails.confirmed if confirmed union = Gitlab::SQL::Union.new([users, emails]) from("(#{union.to_sql}) #{table_name}") diff --git a/changelogs/unreleased/sh-support-users-find-by-confirmed-emails.yml b/changelogs/unreleased/sh-support-users-find-by-confirmed-emails.yml new file mode 100644 index 00000000000..4b0c8117b3e --- /dev/null +++ b/changelogs/unreleased/sh-support-users-find-by-confirmed-emails.yml @@ -0,0 +1,5 @@ +--- +title: Add support for searching users by confirmed e-mails +merge_request: 20893 +author: +type: other diff --git a/spec/models/user_spec.rb b/spec/models/user_spec.rb index fc46551c3be..982d24e7eab 100644 --- a/spec/models/user_spec.rb +++ b/spec/models/user_spec.rb @@ -949,6 +949,7 @@ describe User do user = create(:user, email: 'foo@example.com') expect(described_class.find_by_any_email(user.email)).to eq user + expect(described_class.find_by_any_email(user.email, confirmed: true)).to eq user end it 'finds by secondary email' do @@ -956,11 +957,19 @@ describe User do user = email.user expect(described_class.find_by_any_email(email.email)).to eq user + expect(described_class.find_by_any_email(email.email, confirmed: true)).to eq user end it 'returns nil when nothing found' do expect(described_class.find_by_any_email('')).to be_nil end + + it 'returns nil when user is not confirmed' do + user = create(:user, email: 'foo@example.com', confirmed_at: nil) + + expect(described_class.find_by_any_email(user.email, confirmed: false)).to eq(user) + expect(described_class.find_by_any_email(user.email, confirmed: true)).to be_nil + end end describe '.by_any_email' do @@ -974,6 +983,12 @@ describe User do expect(described_class.by_any_email(user.email)).to eq([user]) end + + it 'returns a relation of users for confirmed users' do + user = create(:user) + + expect(described_class.by_any_email(user.email, confirmed: true)).to eq([user]) + end end describe '.search' do |