Refactor login activity partial (remove inline ruby) (#28687)

This commit is contained in:
Matt Jankowski 2024-01-11 05:36:27 -05:00 committed by GitHub
parent 204bbf49d2
commit 55802242ce
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 62 additions and 9 deletions

View file

@ -4,4 +4,60 @@ module Admin::SettingsHelper
def captcha_available?
ENV['HCAPTCHA_SECRET_KEY'].present? && ENV['HCAPTCHA_SITE_KEY'].present?
end
def login_activity_title(activity)
t(
"login_activities.#{login_activity_key(activity)}",
method: login_activity_method(activity),
ip: login_activity_ip(activity),
browser: login_activity_browser(activity)
)
end
private
def login_activity_key(activity)
activity.success? ? 'successful_sign_in_html' : 'failed_sign_in_html'
end
def login_activity_method(activity)
content_tag(
:span,
login_activity_method_string(activity),
class: 'target'
)
end
def login_activity_ip(activity)
content_tag(
:span,
activity.ip,
class: 'target'
)
end
def login_activity_browser(activity)
content_tag(
:span,
login_activity_browser_description(activity),
class: 'target',
title: activity.user_agent
)
end
def login_activity_method_string(activity)
if activity.omniauth?
t("auth.providers.#{activity.provider}")
else
t("login_activities.authentication_methods.#{activity.authentication_method}")
end
end
def login_activity_browser_description(activity)
t(
'sessions.description',
browser: t(activity.browser, scope: 'sessions.browsers', default: activity.browser.to_s),
platform: t(activity.platform, scope: 'sessions.platforms', default: activity.platform.to_s)
)
end
end

View file

@ -1,8 +1,3 @@
:ruby
method_str = content_tag(:span, login_activity.omniauth? ? t(login_activity.provider, scope: 'auth.providers') : t(login_activity.authentication_method, scope: 'login_activities.authentication_methods'), class: 'target')
ip_str = content_tag(:span, login_activity.ip, class: 'target')
browser_str = content_tag(:span, t('sessions.description', browser: t("sessions.browsers.#{login_activity.browser}", default: login_activity.browser.to_s), platform: t("sessions.platforms.#{login_activity.platform}", default: login_activity.platform.to_s)), class: 'target', title: login_activity.user_agent)
.log-entry
.log-entry__header
.log-entry__avatar
@ -10,9 +5,6 @@
= fa_icon login_activity.success? ? 'check' : 'times'
.log-entry__content
.log-entry__title
- if login_activity.success?
= t('login_activities.successful_sign_in_html', method: method_str, ip: ip_str, browser: browser_str)
- else
= t('login_activities.failed_sign_in_html', method: method_str, ip: ip_str, browser: browser_str)
= login_activity_title(login_activity)
.log-entry__timestamp
%time.formatted{ datetime: login_activity.created_at.iso8601 }= l(login_activity.created_at)

View file

@ -6,6 +6,7 @@ describe Settings::LoginActivitiesController do
render_views
let!(:user) { Fabricate(:user) }
let!(:login_activity) { Fabricate :login_activity, user: user }
before do
sign_in user, scope: :user
@ -19,6 +20,10 @@ describe Settings::LoginActivitiesController do
it 'returns http success with private cache control headers', :aggregate_failures do
expect(response).to have_http_status(200)
expect(response.headers['Cache-Control']).to include('private, no-store')
expect(response.body)
.to include(login_activity.user_agent)
.and include(login_activity.authentication_method)
.and include(login_activity.ip.to_s)
end
end
end