domain_allow.rb 935 B

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. # frozen_string_literal: true
  2. # == Schema Information
  3. #
  4. # Table name: domain_allows
  5. #
  6. # id :bigint(8) not null, primary key
  7. # domain :string default(""), not null
  8. # created_at :datetime not null
  9. # updated_at :datetime not null
  10. #
  11. class DomainAllow < ApplicationRecord
  12. include Paginable
  13. include DomainNormalizable
  14. include DomainMaterializable
  15. validates :domain, presence: true, uniqueness: true, domain: true
  16. scope :matches_domain, ->(value) { where(arel_table[:domain].matches("%#{value}%")) }
  17. def to_log_human_identifier
  18. domain
  19. end
  20. class << self
  21. def allowed?(domain)
  22. !rule_for(domain).nil?
  23. end
  24. def allowed_domains
  25. select(:domain)
  26. end
  27. def rule_for(domain)
  28. return if domain.blank?
  29. uri = Addressable::URI.new.tap { |u| u.host = domain.gsub(/[\/]/, '') }
  30. find_by(domain: uri.normalized_host)
  31. end
  32. end
  33. end