I am using AuthLogic and the subdomain method that dhh covers this blog post , everything works fine and, as expected. I am trying to figure out how to create a similar subdomain, such as "admin" or "host", which will have user authentication from AuthLogic (this may be trivial and not necessarily mentioned) that will manage the subdomains. Thus, basically, all subdomains will operate normally, except admin.site.com, which will go to their own controller and layout.
dhh suggested just throwing an exception for redirection, but I'm not sure where this happens, it seemed to me not so easy, any ideas?
EDIT
I think the fact that I use AuthLogic is important here because the subdomain logic does not redirect users anywhere, after authentication AuthLogic sends the user to / account - so my question may be related to how to tell AuthLogic to another place if the user is the root user, logs in to the administrator subdomain.
Here is the code we have implemented so far
Company model
class Company < ActiveRecord::Base
has_many :users
has_many :brands, :dependent => :destroy
validates_presence_of :name, :phone, :subdomain
validates_format_of :subdomain, :with => /^[A-Za-z0-9-]+$/, :message => 'The subdomain can only contain alphanumeric characters and dashes.', :allow_blank => true
validates_uniqueness_of :subdomain, :case_sensitive => false
validates_exclusion_of :format, :in => %w( support blog billing help api www host admin manage ryan jeff allie), :message => "Subdomain {{value}} is not allowed."
before_validation :downcase_subdomain
protected
def downcase_subdomain
self.subdomain.downcase! if attribute_present?("subdomain")
end
end
Module SubdomainCompanies
module SubdomainCompanies
def self.included( controller )
controller.helper_method(:company_domain, :company_subdomain, :company_url, :company_account, :default_company_subdomain, :default_company_url)
end
protected
def default_company_subdomain
''
end
def company_url( company_subdomain = default_company_subdomain, use_ssl = request.ssl? )
http_protocol(use_ssl) + company_host(company_subdomain)
end
def company_host( subdomain )
company_host = ''
company_host << subdomain + '.'
company_host << company_domain
end
def company_domain
company_domain = ''
company_domain << request.domain + request.port_string
end
def company_subdomain
request.subdomains.first || ''
end
def default_company_url( use_ssl = request.ssl? )
http_protocol(use_ssl) + company_domain
end
def current_company
Company.find_by_subdomain(company_subdomain)
end
def http_protocol( use_ssl = request.ssl? )
(use_ssl ? "https://" : "http://")
end
end
Application controller
class ApplicationController < ActionController::Base
include SubdomainCompanies
rescue_from 'Acl9::AccessDenied', :with => :access_denied
helper :all
protect_from_forgery
helper_method :current_user_session, :current_user, :current_company_name
filter_parameter_logging :password, :password_confirmation
before_filter :check_company_status
protected
def public_site?
company_subdomain == default_company_subdomain
end
def current_layout_name
public_site? ? 'public' : 'login'
end
def check_company_status
unless company_subdomain == default_company_subdomain
redirect_to default_company_url if current_company.nil?
end
end
end