2009-06-23 63 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