You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

41 lines
979 B

  1. # frozen_string_literal: true
  2. # == Schema Information
  3. #
  4. # Table name: ip_blocks
  5. #
  6. # id :bigint(8) not null, primary key
  7. # created_at :datetime not null
  8. # updated_at :datetime not null
  9. # expires_at :datetime
  10. # ip :inet default(#<IPAddr: IPv4:0.0.0.0/255.255.255.255>), not null
  11. # severity :integer default(NULL), not null
  12. # comment :text default(""), not null
  13. #
  14. class IpBlock < ApplicationRecord
  15. CACHE_KEY = 'blocked_ips'
  16. include Expireable
  17. enum severity: {
  18. sign_up_requires_approval: 5000,
  19. no_access: 9999,
  20. }
  21. validates :ip, :severity, presence: true
  22. after_commit :reset_cache
  23. class << self
  24. def blocked?(remote_ip)
  25. blocked_ips_map = Rails.cache.fetch(CACHE_KEY) { FastIpMap.new(IpBlock.where(severity: :no_access).pluck(:ip)) }
  26. blocked_ips_map.include?(remote_ip)
  27. end
  28. end
  29. private
  30. def reset_cache
  31. Rails.cache.delete(CACHE_KEY)
  32. end
  33. end