FEATURE: screened IP address range can be entered like 192.* instead of 192.*.*.*

This commit is contained in:
Neil Lalonde 2014-09-24 12:05:29 -04:00
parent b1567488b7
commit d96acdeafa
2 changed files with 7 additions and 2 deletions

View File

@ -29,12 +29,15 @@ class ScreenedIpAddress < ActiveRecord::Base
if num_wildcards == 0 if num_wildcards == 0
write_attribute(:ip_address, val) write_attribute(:ip_address, val)
else else
v = val.gsub(/\/.*/, '') v = val.gsub(/\/.*/, '') # strip ranges like "/16" from the end if present
if v[v.index('*')..-1] =~ /[^\.\*]/ if v[v.index('*')..-1] =~ /[^\.\*]/
self.errors.add(:ip_address, :invalid) self.errors.add(:ip_address, :invalid)
return return
end end
write_attribute(:ip_address, "#{v.gsub('*', '0')}/#{32 - (num_wildcards * 8)}") parts = v.split('.')
(4 - parts.size).times { parts << '*' } # support strings like 192.*
v = parts.join('.')
write_attribute(:ip_address, "#{v.gsub('*', '0')}/#{32 - (v.count('*') * 8)}")
end end
# this gets even messier, Ruby 1.9.2 raised a different exception to Ruby 2.0.0 # this gets even messier, Ruby 1.9.2 raised a different exception to Ruby 2.0.0

View File

@ -76,6 +76,8 @@ describe ScreenedIpAddress do
test_good_value("123.12.*.*", "123.12.0.0/16") test_good_value("123.12.*.*", "123.12.0.0/16")
test_good_value("123.12.1.*", "123.12.1.0/24") test_good_value("123.12.1.*", "123.12.1.0/24")
test_good_value("123.12.*.*/16", "123.12.0.0/16") test_good_value("123.12.*.*/16", "123.12.0.0/16")
test_good_value("123.12.*", "123.12.0.0/16")
test_good_value("123.*", "123.0.0.0/8")
end end
it "handles bad input" do it "handles bad input" do