0

我有一个多租户应用程序,我正在像这样设置当前租户:

class ApplicationController < ActionController::Base
  around_filter :scope_current_tenancy

  def scope_current_tenancy
    Tenancy.current_id = current_tenancy.id if request.subdomain != 'www'
    yield
  ensure
    Tenancy.current_id = nil
  end
end

然后在我的用户模型中,我default_scope定义为只访问我的租户内的用户:

class Postulant < ActiveRecord::Base

  default_scope ->{ where("enlistments.tenancy_id = ?", Tenancy.current_id).includes(:enlistments).references(:enlistments) }

到目前为止这有效,但现在使用devise_invitable并尝试接受邀请我收到了一条Filter chain halted as :resource_from_invitation_token rendered or redirected消息。问题是因为我的scope_current_tenancy过滤器是在 之后执行的resource_from_invitation_token,所以resource没有正确加载。

class Devise::InvitationsController < DeviseController

  prepend_before_filter :resource_from_invitation_token, :only => [:edit, :destroy]

  def resource_from_invitation_token
    # Here 'resource_class' is my Postulant model, so when I call
    # 'find_by_invitation_token' applies the defined default_scope
    # which doesn't word without 'scope_current_tenancy'
    unless params[:invitation_token] && self.resource = resource_class.find_by_invitation_token(params[:invitation_token], true)
      set_flash_message(:alert, :invitation_token_invalid)
      redirect_to after_sign_out_path_for(resource_name)
    end
  end

end

所以我的问题是,有没有办法比:scope_current_tenancy之前运行:resource_from_invitation_token
我试图改变around_filter :scope_current_tenancyprepend_around_filter :scope_current_tenancy但我没有运气。有什么想法吗?

4

1 回答 1

1

因为prepend_before_filter :resource_from_invitation_token在你的 ApplicationController 之后,这个过滤器将被添加到过滤器链的前面,即使你使用 prepend_before_filter 作为 scope_current_tenancy。一种选择可能是尝试类似的方法:

skip_around_filter :scope_current_tenancy
prepend_before_filter :resource_from_invitation_token, :only => [:edit, :destroy]
prepend_around_filter :scope_current_tenancy

在你的 Devise::InvitationsController

不确定这是否可行,但似乎值得一试。

或者,您可以只删除“skip_around_filter”行,假设 scope_current_tenancy 是幂等的,这似乎是这种情况。

于 2013-11-20T04:09:00.690 回答