Raise a RoutingError for unknown foodcoop in multi_coop_installs

This gives the correct error code for request to files in the root
folder (e.g. /apple-touch-icon.png), which do not exist.
This commit is contained in:
Patrick Gansterer 2017-09-23 10:17:33 +02:00
parent 0edc780ec7
commit 52dc7b1387
3 changed files with 23 additions and 15 deletions

View file

@ -177,19 +177,16 @@ class ApplicationController < ActionController::Base
# It uses the subdomain to select the appropriate section in the config files # It uses the subdomain to select the appropriate section in the config files
# Use this method as a before filter (first filter!) in ApplicationController # Use this method as a before filter (first filter!) in ApplicationController
def select_foodcoop def select_foodcoop
if FoodsoftConfig[:multi_coop_install] return unless FoodsoftConfig[:multi_coop_install]
if params[:foodcoop].present?
begin foodcoop = params[:foodcoop]
# Set Config and database connection if foodcoop.blank?
FoodsoftConfig.select_foodcoop params[:foodcoop]
rescue => error
FoodsoftConfig.select_default_foodcoop
redirect_to root_url, alert: error.message
end
else
FoodsoftConfig.select_default_foodcoop FoodsoftConfig.select_default_foodcoop
redirect_to root_url redirect_to root_url
end elsif FoodsoftConfig.allowed_foodcoop? foodcoop
FoodsoftConfig.select_foodcoop foodcoop
else
raise ActionController::RoutingError.new 'Foodcoop Not Found'
end end
end end

View file

@ -11,9 +11,16 @@ class ErrorsController < ApplicationController
private private
def select_foodcoop
foodcoop = params[:foodcoop]
if FoodsoftConfig.allowed_foodcoop? foodcoop
FoodsoftConfig.select_foodcoop foodcoop
else
FoodsoftConfig.select_default_foodcoop
end
end
def current_layout def current_layout
# Need foodcoop for `current_user`, even though it may not be retrieved from the url.
params[:foodcoop] ||= session[:scope]
current_user ? 'application' : 'login' current_user ? 'application' : 'login'
end end

View file

@ -159,6 +159,10 @@ class FoodsoftConfig
end end
end end
def allowed_foodcoop?(foodcoop)
foodcoops.include? foodcoop
end
# @return [Boolean] Whether this key may be set in the database # @return [Boolean] Whether this key may be set in the database
def allowed_key?(key) def allowed_key?(key)
# fast check for keys without nesting # fast check for keys without nesting