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

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

问题描述

我有两个用户的机型,首先是从远程数据库遗留和公司内部的目的。 (员工登录)。二是我们的公共登记项目,并登录。但我想要一个登录表单。我搜索了很长一段时间,但一些解决方法混淆了我。

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

但现在我不知道该怎么做用户控制器尝试验证从第一种模式,当用户不存在,请转到第二个模型,并再次尝试。

But 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.

使用:


  • 的Rails 3.1

  • 设计1.4.3

编辑:

在设计的Wiki是一些关于多模型,但我有点混乱,没有例子更为复杂。

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

感谢您。

问候,瑞士雷达

推荐答案

您应该猴补丁 find_for_authentication 从方法设计/模型/ authenticatable.rb

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

关于身份验证的逻辑:
使用两个模型针对你的情况验证它是真的坏主意。您是如何想建立与两个用户模式的关系?这是一个很多不必要的code的。

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.

如果用户凭据是错误的 - 试图向他CrmUser模型验证

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

如果有CrmUser认证是确定的他添加到用户表如果他不存在,存在了。

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

返回用户的模型对象。

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

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