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

Version Path
decidim-core-0.27.9 lib/decidim/middleware/current_organization.rb
decidim-core-0.27.8 lib/decidim/middleware/current_organization.rb
decidim-core-0.27.7 lib/decidim/middleware/current_organization.rb
decidim-core-0.27.6 lib/decidim/middleware/current_organization.rb
decidim-core-0.26.10 lib/decidim/middleware/current_organization.rb
decidim-core-0.26.9 lib/decidim/middleware/current_organization.rb
decidim-core-0.27.5 lib/decidim/middleware/current_organization.rb
decidim-core-0.26.8 lib/decidim/middleware/current_organization.rb
decidim-core-0.27.4 lib/decidim/middleware/current_organization.rb
decidim-core-0.27.3 lib/decidim/middleware/current_organization.rb
decidim-core-0.26.7 lib/decidim/middleware/current_organization.rb
decidim-core-0.26.5 lib/decidim/middleware/current_organization.rb
decidim-core-0.27.2 lib/decidim/middleware/current_organization.rb
decidim-core-0.27.1 lib/decidim/middleware/current_organization.rb
decidim-core-0.26.4 lib/decidim/middleware/current_organization.rb
decidim-core-0.27.0 lib/decidim/middleware/current_organization.rb
decidim-core-0.26.3 lib/decidim/middleware/current_organization.rb
decidim-core-0.27.0.rc2 lib/decidim/middleware/current_organization.rb
decidim-core-0.27.0.rc1 lib/decidim/middleware/current_organization.rb
decidim-core-0.26.2 lib/decidim/middleware/current_organization.rb