我试图禁用单用户使用设计身份验证的多登录,并提出了此解决方案。禁用带导轨的单用户多登录导轨
我用active record
session store
,这样我就可以通过它们的ID访问所有的用户会话。改变后的初始化到session_store.rb
MyApp::Application.config.session_store :active_record_store
附加字段SESSION_ID到用户表和会话创建模型(session.rb)
导轨迁移:
rails g migration add_session_id_to_users session_id:integer
session.rb
:
class Session < ActiveRecord::Base
end
因为,使用监狱长设计宝石我设置回调after_authentication
来检查用户会话。
user.rb
:
class << self
def delete_session(session_id)
begin
session = Session.where("session_id=?",session_id)
session.delete_all
rescue => e
end
end
end
Warden::Manager.after_authentication do |user, auth, opts|
unless user.session_id == ""
auth.logout
User.delete_session(user.session_id)
user.session_id = ""
user.save
throw(:warden, :message => "User already logged in, try again wil singout from other machine")
end
end
当用户已经登录此回调将提醒
我保存的用户会话ID在application_controller
before_filter
(aplication_helper.rb
)
before_action :save_session
def save_session
if user_signed_in? && current_user.session_id == ""
current_user.session_id = request.session_options[:id]
current_user.save
end
end
我正在会议ID由request.session_options[:id]
。
此设置正常工作没有任何问题。你们可以请你分享一下你的建议。这个解决方案会发生任何潜在的问题吗?