使用 Rails 中的 Restful_authentication 插件跟踪匿名用户的最佳方法是什么?

发布于 2024-11-18 16:52:05 字数 122 浏览 2 评论 0原文

我看过关于如何在投票应用程序中跟踪匿名用户(例如使用 cookie)的一般帖子。但具体来说,我如何修改 restful_authentication 来使用 IP + 用户代理(哈希)跟踪匿名用户。谢谢。

I have seen general posts on how to track anonymous users in a voting app such as with a cookie. But specifically how do I modify restful_authentication to track anonymous users with IP + user-agent (hash). Thanks.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

剩余の解释 2024-11-25 16:52:05

我不认为静态身份验证会有帮助吗?我认为您需要为此创建自己的方法。

为什么还要将 IP 和 User-Agent 耦合起来?仅 IP 地址真的足够吗?

I don't think restful-authentication would help there? I think you'll need to create your own method for that.

Why do you want to couple the IP and User-Agent also? Does IP address alone really suffice?

我要还你自由 2024-11-25 16:52:05

我最终在 lib/authenticated_system.rb 中创建了一个方法 login_from_anonymous ,如下所示。该方法称为 current_user 方法,如下所示。

def login_from_anonymous
  user = User.new({"new_profile_attributes"=>                                 
                      { "country_code"=>"", "zip"=>"", "first_name"=>"Anonymous",
                        "last_name"=>"User", "affiliation_id"=>"1"
                      }, 
                   "password"               =>  "anonymous123", 
                   "password_confirmation"  =>  "anonymous123", 
                   #"invitation_token"       =>  "", 
                   "invitation_limit"       =>  0,                        
                   "login"                  =>  "anonymous_#{Time.now.strftime("%m-%d-%y+%I:%M:%S%p")}", 
                   "email"                  =>  "[email protected]",
                   "current_ip"             =>  request.env['REMOTE_ADDR']})    
  user.send(:create_without_callbacks)
  self.current_user = user
  handle_remember_cookie! true # freshen cookie token (keeping date)
  self.current_user
end  

def current_user     
    @current_user ||= (login_from_session || login_from_basic_auth || login_from_cookie || login_from_anonymous) unless @current_user == false
end 

I ended up creating a method login_from_anonymous in lib/authenticated_system.rb as shown below. That method is called current_user method also shown below.

def login_from_anonymous
  user = User.new({"new_profile_attributes"=>                                 
                      { "country_code"=>"", "zip"=>"", "first_name"=>"Anonymous",
                        "last_name"=>"User", "affiliation_id"=>"1"
                      }, 
                   "password"               =>  "anonymous123", 
                   "password_confirmation"  =>  "anonymous123", 
                   #"invitation_token"       =>  "", 
                   "invitation_limit"       =>  0,                        
                   "login"                  =>  "anonymous_#{Time.now.strftime("%m-%d-%y+%I:%M:%S%p")}", 
                   "email"                  =>  "[email protected]",
                   "current_ip"             =>  request.env['REMOTE_ADDR']})    
  user.send(:create_without_callbacks)
  self.current_user = user
  handle_remember_cookie! true # freshen cookie token (keeping date)
  self.current_user
end  

def current_user     
    @current_user ||= (login_from_session || login_from_basic_auth || login_from_cookie || login_from_anonymous) unless @current_user == false
end 
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文