From 1ab1094a8c134e531a6abb87627653a7c537c4b8 Mon Sep 17 00:00:00 2001 From: MEspositoE14s <133133846+MEspositoE14s@users.noreply.github.com> Date: Wed, 27 May 2026 15:48:23 -0400 Subject: [PATCH 1/7] Add primary to user_emails and model changes --- dpc-portal/app/models/user_email.rb | 13 ++++++++ ...260527181236_add_primary_to_user_emails.rb | 11 +++++++ dpc-portal/db/schema.rb | 4 ++- dpc-portal/spec/models/user_email_spec.rb | 31 +++++++++++++++++++ 4 files changed, 58 insertions(+), 1 deletion(-) create mode 100644 dpc-portal/db/migrate/20260527181236_add_primary_to_user_emails.rb diff --git a/dpc-portal/app/models/user_email.rb b/dpc-portal/app/models/user_email.rb index afc006aed..80476f91c 100644 --- a/dpc-portal/app/models/user_email.rb +++ b/dpc-portal/app/models/user_email.rb @@ -4,4 +4,17 @@ class UserEmail < ApplicationRecord belongs_to :csp_user has_one :user, through: :csp_users + + # If we update this email to primary, make sure the others aren't. + before_save :ensure_only_one_primary, if: -> { primary? && primary_changed? } + + private + + # Sets all of the user's other emails to not primary. + def ensure_only_one_primary + UserEmail.where(csp_user_id: csp_user_id) + .where(primary: true) + .where.not(id: id) + .update_all(primary: false) + end end diff --git a/dpc-portal/db/migrate/20260527181236_add_primary_to_user_emails.rb b/dpc-portal/db/migrate/20260527181236_add_primary_to_user_emails.rb new file mode 100644 index 000000000..43d013287 --- /dev/null +++ b/dpc-portal/db/migrate/20260527181236_add_primary_to_user_emails.rb @@ -0,0 +1,11 @@ +class AddPrimaryToUserEmails < ActiveRecord::Migration[8.0] + def change + add_column :user_emails, :primary, :boolean, default: false, null: false + + # Make sure each csp_user can only have one primary email. + add_index :user_emails, :csp_user_id, + unique: true, + where: '"primary" = true', + name: 'index_unique_primary_email_per_csp_user' + end +end diff --git a/dpc-portal/db/schema.rb b/dpc-portal/db/schema.rb index 543bcd729..03044bce5 100644 --- a/dpc-portal/db/schema.rb +++ b/dpc-portal/db/schema.rb @@ -10,7 +10,7 @@ # # It's strongly recommended that you check this file into your version control system. -ActiveRecord::Schema[8.0].define(version: 2026_04_24_194005) do +ActiveRecord::Schema[8.0].define(version: 2026_05_27_181236) do # These are extensions that must be enabled in order to support this database enable_extension "pg_catalog.plpgsql" @@ -252,7 +252,9 @@ t.datetime "reactivated_at" t.datetime "created_at", null: false t.datetime "updated_at", null: false + t.boolean "primary", default: false, null: false t.index ["csp_user_id", "email"], name: "index_user_emails_on_csp_user_id_and_email", unique: true + t.index ["csp_user_id"], name: "index_unique_primary_email_per_csp_user", unique: true, where: "(\"primary\" = true)" t.index ["csp_user_id"], name: "index_user_emails_on_csp_user_id" end diff --git a/dpc-portal/spec/models/user_email_spec.rb b/dpc-portal/spec/models/user_email_spec.rb index 63fb237f5..a20a11769 100644 --- a/dpc-portal/spec/models/user_email_spec.rb +++ b/dpc-portal/spec/models/user_email_spec.rb @@ -10,4 +10,35 @@ user_email = create(:user_email, csp_user:) expect(user_email.csp_user).to eq csp_user end + + it 'ensures that creating a new primary email unsets others' do + user = create(:user) + csp = create(:csp) + csp_user = create(:csp_user, user:, csp:) + + email_old_primary = create(:user_email, csp_user:, primary: true, email: 'old@email.com') + email_new_primary = create(:user_email, csp_user:, primary: true, email: 'new@email.com') + + expect(email_old_primary.reload.primary).to eq false + expect(email_new_primary.reload.primary).to eq true + end + + it 'ensures that setting an email to primary unsets others' do + user = create(:user) + csp = create(:csp) + csp_user = create(:csp_user, user:, csp:) + + email_old_primary = create(:user_email, csp_user:, primary: true, email: 'old@email.com') + email_new_primary = create(:user_email, csp_user:, primary: false, email: 'new@email.com') + expect(email_old_primary.primary).to eq true + expect(email_new_primary.primary).to eq false + + # Should set email1 to not primary + email_new_primary.update!(primary: true) + email_old_primary.reload + email_new_primary.reload + + expect(email_old_primary.primary).to eq false + expect(email_new_primary.primary).to eq true + end end From a19369b0632e3a3b589eecbfe034fa997c1f2b06 Mon Sep 17 00:00:00 2001 From: MEspositoE14s <133133846+MEspositoE14s@users.noreply.github.com> Date: Wed, 27 May 2026 16:39:44 -0400 Subject: [PATCH 2/7] Login.gov marks primary email --- dpc-portal/app/controllers/login_dot_gov_controller.rb | 8 ++++++-- dpc-portal/spec/requests/login_dot_gov_spec.rb | 2 ++ 2 files changed, 8 insertions(+), 2 deletions(-) diff --git a/dpc-portal/app/controllers/login_dot_gov_controller.rb b/dpc-portal/app/controllers/login_dot_gov_controller.rb index e75c6d8a9..b703aae5d 100644 --- a/dpc-portal/app/controllers/login_dot_gov_controller.rb +++ b/dpc-portal/app/controllers/login_dot_gov_controller.rb @@ -77,7 +77,7 @@ def maybe_update_user(user, data) user&.update(given_name: data.given_name, family_name: data.family_name) end - def update_email(csp_user, new_emails) + def update_email(csp_user, new_emails, primary_email) return unless csp_user existing_emails = csp_user.user_emails @@ -86,6 +86,10 @@ def update_email(csp_user, new_emails) ActiveRecord::Base.transaction do add_or_activate_new_email(csp_user, new_emails, existing_emails) deactivate_old_email(new_emails, existing_emails) + + # Set their primary email, which should now exist in the user_emails table + primary_email = csp_user.user_emails.find_by(email: primary_email) + primary_email.update(primary: true) if primary_email && !primary_email.primary? end end @@ -153,7 +157,7 @@ def csp def post_signin_actions(user, csp_user, auth) ial_2_actions(user, auth) - update_email(csp_user, auth.extra.raw_info.all_emails) + update_email(csp_user, auth.extra.raw_info.all_emails, auth.info.email) end end # rubocop:enable Metrics/ClassLength diff --git a/dpc-portal/spec/requests/login_dot_gov_spec.rb b/dpc-portal/spec/requests/login_dot_gov_spec.rb index fc1c13225..3c4bed4b3 100644 --- a/dpc-portal/spec/requests/login_dot_gov_spec.rb +++ b/dpc-portal/spec/requests/login_dot_gov_spec.rb @@ -204,6 +204,7 @@ emails = UserEmail.last(2).pluck(:email) expect(emails).to match_array(%w[email1@example.com email2@example.com]) expect(UserEmail.pluck(:active)).to all(be true) + expect(UserEmail.find(&:primary?).email).to eq 'email1@example.com' end end @@ -265,6 +266,7 @@ expect(email.active).to eq true expect(email.deactivated_at).to be_nil expect(email.reactivated_at).to_not be_nil + expect(email.primary).to eq true end end end From e1cf83394ef8443d999952841788cb8358dcf8a4 Mon Sep 17 00:00:00 2001 From: MEspositoE14s <133133846+MEspositoE14s@users.noreply.github.com> Date: Wed, 27 May 2026 16:39:44 -0400 Subject: [PATCH 3/7] Login.gov marks primary email --- dpc-portal/app/controllers/login_dot_gov_controller.rb | 8 ++++++-- dpc-portal/spec/requests/login_dot_gov_spec.rb | 3 +++ 2 files changed, 9 insertions(+), 2 deletions(-) diff --git a/dpc-portal/app/controllers/login_dot_gov_controller.rb b/dpc-portal/app/controllers/login_dot_gov_controller.rb index e75c6d8a9..b703aae5d 100644 --- a/dpc-portal/app/controllers/login_dot_gov_controller.rb +++ b/dpc-portal/app/controllers/login_dot_gov_controller.rb @@ -77,7 +77,7 @@ def maybe_update_user(user, data) user&.update(given_name: data.given_name, family_name: data.family_name) end - def update_email(csp_user, new_emails) + def update_email(csp_user, new_emails, primary_email) return unless csp_user existing_emails = csp_user.user_emails @@ -86,6 +86,10 @@ def update_email(csp_user, new_emails) ActiveRecord::Base.transaction do add_or_activate_new_email(csp_user, new_emails, existing_emails) deactivate_old_email(new_emails, existing_emails) + + # Set their primary email, which should now exist in the user_emails table + primary_email = csp_user.user_emails.find_by(email: primary_email) + primary_email.update(primary: true) if primary_email && !primary_email.primary? end end @@ -153,7 +157,7 @@ def csp def post_signin_actions(user, csp_user, auth) ial_2_actions(user, auth) - update_email(csp_user, auth.extra.raw_info.all_emails) + update_email(csp_user, auth.extra.raw_info.all_emails, auth.info.email) end end # rubocop:enable Metrics/ClassLength diff --git a/dpc-portal/spec/requests/login_dot_gov_spec.rb b/dpc-portal/spec/requests/login_dot_gov_spec.rb index fc1c13225..ff8b57971 100644 --- a/dpc-portal/spec/requests/login_dot_gov_spec.rb +++ b/dpc-portal/spec/requests/login_dot_gov_spec.rb @@ -204,6 +204,8 @@ emails = UserEmail.last(2).pluck(:email) expect(emails).to match_array(%w[email1@example.com email2@example.com]) expect(UserEmail.pluck(:active)).to all(be true) + expect(UserEmail.find(&:primary?).email).to eq 'email1@example.com' + expect(UserEmail.count(&:primary?)).to eq 1 end end @@ -265,6 +267,7 @@ expect(email.active).to eq true expect(email.deactivated_at).to be_nil expect(email.reactivated_at).to_not be_nil + expect(email.primary).to eq true end end end From 6e4db1fdd76deede05fc5d7663ebe72aa4cdc353 Mon Sep 17 00:00:00 2001 From: MEspositoE14s <133133846+MEspositoE14s@users.noreply.github.com> Date: Mon, 1 Jun 2026 11:40:18 -0400 Subject: [PATCH 4/7] Move pulling emails to private methods --- .../app/controllers/login_dot_gov_controller.rb | 16 ++++++++++++---- 1 file changed, 12 insertions(+), 4 deletions(-) diff --git a/dpc-portal/app/controllers/login_dot_gov_controller.rb b/dpc-portal/app/controllers/login_dot_gov_controller.rb index b703aae5d..c24d6262d 100644 --- a/dpc-portal/app/controllers/login_dot_gov_controller.rb +++ b/dpc-portal/app/controllers/login_dot_gov_controller.rb @@ -77,7 +77,7 @@ def maybe_update_user(user, data) user&.update(given_name: data.given_name, family_name: data.family_name) end - def update_email(csp_user, new_emails, primary_email) + def update_email(csp_user, new_emails, verified_email) return unless csp_user existing_emails = csp_user.user_emails @@ -88,8 +88,8 @@ def update_email(csp_user, new_emails, primary_email) deactivate_old_email(new_emails, existing_emails) # Set their primary email, which should now exist in the user_emails table - primary_email = csp_user.user_emails.find_by(email: primary_email) - primary_email.update(primary: true) if primary_email && !primary_email.primary? + verified_email = csp_user.user_emails.find_by(email: verified_email) + verified_email.update(primary: true) if verified_email && !verified_email.primary? end end @@ -157,7 +157,15 @@ def csp def post_signin_actions(user, csp_user, auth) ial_2_actions(user, auth) - update_email(csp_user, auth.extra.raw_info.all_emails, auth.info.email) + update_email(csp_user, all_emails(auth), verified_email(auth)) + end + + def verified_email(auth) + auth.info.email + end + + def all_emails(auth) + auth.extra.raw_info.all_emails end end # rubocop:enable Metrics/ClassLength From 67978a779dd8b6e3491aaca242784bb5e34bd5c1 Mon Sep 17 00:00:00 2001 From: MEspositoE14s <133133846+MEspositoE14s@users.noreply.github.com> Date: Mon, 1 Jun 2026 14:10:18 -0400 Subject: [PATCH 5/7] Rename primary email to verified --- .../controllers/login_dot_gov_controller.rb | 4 +-- dpc-portal/app/models/user_email.rb | 12 +++---- ...260527181236_add_primary_to_user_emails.rb | 11 ------- ...60527181236_add_verified_to_user_emails.rb | 11 +++++++ dpc-portal/db/schema.rb | 4 +-- dpc-portal/spec/models/user_email_spec.rb | 32 +++++++++---------- .../spec/requests/login_dot_gov_spec.rb | 6 ++-- 7 files changed, 40 insertions(+), 40 deletions(-) delete mode 100644 dpc-portal/db/migrate/20260527181236_add_primary_to_user_emails.rb create mode 100644 dpc-portal/db/migrate/20260527181236_add_verified_to_user_emails.rb diff --git a/dpc-portal/app/controllers/login_dot_gov_controller.rb b/dpc-portal/app/controllers/login_dot_gov_controller.rb index c24d6262d..b8539e955 100644 --- a/dpc-portal/app/controllers/login_dot_gov_controller.rb +++ b/dpc-portal/app/controllers/login_dot_gov_controller.rb @@ -87,9 +87,9 @@ def update_email(csp_user, new_emails, verified_email) add_or_activate_new_email(csp_user, new_emails, existing_emails) deactivate_old_email(new_emails, existing_emails) - # Set their primary email, which should now exist in the user_emails table + # Set their verified email, which should now exist in the user_emails table verified_email = csp_user.user_emails.find_by(email: verified_email) - verified_email.update(primary: true) if verified_email && !verified_email.primary? + verified_email.update(verified: true) if verified_email && !verified_email.verified? end end diff --git a/dpc-portal/app/models/user_email.rb b/dpc-portal/app/models/user_email.rb index 80476f91c..803863452 100644 --- a/dpc-portal/app/models/user_email.rb +++ b/dpc-portal/app/models/user_email.rb @@ -5,16 +5,16 @@ class UserEmail < ApplicationRecord belongs_to :csp_user has_one :user, through: :csp_users - # If we update this email to primary, make sure the others aren't. - before_save :ensure_only_one_primary, if: -> { primary? && primary_changed? } + # If we update this email to verified, make sure the others aren't. + before_save :ensure_only_one_verified, if: -> { verified? && verified_changed? } private - # Sets all of the user's other emails to not primary. - def ensure_only_one_primary + # Sets all of the user's other emails to not verified. + def ensure_only_one_verified UserEmail.where(csp_user_id: csp_user_id) - .where(primary: true) + .where(verified: true) .where.not(id: id) - .update_all(primary: false) + .update_all(verified: false) end end diff --git a/dpc-portal/db/migrate/20260527181236_add_primary_to_user_emails.rb b/dpc-portal/db/migrate/20260527181236_add_primary_to_user_emails.rb deleted file mode 100644 index 43d013287..000000000 --- a/dpc-portal/db/migrate/20260527181236_add_primary_to_user_emails.rb +++ /dev/null @@ -1,11 +0,0 @@ -class AddPrimaryToUserEmails < ActiveRecord::Migration[8.0] - def change - add_column :user_emails, :primary, :boolean, default: false, null: false - - # Make sure each csp_user can only have one primary email. - add_index :user_emails, :csp_user_id, - unique: true, - where: '"primary" = true', - name: 'index_unique_primary_email_per_csp_user' - end -end diff --git a/dpc-portal/db/migrate/20260527181236_add_verified_to_user_emails.rb b/dpc-portal/db/migrate/20260527181236_add_verified_to_user_emails.rb new file mode 100644 index 000000000..b1e3cf20b --- /dev/null +++ b/dpc-portal/db/migrate/20260527181236_add_verified_to_user_emails.rb @@ -0,0 +1,11 @@ +class AddVerifiedToUserEmails < ActiveRecord::Migration[8.0] + def change + add_column :user_emails, :verified, :boolean, default: false, null: false + + # Make sure each csp_user can only have one verified email. + add_index :user_emails, :csp_user_id, + unique: true, + where: '"verified" = true', + name: 'index_unique_verified_email_per_csp_user' + end +end diff --git a/dpc-portal/db/schema.rb b/dpc-portal/db/schema.rb index 03044bce5..31461190d 100644 --- a/dpc-portal/db/schema.rb +++ b/dpc-portal/db/schema.rb @@ -252,9 +252,9 @@ t.datetime "reactivated_at" t.datetime "created_at", null: false t.datetime "updated_at", null: false - t.boolean "primary", default: false, null: false + t.boolean "verified", default: false, null: false t.index ["csp_user_id", "email"], name: "index_user_emails_on_csp_user_id_and_email", unique: true - t.index ["csp_user_id"], name: "index_unique_primary_email_per_csp_user", unique: true, where: "(\"primary\" = true)" + t.index ["csp_user_id"], name: "index_unique_verified_email_per_csp_user", unique: true, where: "(verified = true)" t.index ["csp_user_id"], name: "index_user_emails_on_csp_user_id" end diff --git a/dpc-portal/spec/models/user_email_spec.rb b/dpc-portal/spec/models/user_email_spec.rb index a20a11769..9d90ad9ae 100644 --- a/dpc-portal/spec/models/user_email_spec.rb +++ b/dpc-portal/spec/models/user_email_spec.rb @@ -11,34 +11,34 @@ expect(user_email.csp_user).to eq csp_user end - it 'ensures that creating a new primary email unsets others' do + it 'ensures that creating a new verified email unsets others' do user = create(:user) csp = create(:csp) csp_user = create(:csp_user, user:, csp:) - email_old_primary = create(:user_email, csp_user:, primary: true, email: 'old@email.com') - email_new_primary = create(:user_email, csp_user:, primary: true, email: 'new@email.com') + email_old_verified = create(:user_email, csp_user:, verified: true, email: 'old@email.com') + email_new_verified = create(:user_email, csp_user:, verified: true, email: 'new@email.com') - expect(email_old_primary.reload.primary).to eq false - expect(email_new_primary.reload.primary).to eq true + expect(email_old_verified.reload.verified).to eq false + expect(email_new_verified.reload.verified).to eq true end - it 'ensures that setting an email to primary unsets others' do + it 'ensures that setting an email to verified unsets others' do user = create(:user) csp = create(:csp) csp_user = create(:csp_user, user:, csp:) - email_old_primary = create(:user_email, csp_user:, primary: true, email: 'old@email.com') - email_new_primary = create(:user_email, csp_user:, primary: false, email: 'new@email.com') - expect(email_old_primary.primary).to eq true - expect(email_new_primary.primary).to eq false + email_old_verified = create(:user_email, csp_user:, verified: true, email: 'old@email.com') + email_new_verified = create(:user_email, csp_user:, verified: false, email: 'new@email.com') + expect(email_old_verified.verified).to eq true + expect(email_new_verified.verified).to eq false - # Should set email1 to not primary - email_new_primary.update!(primary: true) - email_old_primary.reload - email_new_primary.reload + # Should set email1 to not verified + email_new_verified.update!(verified: true) + email_old_verified.reload + email_new_verified.reload - expect(email_old_primary.primary).to eq false - expect(email_new_primary.primary).to eq true + expect(email_old_verified.verified).to eq false + expect(email_new_verified.verified).to eq true end end diff --git a/dpc-portal/spec/requests/login_dot_gov_spec.rb b/dpc-portal/spec/requests/login_dot_gov_spec.rb index ff8b57971..5d1351870 100644 --- a/dpc-portal/spec/requests/login_dot_gov_spec.rb +++ b/dpc-portal/spec/requests/login_dot_gov_spec.rb @@ -204,8 +204,8 @@ emails = UserEmail.last(2).pluck(:email) expect(emails).to match_array(%w[email1@example.com email2@example.com]) expect(UserEmail.pluck(:active)).to all(be true) - expect(UserEmail.find(&:primary?).email).to eq 'email1@example.com' - expect(UserEmail.count(&:primary?)).to eq 1 + expect(UserEmail.find(&:verified?).email).to eq 'email1@example.com' + expect(UserEmail.count(&:verified?)).to eq 1 end end @@ -267,7 +267,7 @@ expect(email.active).to eq true expect(email.deactivated_at).to be_nil expect(email.reactivated_at).to_not be_nil - expect(email.primary).to eq true + expect(email.verified).to eq true end end end From c90956280da99258c64add8a8d662a758b6dbf5a Mon Sep 17 00:00:00 2001 From: MEspositoE14s <133133846+MEspositoE14s@users.noreply.github.com> Date: Mon, 1 Jun 2026 14:39:51 -0400 Subject: [PATCH 6/7] Revert "Rename primary email to verified" This reverts commit 67978a779dd8b6e3491aaca242784bb5e34bd5c1. --- .../controllers/login_dot_gov_controller.rb | 4 +-- dpc-portal/app/models/user_email.rb | 12 +++---- ...260527181236_add_primary_to_user_emails.rb | 11 +++++++ ...60527181236_add_verified_to_user_emails.rb | 11 ------- dpc-portal/db/schema.rb | 4 +-- dpc-portal/spec/models/user_email_spec.rb | 32 +++++++++---------- .../spec/requests/login_dot_gov_spec.rb | 6 ++-- 7 files changed, 40 insertions(+), 40 deletions(-) create mode 100644 dpc-portal/db/migrate/20260527181236_add_primary_to_user_emails.rb delete mode 100644 dpc-portal/db/migrate/20260527181236_add_verified_to_user_emails.rb diff --git a/dpc-portal/app/controllers/login_dot_gov_controller.rb b/dpc-portal/app/controllers/login_dot_gov_controller.rb index b8539e955..c24d6262d 100644 --- a/dpc-portal/app/controllers/login_dot_gov_controller.rb +++ b/dpc-portal/app/controllers/login_dot_gov_controller.rb @@ -87,9 +87,9 @@ def update_email(csp_user, new_emails, verified_email) add_or_activate_new_email(csp_user, new_emails, existing_emails) deactivate_old_email(new_emails, existing_emails) - # Set their verified email, which should now exist in the user_emails table + # Set their primary email, which should now exist in the user_emails table verified_email = csp_user.user_emails.find_by(email: verified_email) - verified_email.update(verified: true) if verified_email && !verified_email.verified? + verified_email.update(primary: true) if verified_email && !verified_email.primary? end end diff --git a/dpc-portal/app/models/user_email.rb b/dpc-portal/app/models/user_email.rb index 803863452..80476f91c 100644 --- a/dpc-portal/app/models/user_email.rb +++ b/dpc-portal/app/models/user_email.rb @@ -5,16 +5,16 @@ class UserEmail < ApplicationRecord belongs_to :csp_user has_one :user, through: :csp_users - # If we update this email to verified, make sure the others aren't. - before_save :ensure_only_one_verified, if: -> { verified? && verified_changed? } + # If we update this email to primary, make sure the others aren't. + before_save :ensure_only_one_primary, if: -> { primary? && primary_changed? } private - # Sets all of the user's other emails to not verified. - def ensure_only_one_verified + # Sets all of the user's other emails to not primary. + def ensure_only_one_primary UserEmail.where(csp_user_id: csp_user_id) - .where(verified: true) + .where(primary: true) .where.not(id: id) - .update_all(verified: false) + .update_all(primary: false) end end diff --git a/dpc-portal/db/migrate/20260527181236_add_primary_to_user_emails.rb b/dpc-portal/db/migrate/20260527181236_add_primary_to_user_emails.rb new file mode 100644 index 000000000..43d013287 --- /dev/null +++ b/dpc-portal/db/migrate/20260527181236_add_primary_to_user_emails.rb @@ -0,0 +1,11 @@ +class AddPrimaryToUserEmails < ActiveRecord::Migration[8.0] + def change + add_column :user_emails, :primary, :boolean, default: false, null: false + + # Make sure each csp_user can only have one primary email. + add_index :user_emails, :csp_user_id, + unique: true, + where: '"primary" = true', + name: 'index_unique_primary_email_per_csp_user' + end +end diff --git a/dpc-portal/db/migrate/20260527181236_add_verified_to_user_emails.rb b/dpc-portal/db/migrate/20260527181236_add_verified_to_user_emails.rb deleted file mode 100644 index b1e3cf20b..000000000 --- a/dpc-portal/db/migrate/20260527181236_add_verified_to_user_emails.rb +++ /dev/null @@ -1,11 +0,0 @@ -class AddVerifiedToUserEmails < ActiveRecord::Migration[8.0] - def change - add_column :user_emails, :verified, :boolean, default: false, null: false - - # Make sure each csp_user can only have one verified email. - add_index :user_emails, :csp_user_id, - unique: true, - where: '"verified" = true', - name: 'index_unique_verified_email_per_csp_user' - end -end diff --git a/dpc-portal/db/schema.rb b/dpc-portal/db/schema.rb index 31461190d..03044bce5 100644 --- a/dpc-portal/db/schema.rb +++ b/dpc-portal/db/schema.rb @@ -252,9 +252,9 @@ t.datetime "reactivated_at" t.datetime "created_at", null: false t.datetime "updated_at", null: false - t.boolean "verified", default: false, null: false + t.boolean "primary", default: false, null: false t.index ["csp_user_id", "email"], name: "index_user_emails_on_csp_user_id_and_email", unique: true - t.index ["csp_user_id"], name: "index_unique_verified_email_per_csp_user", unique: true, where: "(verified = true)" + t.index ["csp_user_id"], name: "index_unique_primary_email_per_csp_user", unique: true, where: "(\"primary\" = true)" t.index ["csp_user_id"], name: "index_user_emails_on_csp_user_id" end diff --git a/dpc-portal/spec/models/user_email_spec.rb b/dpc-portal/spec/models/user_email_spec.rb index 9d90ad9ae..a20a11769 100644 --- a/dpc-portal/spec/models/user_email_spec.rb +++ b/dpc-portal/spec/models/user_email_spec.rb @@ -11,34 +11,34 @@ expect(user_email.csp_user).to eq csp_user end - it 'ensures that creating a new verified email unsets others' do + it 'ensures that creating a new primary email unsets others' do user = create(:user) csp = create(:csp) csp_user = create(:csp_user, user:, csp:) - email_old_verified = create(:user_email, csp_user:, verified: true, email: 'old@email.com') - email_new_verified = create(:user_email, csp_user:, verified: true, email: 'new@email.com') + email_old_primary = create(:user_email, csp_user:, primary: true, email: 'old@email.com') + email_new_primary = create(:user_email, csp_user:, primary: true, email: 'new@email.com') - expect(email_old_verified.reload.verified).to eq false - expect(email_new_verified.reload.verified).to eq true + expect(email_old_primary.reload.primary).to eq false + expect(email_new_primary.reload.primary).to eq true end - it 'ensures that setting an email to verified unsets others' do + it 'ensures that setting an email to primary unsets others' do user = create(:user) csp = create(:csp) csp_user = create(:csp_user, user:, csp:) - email_old_verified = create(:user_email, csp_user:, verified: true, email: 'old@email.com') - email_new_verified = create(:user_email, csp_user:, verified: false, email: 'new@email.com') - expect(email_old_verified.verified).to eq true - expect(email_new_verified.verified).to eq false + email_old_primary = create(:user_email, csp_user:, primary: true, email: 'old@email.com') + email_new_primary = create(:user_email, csp_user:, primary: false, email: 'new@email.com') + expect(email_old_primary.primary).to eq true + expect(email_new_primary.primary).to eq false - # Should set email1 to not verified - email_new_verified.update!(verified: true) - email_old_verified.reload - email_new_verified.reload + # Should set email1 to not primary + email_new_primary.update!(primary: true) + email_old_primary.reload + email_new_primary.reload - expect(email_old_verified.verified).to eq false - expect(email_new_verified.verified).to eq true + expect(email_old_primary.primary).to eq false + expect(email_new_primary.primary).to eq true end end diff --git a/dpc-portal/spec/requests/login_dot_gov_spec.rb b/dpc-portal/spec/requests/login_dot_gov_spec.rb index 5d1351870..ff8b57971 100644 --- a/dpc-portal/spec/requests/login_dot_gov_spec.rb +++ b/dpc-portal/spec/requests/login_dot_gov_spec.rb @@ -204,8 +204,8 @@ emails = UserEmail.last(2).pluck(:email) expect(emails).to match_array(%w[email1@example.com email2@example.com]) expect(UserEmail.pluck(:active)).to all(be true) - expect(UserEmail.find(&:verified?).email).to eq 'email1@example.com' - expect(UserEmail.count(&:verified?)).to eq 1 + expect(UserEmail.find(&:primary?).email).to eq 'email1@example.com' + expect(UserEmail.count(&:primary?)).to eq 1 end end @@ -267,7 +267,7 @@ expect(email.active).to eq true expect(email.deactivated_at).to be_nil expect(email.reactivated_at).to_not be_nil - expect(email.verified).to eq true + expect(email.primary).to eq true end end end From 7e0cb01e7e6989b812f3f4dc5c95280d4a682983 Mon Sep 17 00:00:00 2001 From: MEspositoE14s <133133846+MEspositoE14s@users.noreply.github.com> Date: Mon, 1 Jun 2026 14:44:36 -0400 Subject: [PATCH 7/7] Rename verfied email to primary --- dpc-portal/app/controllers/login_dot_gov_controller.rb | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/dpc-portal/app/controllers/login_dot_gov_controller.rb b/dpc-portal/app/controllers/login_dot_gov_controller.rb index c24d6262d..9304bcd89 100644 --- a/dpc-portal/app/controllers/login_dot_gov_controller.rb +++ b/dpc-portal/app/controllers/login_dot_gov_controller.rb @@ -77,7 +77,7 @@ def maybe_update_user(user, data) user&.update(given_name: data.given_name, family_name: data.family_name) end - def update_email(csp_user, new_emails, verified_email) + def update_email(csp_user, new_emails, primary_email) return unless csp_user existing_emails = csp_user.user_emails @@ -88,8 +88,8 @@ def update_email(csp_user, new_emails, verified_email) deactivate_old_email(new_emails, existing_emails) # Set their primary email, which should now exist in the user_emails table - verified_email = csp_user.user_emails.find_by(email: verified_email) - verified_email.update(primary: true) if verified_email && !verified_email.primary? + primary_email = csp_user.user_emails.find_by(email: primary_email) + primary_email.update(primary: true) if primary_email && !primary_email.primary? end end @@ -157,10 +157,10 @@ def csp def post_signin_actions(user, csp_user, auth) ial_2_actions(user, auth) - update_email(csp_user, all_emails(auth), verified_email(auth)) + update_email(csp_user, all_emails(auth), primary_email(auth)) end - def verified_email(auth) + def primary_email(auth) auth.info.email end