2010-12-17 93 views
3

我需要一些應該非常基本的幫助。我遵循了讓Rails 3和Authlogic一起工作的優秀教程。它是許多地方引用的非常基本的身份驗證shell。但我只是不能使註銷功能工作。我花了幾天時間嘗試各種各樣的事情。所以我需要幫助。也許它只是我不正確的理解,但這裏是問題:Rails/Authlogic - UserSession.destroy不會終止會話

底線:@ user_session.destroy不會終止會話。

詳細信息: 我正在研究一個web服務,所以我只與.xml格式的請求進行交互......而我正在使用cURL來測試。

一切工作正常,除了我的主要問題。我用Users來播種數據庫。

當我這樣做沒有在首測和cookie.txt的是空白的(或者是其他一些用戶的Cookie) - >

curl -v -H "Accept: text/xml" -X GET --cookie cookies.txt http://localhost:3000/user.xml 

我的請求被拒絕了,因爲預期(無會話)

當我使用此登錄 - >

curl -v -H "Accept: text/xml" -X POST -d 'user_session[email][email protected]&user_session[password]=secret' --cookie-jar cookies.txt http://localhost:3000/user_session.xml 

創建一個新的會話,然後我可以做以前的GET我心中的內容。一切都可以正常運行在使用其他用戶的cookie不工作,不使用cookie時我註銷不工作等

但是......這樣做 - >

curl -v -H "Accept: text/xml" -X DELETE --cookie cookies.txt http://localhost:3000/user_session.xml 

的UserSessionsController#破壞動作稱爲

def destroy 
    @user_session = UserSession.find 
    @user_session.destroy 

respond_to do |format| 
    format.html { redirect_to(:users, :notice => 'Goodbye!') } 
    format.xml { head :ok } 
end 
end 

和動作中的線 - @user_session = UserSession.find和 @ user_session.destroy被執行。沒有錯誤和測試@user_session = UserSession.find在銷燬後立即產生零如預期

然後...在下一個GET請求,如果這個相同的cookie,現在應該過期,在請求中傳遞相同的數據庫查找該特定用戶的ID發生並且信息被傳遞 - 就像一個會話繼續存在一樣!

我知道這是基本的東西,但爲了完整性這裏是我的ApplicationController:

class ApplicationController < ActionController::Base 
#protect_from_forgery 
helper_method :current_user_session, :current_user 

private 

def current_user_session 
    return @current_user_session if defined?(@current_user_session) 
    @current_user_session = UserSession.find 
end 

def current_user 
return @current_user if defined?(@current_user) 
@current_user = current_user_session && current_user_session.record 
end 

def require_user 
unless current_user 
    respond_to do |format| 
    format.xml { head :unauthorized } 
    end 
    return false 
end 
end 

def require_no_user 
if current_user 
    respond_to do |format| 
    format.xml { head :bad_request } 
    end 
return false 
end 
end 

@user_session = UserSession.find在current_user_session - 產生被註銷用戶,並會繼續這樣做......如果永遠在提供的登錄中創建的原始cookie。

無論我嘗試什麼,我都無法退休。我甚至鑽研關閉ActiveRecord緩存,因爲我看到緩存命中發生。緩存關閉,沒有任何改變。我認爲這或者是我的認識上的一個嚴重錯誤,或者是與Authlogic有關,我不明白或不起作用。

任何想法??

回答

1

如果你還沒有找到解決方法 - 會話記錄永遠不會被清理。一種方法是使用cron作業定期清理會話。

這是我發現的資源有所幫助:Sessions and cookies in Ruby on Rails

另外還有一點,就可以安裝用戶會話(包括自動清理功能)的更智能的管理插件:基於Zabba的優秀Limited Sessions

+0

謝謝!不,我沒有找到一個好的解釋或解決辦法。我非常感謝幫助。對不起,我的回覆很拖延,但我在假期很遠。我很驚訝我沒有在搜索中遇到Rails的這個「特性」。 – 2011-01-03 15:27:00

3

回答我提出了在註銷後清理會話記錄的解決方案,以及在創建後的預定時間之後(分開 - 我都希望)。 Zabba的回答很好,我只是想在Authlogic中整合一個答案。

該解決方案非常簡單和容易。直到我找到以下兩個資源時,我才知道如何去做。我想我之前沒有找到這些,因爲我不知道我在找什麼。

爲了清除會議註銷 - 我用這個答案,它運作良好。

Authlogic and multiple sessions for the same user

超時我這裏使用的第二個答案 -

Ruby on rails - Authlogic : periodically check if user session is valid

所有的一切都似乎是偉大的工作。