Add User#email_domain method to extract domain from email address (#35159)

This commit is contained in:
Matt Jankowski 2025-06-25 03:22:19 -04:00 committed by GitHub
parent 8ba1487f30
commit f852da50f6
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
5 changed files with 38 additions and 3 deletions

View File

@ -163,6 +163,7 @@ class Account < ApplicationRecord
after_update_commit :trigger_update_webhooks after_update_commit :trigger_update_webhooks
delegate :email, delegate :email,
:email_domain,
:unconfirmed_email, :unconfirmed_email,
:current_sign_in_at, :current_sign_in_at,
:created_at, :created_at,

View File

@ -223,6 +223,12 @@ class User < ApplicationRecord
end end
end end
def email_domain
Mail::Address.new(email).domain
rescue Mail::Field::ParseError
nil
end
def update_sign_in!(new_sign_in: false) def update_sign_in!(new_sign_in: false)
old_current = current_sign_in_at old_current = current_sign_in_at
new_current = Time.now.utc new_current = Time.now.utc

View File

@ -25,7 +25,7 @@
%td.accounts-table__extra %td.accounts-table__extra
- if account.local? - if account.local?
- if account.user_email - if account.user_email
= link_to account.user_email.split('@').last, admin_accounts_path(email: "%@#{account.user_email.split('@').last}"), title: account.user_email = link_to account.user_email_domain, admin_accounts_path(email: "%@#{account.user_email_domain}"), title: account.user_email
- else - else
\- \-
%br/ %br/

View File

@ -22,10 +22,10 @@
%td{ rowspan: can?(:create, :email_domain_block) ? 3 : 2 }= account.user_email %td{ rowspan: can?(:create, :email_domain_block) ? 3 : 2 }= account.user_email
%td= table_link_to 'edit', t('admin.accounts.change_email.label'), admin_account_change_email_path(account.id) if can?(:change_email, account.user) %td= table_link_to 'edit', t('admin.accounts.change_email.label'), admin_account_change_email_path(account.id) if can?(:change_email, account.user)
%tr %tr
%td= table_link_to 'search', t('admin.accounts.search_same_email_domain'), admin_accounts_path(email: "%@#{account.user_email.split('@').last}") %td= table_link_to 'search', t('admin.accounts.search_same_email_domain'), admin_accounts_path(email: "%@#{account.user_email_domain}")
- if can?(:create, :email_domain_block) - if can?(:create, :email_domain_block)
%tr %tr
%td= table_link_to 'hide_source', t('admin.accounts.add_email_domain_block'), new_admin_email_domain_block_path(_domain: account.user_email.split('@').last) %td= table_link_to 'hide_source', t('admin.accounts.add_email_domain_block'), new_admin_email_domain_block_path(_domain: account.user_email_domain)
- if account.user_unconfirmed_email.present? - if account.user_unconfirmed_email.present?
%tr %tr
%th= t('admin.accounts.unconfirmed_email') %th= t('admin.accounts.unconfirmed_email')

View File

@ -166,6 +166,34 @@ RSpec.describe User do
end end
end end
describe '#email_domain' do
subject { described_class.new(email: email).email_domain }
context 'when value is nil' do
let(:email) { nil }
it { is_expected.to be_nil }
end
context 'when value is blank' do
let(:email) { '' }
it { is_expected.to be_nil }
end
context 'when value has valid domain' do
let(:email) { 'user@host.example' }
it { is_expected.to eq('host.example') }
end
context 'when value has no split' do
let(:email) { 'user$host.example' }
it { is_expected.to be_nil }
end
end
describe '#update_sign_in!' do describe '#update_sign_in!' do
context 'with an existing user' do context 'with an existing user' do
let!(:user) { Fabricate :user, last_sign_in_at: 10.days.ago, current_sign_in_at: 1.hour.ago, sign_in_count: 123 } let!(:user) { Fabricate :user, last_sign_in_at: 10.days.ago, current_sign_in_at: 1.hour.ago, sign_in_count: 123 }