【问题标题】:How do I create an admin subdomain to manage subdomains in Rails如何创建管理子域来管理 Rails 中的子域
【发布时间】:2010-02-19 03:55:19
【问题描述】:

我正在使用 AuthLogic 和 dhh 在this blog post 中介绍的子域方法,一切正常,并且符合预期。我想弄清楚的是如何创建一个像“admin”或“host”这样的子域,该子域将使用户从 AuthLogic 进行身份验证(这可能是微不足道的,无需提及),它将管理子域。所以基本上,所有子域都会正常运行,除了 admin.site.com 将转到自己的控制器和布局..

dhh 建议只抛出一个异常来重定向,但我不确定它的去向,对我来说似乎没有那么简单,有什么想法吗?

编辑 我认为我在这里使用 AuthLogic 的事实很重要,因为一旦经过身份验证的 AuthLogic 将用户发送到 /account,子域逻辑不会将用户转发到任何地方 - 所以我的问题可能与我如何告诉 AuthLogic 到不同的位置有关,如果user 是 root 用户,登录到 admin 子域..

这是我们目前实现的代码

公司模式

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
  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

【问题讨论】:

    标签: ruby-on-rails subdomain


    【解决方案1】:

    查看subdomain-fu,它允许您根据子域路由到不同的控制器和操作。我已经就这个主题做了Railscasts Episode

    它可能看起来像这样。

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

    这需要在路由列表中足够高,以便在它之前没有其他匹配。

    【讨论】:

    • 我真的很想避免使用插件,我觉得它对于我们的简单需求来说太过分了。但是,我可能只需要走那条路——如果用户点击了那个 url,并且他们没有经过身份验证,subdomain-fu 会以某种方式设法重定向它们或显示正确的布局吗?在我去安装插件之前,我想确保它可以与 Authlogic 一起使用。你这样做了吗?
    【解决方案2】:

    FOR RAILS 2.3:您可以下载完整的示例应用程序(带有分步教程),展示如何使用 Devise 实现管理子域、主域和多个用户子域用于身份验证的 gem 和用于管理子域的 subdomain-routes gem。这是链接:subdomain authentication for Rails 2.3

    FOR RAILS 3:这是Rails 3 subdomains with authentication 的完整示例实现(以及详细教程)。在 Rails 3 中比在 Rails 2 中更容易做到这一点(不需要插件)。

    【讨论】:

      猜你喜欢
      • 2011-04-17
      • 2013-02-26
      • 2017-11-22
      • 1970-01-01
      • 1970-01-01
      • 2010-10-25
      • 1970-01-01
      • 1970-01-01
      • 2017-02-17
      相关资源
      最近更新 更多