[英]Custom authentication with foreign model in Rails 4 with Devise
我有一个典型的User模型(带有用户名,电子邮件和密码字段),具有has_one客户端模型,该模型具有许多 Phone模型。
我想做的是允许用户使用电子邮件+密码,用户名+密码或电话号码+密码之一的组合登录。
如何使用Devise实现此目的?
为了实现这一点,我必须在用户模型中添加以下内容:
attr_accessor :login
private
def self.find_first_by_auth_conditions(warden_conditions)
conditions = warden_conditions.dup
if login = conditions.delete(:login)
match = where(conditions).where(
["lower(email) = :login OR lower(username) = :login",
{ login: login.downcase }]).first
unless match
match = joins(client: :phones).where(conditions).where(
["lower(client_phones.number) = :phone",
{ phone: PhoneFormatter.parse(login) }]).first
end
find(match.id) if match
else
where(conditions).first
end
end
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.