2010-02-04 8 views
5

मैं एक समाधान की तलाश कर रहा हूं, यदि उपयोगकर्ता सत्र समाप्त हो गया है और यदि उसे लॉगिन पृष्ठ पर रीडायरेक्ट किया गया है तो मुझे समय-समय पर जांच करने की अनुमति मिल रही है।
मैं औथ्लोगिक मणि का उपयोग कर रहा हूं, इसलिए मैं जो कर रहा हूं वह एक फ़ंक्शन को कॉल करता है जो current_user पर परीक्षण करता है।
मेरा USER_SESSION_TIMEOUT 5 मिनट है इसलिए मैं हर 5:10 मिनट में यह AJAX कॉल करता हूं।रेल पर रूबी - ऑथलॉगिक: समय-समय पर जांचें कि क्या उपयोगकर्ता सत्र मान्य है

<%= periodically_call_remote :url => {:controller => 'user_session', :action => 'check_session_timed_out'}, :frequency => (USER_SESSION_TIMEOUT + 10.seconds) %> 

def check_session_timed_out 
    if !current_user 
     flash[:login_notice] = "Your session timed out due to a period of inactivity. Please sign in again." 
     render :update do |page| 
      page.redirect_to "/user_sessions/new" 
     end 
    else 
     render :nothing => true 
    end 
end 

मैंने देखा है कि हर बार जब मैं उपयोगकर्ता वस्तु current_user फोन अद्यतन किया जाता है और इतने सत्र 5 मिनट के लिए नए सिरे से किया गया है।
कोई समस्या नहीं है जब केवल एक टैब खोला जाता है लेकिन यदि मेरे पास प्रत्येक बार 2 टैब होते हैं तो मैं check_session_timed_out current_user नवीनीकरण उपयोगकर्ता को अद्यतन करता हूं और इसलिए सत्र कभी समाप्त नहीं होता है।

कोई विचार? धन्यवाद

उत्तर

5

AuthLogic source itself से:

# For example, what if you had a javascript function that polled the server 
# updating how much time is left in their session before it times out. Obviously 
# you would want to ignore this request, because then the user would never 
# time out. So you can do something like this in your controller: 

def last_request_update_allowed? 
action_name != "update_session_time_left" 
end 

आपके मामले में, आप अपने कार्य के नाम का उपयोग कर अपने नियंत्रक को विधि जोड़ना चाहते हैं:

def last_request_update_allowed? 
    action_name != "check_session_timed_out" 
end 
+0

मेर्सी! यह एकदम सही है – Mathieu

6

Authlogic क्या कर सकते हैं यह तुम्हारे लिए। बस अपने मॉडल में उपयोग करें:

उपयोगकर्ता मॉडल पर:

acts_as_authentic do |c| 
    c.logged_in_timeout(5.minutes) 
end 

... और UserSession मॉडल पर:

self.logout_on_timeout = true 

और बस काम करते हैं! = डी

संबंधित मुद्दे