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 Apr 28, 2022
1 parent 959cff7 commit fa059ec
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 0 deletions.
6 changes: 6 additions & 0 deletions app/models/user/sign_in.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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?
Expand Down
5 changes: 5 additions & 0 deletions app/views/admin/users/_sign_in_table.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,11 @@
</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>
Expand Down
32 changes: 32 additions & 0 deletions spec/models/user/sign_in_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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 }

Expand Down

0 comments on commit fa059ec

Please sign in to comment.