kxh*_*tiz 6 ruby-on-rails devise
我在rails应用程序中使用devise进行身份验证,我的应用程序可以有许多子域.目前,它使用电子邮件作为身份验证,电子邮件对于整个应用程序应该是唯一的.
现在有什么方法可以将电子邮件地址的唯一性范围扩展到子域,而不是整个应用程序?我试过了:
validates_uniqueness_of :email, :scope => :account_id
Run Code Online (Sandbox Code Playgroud)
但没有奏效.它仍然寻求整个应用程序的电子邮件的唯一性,而不是注册新用户时的特定子域.
任何帮助将受到高度赞赏.
好的,我这样做了.
在devise.rb中编辑了config.authentication_keys为
config.authentication_keys = [ :login, :account_id ]
Run Code Online (Sandbox Code Playgroud)
我还创建了隐藏字段,以便在登录表单中包含account_id
<%= f.hidden_field :account_id, :value => @account.id %>
Run Code Online (Sandbox Code Playgroud)
@account保存与当前子域相关的帐户.
并在user.rb中添加了以下protected方法以覆盖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
Run Code Online (Sandbox Code Playgroud)
如果有更好的解决方案,那么随意评论家伙..
干杯!