2012-12-06 1 views
1

저는 다중 스키마가있는 PostgreSQL을 사용하는 애플리케이션을 개발하여 애플리케이션의 멀티 테넌트 측을 구현했습니다. 세입자는 하위 도메인을 통해 확인되지만, 여전히 주 도메인을 등록 포인트로 사용하고 관리 백엔드를 사용할 수있는 방법을 찾고 있습니다.하위 도메인 및 하위 도메인을 사용하지 않는 멀티 테넌트 루비 애플리케이션

다음은 입주자가 확인중인 내 애플리케이션 컨트롤러입니다.

는 여기 http://railscasts.com/episodes/389-multitenancy-with-postgresql?view=comments

class ApplicationController < ActionController::Base 


protect_from_forgery 

    rescue_from CanCan::AccessDenied do |exception| 
    redirect_to root_path, :alert => exception.message 
    end 

    before_filter :mailer_set_url_options 

    def mailer_set_url_options 
    ActionMailer::Base.default_url_options[:host] = request.host_with_port 
    end 

    around_filter :scope_current_tenant 

private 

    def current_user 
    @current_user ||= User.find(session[:user_id]) if session[:user_id] 
    end 
helper_method :current_user 

    def current_tenant 
    @current_tenant ||= Tenant.find_by_subdomain!(request.subdomain) 
    end 
    helper_method :current_tenant 

    def scope_current_tenant(&block) 
    current_tenant.scope_schema("public", &block) 
    end 
end 

내가 납니까 다른 문제가 유증 통해에서 railscast을 따랐다. 그것은 multitenant 때문에 나타나고 도우미 current_user를 정의하는 것은 응용 프로그램 컨트롤러에서 세션과 함께 까다롭게 놀고 있습니다, 어떤 아이디어입니까?

+1

해결 방법을 찾으셨습니까? @richard – scanales

+0

@scanales 아직 프로젝트를 찾지 못했습니다. 프로젝트가 보류 상태가되어 다음 프로젝트로 넘어갔습니다. 다시 돌아 가면 솔루션을 추가 할 것입니다. –

답변

0

해결책을 찾았지만 게시하는 것을 잊어 버렸지 만 귀하의 의견은 나를 기억하게했습니다. 기본적으로

내가 current_id 뭔가 잘못을 하위 도메인이 "admin" 같을 때,이 경우 "admin"에, 및 하위 도메인이 wwwcurrent_id를 설정하지 않거나 : 이것은 내가 현재의 클라이언트를 범위 지정하면 뭘하는지입니다 내가 메인 페이지에 사용하는 것이다 (빈.

희망 당신이 예를 위해 (주 도메인에 대한 세입자가없는 경우이 방문자.

def scope_current_client 
    case request.subdomain 
    when /^admin$/ 
     Client.current_id = "admin" 
    when /^www$|^$/ 

    else 
     Client.current_id = current_client.id 
    end 
    yield 
    ensure 
    Client.current_id = nil   
end 
1

도움)와 current_tenant 따라서에서 전무로 설정 이 경우 current_te를 호출 할 수 없습니다. nant.scope_schema,하지만 난 당신이 같은 것을 할 수 있다고 생각 :이 예제 세입자에

def current_tenant 
    # Select tenant based on a user and set it to nil if no user: 
    @current_tenant ||= current_user.tenant unless current_user.nil? 
end 
helper_method :current_tenant 

def scope_current_tenant(&block) 
    if current_tenant.nil? 
     scope_visitor_schema 
     yield 
    else 
    current_tenant.scope_schema("public", &block) 
    end 
end 

def scope_visitor_schema() 
    original_search_path = ActiveRecord::Base.connection.schema_search_path 
    ActiveRecord::Base.connection.schema_search_path = 'public' 
ensure 
    ActiveRecord::Base.connection.schema_search_path = original_search_path 
end 

은 사용자가 아닌 하위 도메인에 따라 선택하지만, 방법은 동일합니다.

관련 문제