Skip to content

Commit

Permalink
Allow filtering accounts by IP mask
Browse files Browse the repository at this point in the history
  • Loading branch information
Gargron committed Nov 25, 2018
1 parent ac00caf commit 5893bee
Show file tree
Hide file tree
Showing 4 changed files with 1 addition and 31 deletions.
2 changes: 1 addition & 1 deletion app/models/account_filter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ def scope_for(key, value)
when 'email'
accounts_with_users.merge User.matches_email(value)
when 'ip'
valid_ip?(value) ? accounts_with_users.merge(User.with_recent_ip_address(value)) : Account.none
valid_ip?(value) ? accounts_with_users.where('users.current_sign_in_ip <<= ?', value) : Account.none
when 'staff'
accounts_with_users.merge User.staff
else
Expand Down
1 change: 0 additions & 1 deletion app/models/user.rb
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,6 @@ 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: false }) }
scope :matches_email, ->(value) { where(arel_table[:email].matches("#{value}%")) }
scope :with_recent_ip_address, ->(value) { where(arel_table[:current_sign_in_ip].eq(value).or(arel_table[:last_sign_in_ip].eq(value))) }

before_validation :sanitize_languages

Expand Down
17 changes: 0 additions & 17 deletions spec/models/account_filter_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,23 +17,6 @@
end
end

describe 'when an IP address is provided' do
it 'filters with IP when valid' do
filter = described_class.new(ip: '127.0.0.1')
allow(User).to receive(:with_recent_ip_address).and_return(User.none)

filter.results
expect(User).to have_received(:with_recent_ip_address).with('127.0.0.1')
end

it 'skips IP when invalid' do
filter = described_class.new(ip: '345.678.901.234')
expect(User).not_to receive(:with_recent_ip_address)

filter.results
end
end

describe 'with valid params' do
it 'combines filters on Account' do
filter = described_class.new(
Expand Down
12 changes: 0 additions & 12 deletions spec/models/user_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -89,18 +89,6 @@
expect(User.matches_email('specified')).to match_array([specified])
end
end

describe 'with_recent_ip_address' do
it 'returns a relation of users who is, or was at last time, online with the given IP address' do
specifieds = [
Fabricate(:user, current_sign_in_ip: '0.0.0.42', last_sign_in_ip: '0.0.0.0'),
Fabricate(:user, current_sign_in_ip: nil, last_sign_in_ip: '0.0.0.42')
]
Fabricate(:user, current_sign_in_ip: '0.0.0.0', last_sign_in_ip: '0.0.0.0')

expect(User.with_recent_ip_address('0.0.0.42')).to match_array(specifieds)
end
end
end

let(:account) { Fabricate(:account, username: 'alice') }
Expand Down

0 comments on commit 5893bee

Please sign in to comment.