2016-01-21 65 views
4

在我的應用程序中,我需要不時註銷特定用戶。我需要通過界面或sidekiq工作人員來完成。所以我想在我的user模型中創建一個sign_out方法。從模型中註銷設計用戶

我在文檔中看到設計提供了一個sign_out方法,但只在控制器中。有沒有辦法從模型或類似的東西訪問這個方法。

感謝

+0

那麼添加「force_logged_out」字段給用戶,然後註銷控制器下一個會話? –

回答

3

你需要閱讀這個答案https://stackoverflow.com/a/24388643/4269732第一。

如果我要實現這種行爲,那麼我會在用戶模型中添加一列,如expire_at_next_request? 然後只需在before_filter中檢查此值並在用戶註銷時註銷。

class ApplicationController < ActionController::Base 
    before_filter :logout_if_requested 

    def logout_if_requested 
    if current_user && current_user.expire_at_next_request? 
     current_user.update_attributes(:expire_at_next_request=>false) 
     sign_out current_user 
     redirect_to :new_session_path 
    end 
    end