From 28b0797bfa3fef4bae5d03d9f0feba8406bcabe0 Mon Sep 17 00:00:00 2001 From: Vraj Mohan Date: Tue, 17 Dec 2024 13:52:38 -0800 Subject: [PATCH] Get rid of EmailContext --- .../selected_email_controller.rb | 2 +- app/controllers/concerns/mfa_setup_concern.rb | 2 +- .../sign_up/completions_controller.rb | 2 +- .../sign_up/select_email_controller.rb | 2 +- app/decorators/email_context.rb | 21 ----------------- app/models/user.rb | 2 +- app/policies/email_policy.rb | 8 +++---- app/presenters/account_show_presenter.rb | 2 +- .../openid_connect_user_info_presenter.rb | 4 ---- app/services/displayable_pii_formatter.rb | 2 +- config/initializers/saml_idp.rb | 2 +- spec/decorators/email_context_spec.rb | 23 ------------------- spec/services/attribute_asserter_spec.rb | 7 +++--- 13 files changed, 15 insertions(+), 64 deletions(-) delete mode 100644 app/decorators/email_context.rb delete mode 100644 spec/decorators/email_context_spec.rb diff --git a/app/controllers/accounts/connected_accounts/selected_email_controller.rb b/app/controllers/accounts/connected_accounts/selected_email_controller.rb index eacdfd4f300..1ae8b77a6fe 100644 --- a/app/controllers/accounts/connected_accounts/selected_email_controller.rb +++ b/app/controllers/accounts/connected_accounts/selected_email_controller.rb @@ -53,7 +53,7 @@ def identity end def last_email - EmailContext.new(current_user).last_sign_in_email_address.id + current_user.last_sign_in_email_address.id end end end diff --git a/app/controllers/concerns/mfa_setup_concern.rb b/app/controllers/concerns/mfa_setup_concern.rb index e3c1e07ad51..777fbb9004d 100644 --- a/app/controllers/concerns/mfa_setup_concern.rb +++ b/app/controllers/concerns/mfa_setup_concern.rb @@ -96,7 +96,7 @@ def threatmetrix_attrs user_id: current_user.id, request_ip: request&.remote_ip, threatmetrix_session_id: session[:threatmetrix_session_id], - email: EmailContext.new(current_user).last_sign_in_email_address.email, + email: current_user.last_sign_in_email_address.email, uuid_prefix: current_sp&.app_id, } end diff --git a/app/controllers/sign_up/completions_controller.rb b/app/controllers/sign_up/completions_controller.rb index 9eb9aec9edb..f2a9e2c9cb8 100644 --- a/app/controllers/sign_up/completions_controller.rb +++ b/app/controllers/sign_up/completions_controller.rb @@ -22,7 +22,7 @@ def update update_verified_attributes send_in_person_completion_survey if user_session[:selected_email_id_for_linked_identity].nil? - user_session[:selected_email_id_for_linked_identity] = EmailContext.new(current_user) + user_session[:selected_email_id_for_linked_identity] = current_user .last_sign_in_email_address.id end if decider.go_back_to_mobile_app? diff --git a/app/controllers/sign_up/select_email_controller.rb b/app/controllers/sign_up/select_email_controller.rb index 2d691d85d8a..f1316a187b3 100644 --- a/app/controllers/sign_up/select_email_controller.rb +++ b/app/controllers/sign_up/select_email_controller.rb @@ -52,7 +52,7 @@ def last_email if user_session[:selected_email_id_for_linked_identity] user_emails.find(user_session[:selected_email_id_for_linked_identity]).email else - EmailContext.new(current_user).last_sign_in_email_address.email + current_user.last_sign_in_email_address.email end end diff --git a/app/decorators/email_context.rb b/app/decorators/email_context.rb deleted file mode 100644 index 2f41693a362..00000000000 --- a/app/decorators/email_context.rb +++ /dev/null @@ -1,21 +0,0 @@ -# frozen_string_literal: true - -class EmailContext - attr_reader :user - - def initialize(user) - @user = user - end - - def last_sign_in_email_address - user.confirmed_email_addresses.order('last_sign_in_at DESC NULLS LAST').first - end - - def email_address_count - user.email_addresses.count - end - - def confirmed_email_address_count - user.confirmed_email_addresses.count - end -end diff --git a/app/models/user.rb b/app/models/user.rb index 3dee347dd33..41bedac7df4 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -392,7 +392,7 @@ def qrcode(otp_secret_key) interval: IdentityConfig.store.totp_code_interval, } url = ROTP::TOTP.new(otp_secret_key, options).provisioning_uri( - EmailContext.new(self).last_sign_in_email_address.email, + last_sign_in_email_address.email, ) qrcode = RQRCode::QRCode.new(url) qrcode.as_png(size: 240).to_data_url diff --git a/app/policies/email_policy.rb b/app/policies/email_policy.rb index 676936dadb6..5d188ca904d 100644 --- a/app/policies/email_policy.rb +++ b/app/policies/email_policy.rb @@ -2,7 +2,7 @@ class EmailPolicy def initialize(user) - @user = EmailContext.new(user) + @user = user end def can_delete_email?(email) @@ -12,17 +12,17 @@ def can_delete_email?(email) end def can_add_email? - user.email_address_count < IdentityConfig.store.max_emails_per_account + user.email_addresses.count < IdentityConfig.store.max_emails_per_account end private def last_confirmed_email_address? - user.confirmed_email_address_count <= 1 + user.confirmed_email_addresses.count <= 1 end def last_email_address? - user.email_address_count <= 1 + user.email_addresses.count <= 1 end attr_reader :user diff --git a/app/presenters/account_show_presenter.rb b/app/presenters/account_show_presenter.rb index 7b81f0936c1..34cb74edcf1 100644 --- a/app/presenters/account_show_presenter.rb +++ b/app/presenters/account_show_presenter.rb @@ -120,7 +120,7 @@ def personal_key_generated_at def header_personalization return decrypted_pii.first_name if decrypted_pii.present? - EmailContext.new(user).last_sign_in_email_address.email + user.last_sign_in_email_address.email end def totp_content diff --git a/app/presenters/openid_connect_user_info_presenter.rb b/app/presenters/openid_connect_user_info_presenter.rb index 817365ae48a..b918b74a681 100644 --- a/app/presenters/openid_connect_user_info_presenter.rb +++ b/app/presenters/openid_connect_user_info_presenter.rb @@ -57,10 +57,6 @@ def all_emails_from_sp_identity(identity) identity.user.confirmed_email_addresses.map(&:email) end - def email_context - @email_context ||= EmailContext.new(identity.user) - end - def ial2_attributes { given_name: stringify_attr(ial2_data.first_name), diff --git a/app/services/displayable_pii_formatter.rb b/app/services/displayable_pii_formatter.rb index 1bc6399fd0b..b6ef5d93683 100644 --- a/app/services/displayable_pii_formatter.rb +++ b/app/services/displayable_pii_formatter.rb @@ -41,7 +41,7 @@ def email if @selected_email_id current_user.confirmed_email_addresses.find(@selected_email_id).email else - EmailContext.new(current_user).last_sign_in_email_address.email + current_user.last_sign_in_email_address.email end end diff --git a/config/initializers/saml_idp.rb b/config/initializers/saml_idp.rb index 0a21994ef18..697348c76a8 100644 --- a/config/initializers/saml_idp.rb +++ b/config/initializers/saml_idp.rb @@ -27,7 +27,7 @@ config.name_id.formats = { persistent: ->(principal) { principal.asserted_attributes[:uuid][:getter].call(principal) }, - email_address: ->(principal) { EmailContext.new(principal).last_sign_in_email_address.email }, + email_address: ->(principal) { principal.last_sign_in_email_address.email }, } ## Technical contact ## diff --git a/spec/decorators/email_context_spec.rb b/spec/decorators/email_context_spec.rb deleted file mode 100644 index daae915bcbe..00000000000 --- a/spec/decorators/email_context_spec.rb +++ /dev/null @@ -1,23 +0,0 @@ -require 'rails_helper' - -RSpec.describe EmailContext do - let(:user) { create(:user, :fully_registered) } - - subject { described_class.new(user) } - - describe '#last_sign_in_email_address' do - it 'returns the email with the most recent last_sign_in_at date' do - last_sign_in_email_address = create(:email_address, user: user, last_sign_in_at: 1.day.ago) - create(:email_address, user: user, last_sign_in_at: 2.days.ago) - - expect(subject.last_sign_in_email_address).to eq(last_sign_in_email_address) - end - - it 'does not return an email with a null last_sign_in_at date' do - last_sign_in_email_address = create(:email_address, user: user, last_sign_in_at: 1.day.ago) - create(:email_address, user: user, last_sign_in_at: nil) - - expect(subject.last_sign_in_email_address).to eq(last_sign_in_email_address) - end - end -end diff --git a/spec/services/attribute_asserter_spec.rb b/spec/services/attribute_asserter_spec.rb index 13a0934adbb..59055c719aa 100644 --- a/spec/services/attribute_asserter_spec.rb +++ b/spec/services/attribute_asserter_spec.rb @@ -272,8 +272,7 @@ end it 'does not create a getter function for IAL1 attributes' do - expected_email = EmailContext.new(user).last_sign_in_email_address.email - expect(get_asserted_attribute(user, :email)).to eq expected_email + expect(get_asserted_attribute(user, :email)).to eq user.last_sign_in_email_address.email end it 'gets UUID from Service Provider' do @@ -293,8 +292,8 @@ end it 'does not create a getter function for IAL1 attributes' do - expected_email = EmailContext.new(user).last_sign_in_email_address.email - expect(get_asserted_attribute(user, :email)).to eq expected_email + expect(get_asserted_attribute(user, :email)) + .to eq user.last_sign_in_email_address.email end it 'gets UUID from Service Provider' do