2017-05-11 35 views

回答

7

設計有timeoutable模塊,您可以使用。

在你User模型,你將包括:timeoutabledevise模型,並在devise.rb初始化將配置它媲美:

# ==> Configuration for :timeoutable 
    # The time you want to timeout the user session without activity. After this 
    # time the user will be asked for credentials again. Default is 30 minutes. 
    config.timeout_in = ENV['YOUR_TIME'].to_i.minutes 

如果你想與您的用戶類型,您可以添加一個更靈活方法是這樣的你User型號:

def timeout_in 
    if self.type == 'Admin' 
    45.minutes 
    else 
    60.minutes 
    end 
end 

將被用來代替你設置timeout_in r初始化程序。

+2

或'ENV.fetch('YOUR_TIME',5).to_i.minutes'如果你想要一個默認值而不是一個神祕的NoMethodErrror – max

+0

好的方法,但我所有的角色都只需要一個'User'模型,用不同的'type_id';儘管我已經想出了另一個解決方案['gem'activerecord-session_store''](https://github.com/rails/activerecord-session_store)來超時用戶會話,但接受你的答案是有什麼辦法的只讓不同的用戶類型使你的解決方案動態化? – dariush

+0

@dariush我更新了答案,希望能夠充分回答你的問題 – jdgray

相關問題