2010-04-11 23:59:18 +00:00
|
|
|
module Locomotive
|
2010-07-22 22:10:40 +00:00
|
|
|
module Routing
|
2010-04-11 23:59:18 +00:00
|
|
|
module SiteDispatcher
|
2010-07-22 22:10:40 +00:00
|
|
|
|
2010-06-16 14:43:29 +00:00
|
|
|
extend ActiveSupport::Concern
|
2010-07-22 22:10:40 +00:00
|
|
|
|
2010-06-16 14:43:29 +00:00
|
|
|
included do
|
2010-07-22 22:10:40 +00:00
|
|
|
if self.respond_to?(:before_filter)
|
|
|
|
before_filter :fetch_site
|
|
|
|
|
|
|
|
helper_method :current_site
|
|
|
|
end
|
2010-04-11 23:59:18 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
module InstanceMethods
|
2010-07-22 22:10:40 +00:00
|
|
|
|
2010-04-11 23:59:18 +00:00
|
|
|
protected
|
2010-07-22 22:10:40 +00:00
|
|
|
|
2010-04-11 23:59:18 +00:00
|
|
|
def fetch_site
|
2011-07-26 19:20:03 +00:00
|
|
|
Locomotive.log "[fetch site] host = #{request.host} / #{request.env['HTTP_HOST']}"
|
2011-04-01 00:34:19 +00:00
|
|
|
|
2011-04-03 23:59:41 +00:00
|
|
|
if Locomotive.config.multi_sites?
|
2011-04-01 00:34:19 +00:00
|
|
|
@current_site ||= Site.match_domain(request.host).first
|
|
|
|
else
|
|
|
|
@current_site ||= Site.first
|
|
|
|
end
|
2010-04-11 23:59:18 +00:00
|
|
|
end
|
2010-07-22 22:10:40 +00:00
|
|
|
|
2010-04-11 23:59:18 +00:00
|
|
|
def current_site
|
2010-05-10 22:39:52 +00:00
|
|
|
@current_site || fetch_site
|
2010-04-11 23:59:18 +00:00
|
|
|
end
|
2010-07-22 22:10:40 +00:00
|
|
|
|
2010-04-11 23:59:18 +00:00
|
|
|
def require_site
|
2011-01-03 10:50:09 +00:00
|
|
|
return true if current_site
|
|
|
|
|
2010-10-27 00:11:44 +00:00
|
|
|
redirect_to admin_installation_url and return false if Account.count == 0 || Site.count == 0
|
|
|
|
|
2011-01-03 10:50:09 +00:00
|
|
|
render_no_site_error and return false
|
2010-09-21 11:45:34 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def render_no_site_error
|
2011-10-29 06:35:23 +00:00
|
|
|
render :template => '/admin/errors/no_site', :layout => false, :status => :not_found
|
2010-04-11 23:59:18 +00:00
|
|
|
end
|
2010-07-22 22:10:40 +00:00
|
|
|
|
2010-05-10 22:39:52 +00:00
|
|
|
def validate_site_membership
|
2011-02-22 09:34:27 +00:00
|
|
|
return true if current_site.present? && current_site.accounts.include?(current_admin)
|
|
|
|
|
|
|
|
sign_out(current_admin)
|
|
|
|
flash[:alert] = I18n.t(:no_membership, :scope => [:devise, :failure, :admin])
|
|
|
|
redirect_to new_admin_session_url and return false
|
2010-05-10 22:39:52 +00:00
|
|
|
end
|
2010-07-22 22:10:40 +00:00
|
|
|
|
2010-04-11 23:59:18 +00:00
|
|
|
end
|
2010-07-22 22:10:40 +00:00
|
|
|
|
|
|
|
end
|
|
|
|
end
|
2010-07-23 20:09:54 +00:00
|
|
|
end
|