Files
danbooru/app/models/ip_ban.rb
BrokenEagle 63b3503bfc Add ability to use nested only parameter
- The only string works much the same as before with its comma separation
-- Nested includes are indicated with square brackets "[ ]"
-- The nested include is the value immediately preceding the square brackets
-- The only string is the comma separated string inside those brackets
- Default includes are split between format types when necessary
-- This prevents unnecessary includes from being added on page load
- Available includes are those items which are allowed to be accessible to the user
-- Some aren't because they are sensitive, such as the creator of a flag
-- Some aren't because the number of associated items is too large
- The amount of times the same model can be included to prevent recursions
-- One exception is the root model may include the same model once
--- e.g. the user model can include the inviter which is also the user model
-- Another exception is if the include is a has_many association
--- e.g. artist urls can include the artist, and then artist urls again
2020-02-12 23:58:53 +00:00

50 lines
1.4 KiB
Ruby

class IpBan < ApplicationRecord
belongs_to :creator, class_name: "User"
validate :validate_ip_addr
validates_presence_of :reason
validates_uniqueness_of :ip_addr
after_create { ModAction.log("#{creator.name} created ip ban for #{ip_addr}", :ip_ban_create) }
after_destroy { ModAction.log("#{creator.name} deleted ip ban for #{ip_addr}", :ip_ban_delete) }
def self.is_banned?(ip_addr)
where("ip_addr >>= ?", ip_addr).exists?
end
def self.search(params)
q = super
q = q.search_attributes(params, :creator, :reason)
if params[:ip_addr].present?
q = q.where("ip_addr = ?", params[:ip_addr])
end
q.apply_default_order(params)
end
def validate_ip_addr
if ip_addr.blank?
errors[:ip_addr] << "is invalid"
elsif ip_addr.ipv4? && ip_addr.prefix < 24
errors[:ip_addr] << "may not have a subnet bigger than /24"
elsif ip_addr.ipv6? && ip_addr.prefix < 64
errors[:ip_addr] << "may not have a subnet bigger than /64"
elsif ip_addr.private? || ip_addr.loopback? || ip_addr.link_local?
errors[:ip_addr] << "must be a public address"
end
end
def has_subnet?
(ip_addr.ipv4? && ip_addr.prefix < 32) || (ip_addr.ipv6? && ip_addr.prefix < 128)
end
def subnetted_ip
str = ip_addr.to_s
str += "/" + ip_addr.prefix.to_s if has_subnet?
str
end
def self.available_includes
[:creator]
end
end