Devise - login from two models

I have two user models: first from a remote database, both for outdated and internal company goals. (Entrance for employees). The second is our project for public registration and login, but I need one login form. I searched a lot of time, but some solutions confuse me.

The first legacy looks (read-only 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 secondly, for the 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 do not know how to do this. The user controller tries to authenticate with the first model, and when the user does not exist, go to the second model and try again.

Using:

  • Rails 3.1
  • Develop 1.4.3

EDIT:

There is something about several models in the wiki for Devise, but I'm a bit confused, there is no more complicated example.

Thank.

Regards, Rado

+3
authentication ruby-on-rails login devise
Aug 31 '11 at 9:00 a.m.
source share
1 answer

You should do monkeypatch find_for_authentication 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 is a really bad idea. How do you want to build relationships with two user models? This is a lot of unnecessary code.

The right way to solve your problem is to do some synchronization between your tables.

  • Try authenticating the user with the base user model.

  • If the user credentials were incorrect, try authenticating with the CrmUser model.

  • If authentication with CrmUser was OK , add it to the users table if it does not already exist there.

  • Return a user model object.

+1
Nov 21 2018-11-21T00:
source share



All Articles