diff --git a/app/models/user/sign_in.rb b/app/models/user/sign_in.rb index 790f3c2c749..d0f49aa9579 100644 --- a/app/models/user/sign_in.rb +++ b/app/models/user/sign_in.rb @@ -38,6 +38,12 @@ def self.retention_days AlaveteliConfiguration.user_sign_in_activity_retention_days end + def other_users + User.distinct.joins(:sign_ins). + where(user_sign_ins: { ip: ip }). + where.not(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 e815dfddca4..0d2358a97ec 100644 --- a/app/views/admin/users/_sign_in_table.html.erb +++ b/app/views/admin/users/_sign_in_table.html.erb @@ -22,6 +22,11 @@ <%= 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 3b8cb457df3..e8913a4c452 100644 --- a/spec/models/user/sign_in_spec.rb +++ b/spec/models/user/sign_in_spec.rb @@ -113,6 +113,38 @@ 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(:user_1) { FactoryBot.create(:user) } + let(:user_2) { FactoryBot.create(:user) } + + let!(:sign_in_1) do + FactoryBot.create(:user_sign_in, user: user_1, ip: '1.1.1.1') + end + + let!(:sign_in_2) do + FactoryBot.create(:user_sign_in, user: user_2, ip: '1.1.1.1') + end + + 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([user_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 }