2009-06-23 57 views
1

Authlogic(還沒有發現任何瀏覽the docs)到 有一個簡單的方法確保如果用戶已經有一個UserSession對象不能創建UserSession ?Authlogic:確保用戶不能登錄兩次

換句話說:我想確保用戶無法使用相同憑證登錄兩次。

更新:檢查小偷的答案的意見,找到解決這個問題的解決方案。

回答

3

在用戶會話控制器:

before_filter :require_no_user, :only => [:new, :create] 

在您的應用程序控制器:

def require_no_user 
    if current_user 
    store_location 
    flash[:notice] = "You must be logged out to access this page" 
    redirect_to account_url 
    return false 
    end 
end 
+1

此外,當某人登錄時,最好還是隱藏登錄鏈接。 – 2009-06-23 19:42:35

+0

@thief:您能詳細說明一下嗎?我不明白這將如何保持X在客戶端B上以Alice身份登錄,而Y已經在客戶端A上以Alice身份登錄。 – Javier 2009-06-23 22:55:11

0

在我看來,一個更簡潔的方法是在UserSession類使用回調。就像,您可以在那裏定義一個before_create回調,並在適當的情況下將模型標記爲無效。不過,我自己並沒有嘗試過。這裏是the docs for the Callbacks module

相關問題