Rails和Authlogic:允许每个用户只有一个会话? [英] Rails and Authlogic: Allow only one session per user?

查看:133
本文介绍了Rails和Authlogic:允许每个用户只有一个会话?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有办法来限制在Ruby中的会话on Rails应用程序(我使用Authlogic认证)?

Is there a way to limit the number of sessions in Ruby on Rails application (I'm using Authlogic for authentication)?

我想允许每个用户帐户只有1会话。当同一个用户登录的另一台计算机的previous会议应在过期/失效。

I would like to allow only 1 session per user account. When the same user is logging on another computer the previous session should be expired/invalidated.

我在考虑存储在数据库中的会话数据,然后删除它创建一个新的会话实例,但可能还有一个更简单的方法是什么时候? (配置选项)

I was thinking about storing the session data in database and then deleting it when a new session instance is created but probably there is an easier way? (configuration option)

推荐答案

我只是碰到了一个可能的解决方案,如果您重置presistence令牌就可以达到预期的行为:

I just ran into a possible solution, if you reset presistence token you can achieve the intended behaviour:

class UserSession < Authlogic::Session::Base
  before_create :reset_persistence_token

  def reset_persistence_token
    record.reset_persistence_token
  end
end

这样,旧的会话中是无效的用户登录。

By doing this, old sessions for a user logging in are invalidated.

此前我实现它,你提到的:一个session_key可以字段添加到用户表,并确保当前的session_id存储为用户在登录:

Earlier I implemented it as you mentioned: add a session_key field to the users table and make sure that the current session_id is stored for the user on login:

class UserSession < Authlogic::Session::Base
  after_save :set_session_key
  def set_session_key
    record.session_key = controller.session.session_id
  end
end

然后在通用控制器做这样的事踢了一个用户,当别人以相同的帐户登录:

Then in the generic controller do something like this to kick out a user when someone else logged in with that same account:

before_filter :check_for_simultaneous_login

def check_for_simultaneous_login
  # Prevent simultaneous logins
  if @current_user && @current_user.session_key != session[:session_id]
    flash[:notice] = t('simultaneous_logins_detected')
    current_user_session.destroy
    redirect_to login_url
  end
end

这篇关于Rails和Authlogic:允许每个用户只有一个会话?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆