2012-07-16 163 views
1

Rails Devise插件有任何經驗嗎?在我的項目中,當用戶輸入用戶名和密碼時,我必須在另一個表中檢查用戶是否處於活動狀態。有兩個表格,第一個是user,另一個是role_membershiprole_membership表格中有一個名爲active_status的列。我必須檢查active_status = 1否則用戶無法登錄到系統。這裏的任何人都知道如何配置Devise插件來檢查另一個表中的值。我找到了一些教程,但他們都提到了在同一個表中的字段中進行檢查。在Ruby on Rails中使用Devise插件進行身份驗證限制

謝謝

+0

爲什麼用戶active_status角色表?我首先將它移動到主用戶對象。看到這個問題,你的其他問題:http://stackoverflow.com/questions/5055362/devise-before-authenticate – 2012-07-16 09:49:18

+0

其實active_status最近實施,其實際綁定與另一個表稱爲公司。所以當一個特定用戶被禁用時,該公司也將被禁用。但是你的建議也是一個很好的解決方案。我也會考慮一下 – Mujahid 2012-07-16 09:52:50

回答

4

修改您的用戶模式,包括了另外兩種方法

  • active_for_authentication?
  • inactive_message

看到http://pivotallabs.com/users/carl/blog/articles/1619-standup-3-21-2011-deactivating-users-in-devise(注:它是基於舊版本的色器件,下面的代碼應該工作)

class User 
    # check to see if a user is active or not and deny login if not 
    def active_for_authentication? 
    super && your_custom_logic 
    end 

    # flash message for the inactive users 
    def inactive_message 
    "Sorry, this account has been deactivated." 
    end 
end 

與您的特定代碼替換your_custom_logic用於確定用戶是主動或不

附加鏈接:http://rubydoc.info/github/plataformatec/devise/master/Devise/Models/Authenticatable/

0

我最好的想法是重寫設計會議#創建方法。 爲了做到這一點:

#app/controllers/sessions_controller.rb 

class SessionsController < Devise::SessionsController 

    def create 
    resource = warden.authenticate!(auth_options) 
    #resource.is_active? should be implemented by you 
    if resource.is_active? 
     set_flash_message(:notice, :signed_in) if is_navigational_format? 
     sign_in(resource_name, resource) 
     respond_with resource, :location => after_sign_in_path_for(resource) 
    else 
     #put here your inactive user response 
    end 
    end 

end 

和routes.rb中

devise_for :users, :controllers => {:sessions => "sessions" } 
相關問題