# frozen_string_literal: true require 'core_ext/ipaddr' # == Schema Information # # Table name: subnet_bans # # id :integer not null, primary key # enabled :boolean default(TRUE), not null # note :string # reason :string not null # specification :inet # valid_until :datetime not null # created_at :datetime not null # updated_at :datetime not null # banning_user_id :integer not null # generated_ban_id :string not null # # Indexes # # index_subnet_bans_on_banning_user_id (banning_user_id) # index_subnet_bans_on_created_at (created_at) # # Foreign Keys # # fk_rails_... (banning_user_id => users.id) ON DELETE => restrict ON UPDATE => cascade # class SubnetBan < Ban # rolify resourcify belongs_to :banning_user, inverse_of: :subnet_bans_enacted, class_name: 'User', optional: true validates :specification, presence: true def id_prefix 'S' end def self.banned?(address) if address.nil? Rails.logger.warn 'Nil remote address; should not happen.' return false end address = IPAddr.new(address) if address.is_a?(String) valid.where('specification >>= ?', address.to_cidr).detect(&:enabled) || false end def self.ban_exists?(address) address = IPAddr.new(address) if address.is_a?(String) valid.where('specification >>= ?', address.to_cidr).present? end end