Translate asterisks in ip addresses to mask format so people can enter things like 127.*.*.* in screened ip addresses form

This commit is contained in:
Neil Lalonde
2014-02-18 13:00:55 -05:00
parent 69e0342d8f
commit d0ecccb7e4
2 changed files with 55 additions and 3 deletions
+16 -1
View File
@@ -18,7 +18,22 @@ class ScreenedIpAddress < ActiveRecord::Base
# In Rails 4.0.1, an exception is raised before validation happens, so we need this hack for
# inet/cidr columns:
def ip_address=(val)
write_attribute(:ip_address, val)
if val.nil?
self.errors.add(:ip_address, :invalid)
return
end
num_wildcards = val.count('*')
if num_wildcards == 0
write_attribute(:ip_address, val)
else
v = val.gsub(/\/.*/, '')
if v[v.index('*')..-1] =~ /[^\.\*]/
self.errors.add(:ip_address, :invalid)
return
end
write_attribute(:ip_address, "#{v.gsub('*', '0')}/#{32 - (num_wildcards * 8)}")
end
# this gets even messier, Ruby 1.9.2 raised a different exception to Ruby 2.0.0
# handle both exceptions