How do I handle authentication with Devise when using multiple models in Rails 3.2 App

前端 未结 3 1641
萌比男神i
萌比男神i 2021-02-14 05:09

I\'m working on a Rails 3.2 app where I use Devise for authentication. I decided to try single table inheritance for managing user roles, but I quickly ran into a problem. I cur

相关标签:
3条回答
  • 2021-02-14 05:37

    Not sure if this is still needed a solution to this...

    A more elegant way to have the double authentications could be to do the following:

    private
    
    def authenticate!
       :authenticate_admin! || :authenticate_collaborator!
       @current_user = admin_signed_in? ? current_admin : current_collaborator
    end
    

    Then call before_filter :authenticate!

    If you dont need a universal '@current_user' variable just leave out the second line.

    Hope this helps.

    0 讨论(0)
  • 2021-02-14 05:49

    You can solve this using the following solution

    def authenticate!
        if modelA_user_signed_in?
          @current_user = current_modelA
          true
        else
          authenticate_modelB!
        end
    
      end
    
    0 讨论(0)
  • 2021-02-14 05:56

    You may separate all common logic to module and use only same table.

    module UserMethods
      #...
    end
    
    class User < ActiveRecord::Base
      include UserMethods
      devise ...
    
    end  
    
    class Admin < ActiveRecord::Base
      include UserMethods
      self.table_name = "users"
      devise ...
    end
    

    And configure all devise model separately in routes, views(if necessary, see Configuring Views). In this case, you may easy process all different logic.

    0 讨论(0)
提交回复
热议问题