7

私はacts_as_tenant gemを使用してマルチテナンシーを管理しており、deviseを使用してユーザーを管理しています。

テナント用のデバイス ユーザー モデルとアカウント モデルのみをセットアップしました。複数のテナントに対してユーザーを作成できます - これはすべて正常に機能しますが、異なるテナント ID に対して同じ電子メールで 2 人のユーザーを作成しようとすると、一意性エラーが発生します。説明したように、validates_uniqueness_to_tenant オプションを使用しています。

ユーザーモデル

class User < ActiveRecord::Base
  # Include default devise modules. Others available are:
  # :token_authenticatable, :confirmable,
  # :lockable, :timeoutable and :omniauthable
  devise :database_authenticatable, :registerable,
         :recoverable, :rememberable, :trackable, :validatable

  attr_accessible :email, :password, :password_confirmation, :remember_me

  acts_as_tenant(:account)
  validates_uniqueness_to_tenant :email
end

アカウント モデル

class Account < ActiveRecord::Base
  attr_accessible :name
end

アプリケーションコントローラー

class ApplicationController < ActionController::Base
  set_current_tenant_by_subdomain(:account, :subdomain)
  protect_from_forgery
end

これはacts_as_tenantのすべてのドキュメントに基づいて動作するはずですが、代わりにデバイスレベルで何かをオーバーライドする必要がありますか?

編集:頭を悩ませて少し休憩した後、問題は、デフォルトでDeviseがメール列に一意のインデックスを追加したためだと思います。これは明らかにacts_as_tenantがやりたいこととは一致しません...インデックスを削除して、Deviseが吐くかどうかを確認します。

EDIT 2: OK、今のところ正式にこれをあきらめました。メイン サイトの手動認証を行いましたが、acts_as_tenant で適切に機能しています。act_as_tenant と Devise の間の非互換性は、あるレイヤーでのみ想定できます。この段階でそれを見つけることはできません。

4

4 に答える 4

9

これを行う唯一の方法は、デバイスからvalidatableモジュールを削除し、次のように独自の検証を実行することです:

class User < ActiveRecord::Base
  acts_as_tenant :account
  attr_accessible :email, :password, :remember_me

  #remove :validatable
  devise :database_authenticatable, :registerable,
    :recoverable, :rememberable, :trackable

  #run own validations
  #I've omitted any emailformatting checks for clarity's sake.
  validates :email, 
    presence: true,
    uniqueness: { scope: :account_id, case_sensitive: false }
  validates :password,
    presence: true,
    length: { :in => 6..20 },
    :if => :password_required?

protected
  # copied from validatable module
  def password_required?
    !persisted? || !password.nil? || !password_confirmation.nil?
  end

end
于 2012-09-18T18:28:26.753 に答える
0

私はそれをテストしていませんが、順序を変更すると、devise が引き継ぐ前に act_as_tenant がそのことを行うのに役立つのではないかと思います。

class User < ActiveRecord::Base

  acts_as_tenant(:account)
  validates_uniqueness_to_tenant :email

  # Include default devise modules. Others available are:
  # :token_authenticatable, :confirmable,
  # :lockable, :timeoutable and :omniauthable
  devise :database_authenticatable, :registerable,
     :recoverable, :rememberable, :trackable, :validatable

  attr_accessible :email, :password, :password_confirmation, :remember_me

end
于 2012-08-01T16:07:47.153 に答える