Sha256: 7df078f4e77d4502ec7c732fd58117c8de7ab5ed896f577b06e4a55f07933281
Contents?: true
Size: 1.44 KB
Versions: 31
Compression:
Stored size: 1.44 KB
Contents
# frozen_string_literal: true module Decidim module Middleware # A middleware that enhances the request with the current organization based # on the hostname. class CurrentOrganization # Initializes the Rack Middleware. # # app - The Rack application def initialize(app) @app = app end # Main entry point for a Rack Middleware. # # env - A Hash. def call(env) organization = detect_current_organization(env) if organization env["decidim.current_organization"] = organization @app.call(env) else organization = find_secondary_host_org(env) return @app.call(env) unless organization location = new_location_for(env, organization.host) [301, { "Location" => location, "Content-Type" => "text/html", "Content-Length" => "0" }, []] end end private def detect_current_organization(env) host = host_for(env) Decidim::Organization.find_by(host: host) end def find_secondary_host_org(env) host = host_for(env) Decidim::Organization.find_by("? = ANY(secondary_hosts)", host) end def host_for(env) Rack::Request.new(env).host.downcase end def new_location_for(env, host) request = Rack::Request.new(env) url = URI(request.url) url.host = host url.to_s end end end end
Version data entries
31 entries across 31 versions & 1 rubygems