Fix #4736: Display network prefix length (if present) in API key IP whitelist.
This commit is contained in:
23
app/logical/danbooru/ip_address.rb
Normal file
23
app/logical/danbooru/ip_address.rb
Normal file
@@ -0,0 +1,23 @@
|
||||
# A wrapper around the IPAddress gem that adds some extra utility methods.
|
||||
#
|
||||
# https://github.com/ipaddress-gem/ipaddress
|
||||
|
||||
module Danbooru
|
||||
class IpAddress
|
||||
attr_reader :ip_address
|
||||
delegate_missing_to :ip_address
|
||||
|
||||
def initialize(string)
|
||||
@ip_address = ::IPAddress.parse(string)
|
||||
end
|
||||
|
||||
# "1.2.3.4/24" if the address is a subnet, "1.2.3.4" otherwise.
|
||||
def to_s
|
||||
ip_address.size > 1 ? ip_address.to_string : ip_address.to_s
|
||||
end
|
||||
|
||||
def inspect
|
||||
"#<Danbooru::IpAddress #{to_s}>"
|
||||
end
|
||||
end
|
||||
end
|
||||
@@ -1,9 +1,14 @@
|
||||
# See also config/initializers/types.rb
|
||||
|
||||
require "active_record/connection_adapters/postgresql_adapter"
|
||||
|
||||
class IpAddressType < ActiveRecord::ConnectionAdapters::PostgreSQL::OID::Inet
|
||||
def cast(value)
|
||||
super(IPAddress.parse(value))
|
||||
return nil if value.blank?
|
||||
super(Danbooru::IpAddress.new(value))
|
||||
end
|
||||
|
||||
def serialize(value)
|
||||
value.to_string
|
||||
value&.to_string
|
||||
end
|
||||
end
|
||||
|
||||
Reference in New Issue
Block a user