50 lines
1.8 KiB
Ruby
50 lines
1.8 KiB
Ruby
|
# frozen_string_literal: true
|
||
|
|
||
|
class DomainConstraint
|
||
|
attr_reader :domain, :subdomain
|
||
|
|
||
|
DEFAULT_DOMAIN = nil # "yiff.rest".freeze
|
||
|
USE_DEV_HOST = Rails.env.development?
|
||
|
|
||
|
def dev_host
|
||
|
"#{domain.tr('.', '-')}.websites.containers.local"
|
||
|
end
|
||
|
|
||
|
def initialize(domain, subdomain = nil)
|
||
|
@domain = domain
|
||
|
@subdomain = subdomain
|
||
|
end
|
||
|
|
||
|
def resolve_domains(current_host, level = 1)
|
||
|
current_subdomains = nil
|
||
|
if current_host.scan(".").length > level
|
||
|
parts = current_host.split(".")
|
||
|
current_host = parts.pop(level + 1).join(".")
|
||
|
current_subdomains = parts.join(".") if parts.length >= 1
|
||
|
end
|
||
|
|
||
|
[current_host, current_subdomains]
|
||
|
end
|
||
|
|
||
|
def matches?(request)
|
||
|
host = domain
|
||
|
parse = request.domain
|
||
|
parse = "#{request.subdomains.join('.')}.#{request.domain}" unless request.subdomains.empty?
|
||
|
current_host, current_subdomains = resolve_domains(parse)
|
||
|
if Rails.env.development? && USE_DEV_HOST
|
||
|
if DEFAULT_DOMAIN.present?
|
||
|
current_host, current_subdomains = resolve_domains(DEFAULT_DOMAIN)
|
||
|
elsif request.query_parameters[:domain].present?
|
||
|
current_host, current_subdomains = resolve_domains(request.query_parameters[:domain])
|
||
|
elsif current_host.scan(".").length > 3
|
||
|
current_host, current_subdomains = resolve_domains(current_host, 3)
|
||
|
end
|
||
|
|
||
|
current_host = dev_host if current_host == domain
|
||
|
host = dev_host
|
||
|
end
|
||
|
Rails.logger.info("Host: #{host}; Subdomain: #{subdomain}; Current Host: #{current_host}; Current Subdomain: #{current_subdomains}; Matches (Domain): #{current_host == host}; Matches (Subdomain): #{current_subdomains == subdomain || subdomain&.to_sym == :any}") if Rails.env.development?
|
||
|
current_host == host && (current_subdomains == subdomain || subdomain&.to_sym == :any)
|
||
|
end
|
||
|
end
|