将电子邮件范围界定为designrails中的子域



我在rails应用程序中使用design进行身份验证,我的应用程序可以有许多子域。目前它使用电子邮件作为身份验证,并且电子邮件对于整个应用程序应该是唯一的。

现在有没有任何方法可以将电子邮件地址的唯一性范围到子域,而不是整个应用程序?我试过了:

validates_uniqueness_of :email, :scope => :account_id

但没用。在注册新用户时,它仍然寻求电子邮件对整个应用程序的唯一性,而不是特定的子域。

如有任何帮助,我们将不胜感激。

好的,我是这样做的
将devise.rb中的config.authentication_keys编辑为

config.authentication_keys = [ :login, :account_id ]

我还创建了隐藏字段,在登录表单中包含account_id

<%= f.hidden_field :account_id, :value => @account.id %>

这里@account持有与当前子域相关的帐户。

并在user.rb中添加了以下受保护的方法来覆盖find_for_database_authentication类方法

protected
def self.find_for_database_authentication(warden_conditions)
conditions = warden_conditions.dup
login = conditions.delete(:login)
account_id = conditions.delete(:account_id)
where(conditions).where(["lower(username) = :value OR lower(email) = :value", { :value => login.downcase }]).where("account_id = ?", account_id).first
end 

如果有更好的解决方案,请随时发表评论
干杯

您是如何保存用户的,在尝试保存之前是否在用户模型上设置了帐户?如果没有,那么您的作用域将是account_id为nil,因此看起来没有作用域。

相关内容

最新更新