设计 - 从两个模型登录 [英] Devise - login from two model

查看:82
本文介绍了设计 - 从两个模型登录的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个用户模型,首先是远程数据库作为遗留和内部公司用途。 (员工登录)。第二是我们的公共注册和登录项目,但我想要一个登录表单。我有很长时间,但有些解决方案让我感到困惑。



第一个传统看起来像(仅用于阅读和身份验证):

  class CrmUser< ActiveRecord :: Base 

需要Rails.root.join('lib','devise','encryptors','sha1')
#包含默认设计模块。其他可用的是:
#:token_authenticatable,:encryptable,:可确认,可锁定,:可超时,可记忆和:omniauthable
建立连接crm_data
set_table_name:users

devise:database_authenticatable,:encryptable,:authentication_keys => [:login]
alias_attribute:encrypted_pa​​ssword,:crypted_pa​​ssword
alias_attribute:password_salt,:salt

#模型的可访问(或保护)属性
attr_accessible:login ,:password,:password_confirmation,:remember_me,:role_id,:first_name,,last_name

公开和注册:

  class User< ActiveRecord :: Base 

#包含默认设计模块。其他可用的是:
#:token_authenticatable,:encryptable,:confirmable,:lockedable,:timeoutable,,rememberable,and:omniauthable
devise:database_authenticatable,:registerable,:authentication_keys => [:login]
alias_attribute:login,:email

#模型的可访问(或保护)属性
attr_accessible:login,:password,:password_confirmation,:remember_me, :role_id,:first_name,:last_name

现在我不知道该怎么做,从第一个模型进行身份验证,当用户不存在时,请转到第二个模型并再次尝试。



使用:




  • Rails 3.1

  • Devise 1.4.3



编辑:



在Devise的维基中有一些关于多个模型的东西,但是我有点困惑,没有比较复杂的例子。



谢谢。



Regard,Rado

解决方案

您应该从 devise / models / authenticatable.rb

$ b中的monkeypatch find_for_authentication
$ b

 模块设计
模块模型
modul e Authenticatable
def find_for_authentication(条件)
#输入您的身份验证逻辑
end
end
end
end

关于认证逻辑:
在你的情况下使用两个模型进行身份验证是一个非常糟糕的主意。你想如何与两个用户模型建立关系?这是很多不必要的代码。



正确解决问题的方法是使您的表之间进行一些同步。




  • 尝试使用基本用户模型验证用户。


  • 如果用户凭据错误 - 尝试使用CrmUser验证他模型。


  • 如果CrmUser的身份验证确实将他添加到用户表(如果他已经不存在)


  • 返回用户的模型对象



I have two user models, first is from remote database as legacy and for internal company purposes. (Employee logins). Second is our project for public registration and sign in but I want one login form. I have searching long time, but some solutions are confusing for me.

First legacy looks like (only for reading and authentication):

class CrmUser < ActiveRecord::Base

  require Rails.root.join('lib', 'devise', 'encryptors', 'sha1')
  # Include default devise modules. Others available are:
  # :token_authenticatable, :encryptable, :confirmable, :lockable, :timeoutable, :rememberable, and :omniauthable
  establish_connection "crm_data"
  set_table_name :users

  devise :database_authenticatable, :encryptable, :authentication_keys => [:login]
  alias_attribute :encrypted_password, :crypted_password
  alias_attribute :password_salt, :salt

  # Setup accessible (or protected) attributes for your model
  attr_accessible :login, :password, :password_confirmation, :remember_me, :role_id, :first_name, :last_name

And second, for public and registration:

class User < ActiveRecord::Base

  # Include default devise modules. Others available are:
  # :token_authenticatable, :encryptable, :confirmable, :lockable, :timeoutable, :rememberable, and :omniauthable
  devise :database_authenticatable, :registerable, :authentication_keys => [:login]
  alias_attribute :login, :email

  # Setup accessible (or protected) attributes for your model
  attr_accessible :login, :password, :password_confirmation, :remember_me, :role_id, :first_name, :last_name

Now I don't know how to do that User controller try to authentication from first model, and when user doesn't exists, go to second model and try it again.

Using:

  • Rails 3.1
  • Devise 1.4.3

EDIT:

In wiki of Devise is something about multiple model, but I'm little bit confused, there are not example more complex.

Thank you.

Regards, Rado

解决方案

You should monkeypatch find_for_authentication method from devise/models/authenticatable.rb

module Devise
  module Models
    module Authenticatable
      def find_for_authentication(conditions)
          #put your authentication logic here
        end
    end
  end
end

About authentication logic: Using two models for authentication in your case it's realy bad idea. How do u want build relations with two users models? It's a lot of unnecessary code.

Correct way of resolving your problem is make some synchronization between yours tables.

  • Try to authenticate user with base User model.

  • If user credentials was wrong - try to authenticate him with CrmUser model.

  • If authentication with CrmUser was OK add him to users table if he doesn't exists there already.

  • Return User's model object.

这篇关于设计 - 从两个模型登录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆