From ba16d4b4138f12670ce0d056fb025b375dd3fb8d Mon Sep 17 00:00:00 2001 From: Eugen Rochko Date: Mon, 20 Jan 2020 17:00:41 +0100 Subject: Fix search by IP not using alternative browser sessions in admin UI (#12904) --- app/models/user.rb | 1 + 1 file changed, 1 insertion(+) (limited to 'app/models/user.rb') diff --git a/app/models/user.rb b/app/models/user.rb index 7147a9a31..a1753784d 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -93,6 +93,7 @@ class User < ApplicationRecord scope :inactive, -> { where(arel_table[:current_sign_in_at].lt(ACTIVE_DURATION.ago)) } scope :active, -> { confirmed.where(arel_table[:current_sign_in_at].gteq(ACTIVE_DURATION.ago)).joins(:account).where(accounts: { suspended_at: nil }) } scope :matches_email, ->(value) { where(arel_table[:email].matches("#{value}%")) } + scope :matches_ip, ->(value) { left_joins(:session_activations).where('users.current_sign_in_ip <<= ?', value).or(left_joins(:session_activations).where('users.last_sign_in_ip <<= ?', value)).or(left_joins(:session_activations).where('session_activations.ip <<= ?', value)) } scope :emailable, -> { confirmed.enabled.joins(:account).merge(Account.searchable) } before_validation :sanitize_languages -- cgit From 02063c245c8a2a09e741cbce8302edf3ec14feef Mon Sep 17 00:00:00 2001 From: Eugen Rochko Date: Mon, 20 Jan 2020 18:00:54 +0100 Subject: Fix not all of account's active IPs showing up in admin UI (#12909) --- app/models/user.rb | 15 +++++++++++++++ app/views/admin/accounts/show.html.haml | 12 ++++++------ 2 files changed, 21 insertions(+), 6 deletions(-) (limited to 'app/models/user.rb') diff --git a/app/models/user.rb b/app/models/user.rb index a1753784d..a43e63b2e 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -290,6 +290,21 @@ class User < ApplicationRecord setting_display_media == 'hide_all' end + def recent_ips + @recent_ips ||= begin + arr = [] + + session_activations.each do |session_activation| + arr << [session_activation.updated_at, session_activation.ip] + end + + arr << [current_sign_in_at, current_sign_in_ip] if current_sign_in_ip.present? + arr << [last_sign_in_at, last_sign_in_ip] if last_sign_in_ip.present? + + arr.sort_by(&:first).uniq(&:last).reverse! + end + end + protected def send_devise_notification(notification, *args) diff --git a/app/views/admin/accounts/show.html.haml b/app/views/admin/accounts/show.html.haml index 9f1e3816b..1429f56d5 100644 --- a/app/views/admin/accounts/show.html.haml +++ b/app/views/admin/accounts/show.html.haml @@ -139,12 +139,12 @@ %time.formatted{ datetime: @account.created_at.iso8601, title: l(@account.created_at) }= l @account.created_at %td - %tr - %th= t('admin.accounts.most_recent_ip') - %td= @account.user_current_sign_in_ip - %td - - if @account.user_current_sign_in_ip - = table_link_to 'search', t('admin.accounts.search_same_ip'), admin_accounts_path(ip: @account.user_current_sign_in_ip) + - @account.user.recent_ips.each_with_index do |(_, ip), i| + %tr + - if i.zero? + %th{ rowspan: @account.user.recent_ips.size }= t('admin.accounts.most_recent_ip') + %td= ip + %td= table_link_to 'search', t('admin.accounts.search_same_ip'), admin_accounts_path(ip: ip) %tr %th= t('admin.accounts.most_recent_activity') -- cgit