2010-02-19 107 views
3

我正在使用AuthLogic和012h中包含的子域名方法,所有事情都很好,並且和預期的一樣。我試圖弄清楚的是,如何創建一個子域名,如'admin'或'host',這個子域名將擁有一個AuthLogic認證的用戶(這可能是微不足道的,不必提及)將管理子域名。所以基本上,所有的子域名將正常工作,除了admin.site.com這將去自己的控制器和佈局..如何創建一個管理員子域來管理Rails中的子域名

dhh建議只是拋出一個異常重定向,但我不知道哪裏去了,它對我來說似乎不那麼簡單,有什麼想法?

編輯 我認爲我使用AuthLogic的事實在這裏很重要,因爲子域邏輯心不是轉發的用戶在任何地方,一旦被認證AuthLogic向用戶/賬戶 - 所以我的問題可能與我該怎麼辦告訴AuthLogic到不同的點,如果用戶是根用戶,在登錄到管理子域..

這是我們這樣實現的代碼遠

公司型號

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 

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 

    # TODO: need to handle www as well 
    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 

應用控制器

class ApplicationController < ActionController::Base 
    include SubdomainCompanies 

    rescue_from 'Acl9::AccessDenied', :with => :access_denied 

    helper :all # include all helpers, all the time 
    protect_from_forgery # See ActionController::RequestForgeryProtection for details 
    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 
     # TODO: this is where we could check to see if the account is active as well (paid, etc...) 
     redirect_to default_company_url if current_company.nil? 
     end 
    end 
end 

回答

3

調查subdomain-fu它允許您路由到不同的控制器和基於子域的操作。關於這個問題,我做了一個Railscasts Episode

它可能看起來像這樣。

# in routes.rb 
map.manage_companies '', :controller => 'companies', :action => 'index', :conditions => { :subdomain => "admin" } 

這將需要在路由列表中足夠高,所以在它之前沒有其他匹配。

+0

我真的很想避免使用插件,我覺得這對我們簡單的需求來說太過分了。但是,我可能只需要走這條路線 - 如果用戶點擊該網址,而且他們沒有通過身份驗證,子域名fu會以某種方式設法重定向它們或顯示正確的佈局?在我去安裝插件之前,我想確保它能與Authlogic一起工作。你做過這些了嗎? – Rabbott 2010-02-19 21:32:29

3

爲Rails 2.3:你可以下載一個完整的示例應用程序(用一步一步的教程)展示瞭如何使用設計的寶石進行身份驗證,以實現一個管理子域,主域和多個用戶的子域和子域 - 路由gem管理子域。以下是鏈接:subdomain authentication for Rails 2.3

爲Rails 3:這裏有Rails 3 subdomains with authentication一個完整的示例實現(連同一個詳細的教程)。在Rails 3中執行此操作比在Rails 2中執行操作要簡單得多(不需要插件)。