【问题标题】:What is the best way to track an anonymous user with the restful_authentication plugin in Rails?在 Rails 中使用 restful 身份验证插件跟踪匿名用户的最佳方法是什么?
【发布时间】:2011-09-27 22:04:25
【问题描述】:

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

【问题讨论】:

    标签: ruby-on-rails restful-authentication anonymous


    【解决方案1】:

    我最终在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"                  =>  "anonymous@domain.org",
                       "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 
    

    【讨论】:

      【解决方案2】:

      我认为 restful-authentication 在那里没有帮助?我认为您需要为此创建自己的方法。

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

      【讨论】:

        猜你喜欢
        • 2015-03-18
        • 2011-07-02
        • 2015-04-23
        • 2017-03-13
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2021-04-26
        相关资源
        最近更新 更多