diff --git a/app/models/user/sign_in.rb b/app/models/user/sign_in.rb index b8170332913..12e9feeaf3c 100644 --- a/app/models/user/sign_in.rb +++ b/app/models/user/sign_in.rb @@ -32,6 +32,10 @@ def self.retention_days AlaveteliConfiguration.user_sign_in_activity_retention_days end + def other_users + self.class.where(ip: ip).where.not(user_id: user_id) + end + private def create? diff --git a/app/views/admin/users/_sign_in_table.html.erb b/app/views/admin/users/_sign_in_table.html.erb index d4d95fa9d95..f8afd6b9afc 100644 --- a/app/views/admin/users/_sign_in_table.html.erb +++ b/app/views/admin/users/_sign_in_table.html.erb @@ -10,6 +10,10 @@ <%= admin_date(sign_in.created_at, ago_only: true) %> + + + <%= sign_in.other_users.size %> others using this IP + <% end %> diff --git a/spec/models/user/sign_in_spec.rb b/spec/models/user/sign_in_spec.rb index 7e594019930..9633e85c0f2 100644 --- a/spec/models/user/sign_in_spec.rb +++ b/spec/models/user/sign_in_spec.rb @@ -83,6 +83,29 @@ end end + describe '#other_users' do + subject { sign_in.other_users } + + before do + allow(AlaveteliConfiguration). + to receive(:user_sign_in_activity_retention_days).and_return(1) + end + + let(:sign_in_1) { FactoryBot.create(:user_sign_in, ip: '1.1.1.1') } + let(:sign_in_2) { FactoryBot.create(:user_sign_in, ip: '1.1.1.1') } + let(:sign_in_3) { FactoryBot.create(:user_sign_in, ip: '2.2.2.2') } + + context 'when there are other users using the same IP' do + let(:sign_in) { sign_in_1 } + it { is_expected.to match_array([sign_in_2]) } + end + + context 'when there are no other users using the same IP' do + let(:sign_in) { sign_in_3 } + it { is_expected.to be_empty } + end + end + describe '#save' do subject { sign_in.save }