Skip to content

Commit

Permalink
Show count of other users signing in with an IP
Browse files Browse the repository at this point in the history
Helps to identify possible misusers.
  • Loading branch information
garethrees committed Feb 25, 2022
1 parent 04b4d40 commit a315a9d
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 0 deletions.
4 changes: 4 additions & 0 deletions app/models/user/sign_in.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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?
Expand Down
4 changes: 4 additions & 0 deletions app/views/admin/users/_sign_in_table.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,10 @@
</td>

<td><%= admin_date(sign_in.created_at, ago_only: true) %></td>

<td>
<strong><%= sign_in.other_users.size %></strong> others using this IP
</td>
</tr>
<% end %>
</table>
23 changes: 23 additions & 0 deletions spec/models/user/sign_in_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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 }

Expand Down

0 comments on commit a315a9d

Please sign in to comment.