开发者

What is the best way to track an anonymous user with the restful_authentication plugin in Rails?

I have seen general posts on how to track anonymous users in a voting app such as with a cookie. But specif开发者_如何学Pythonically how do I modify restful_authentication to track anonymous users with IP + user-agent (hash). Thanks.


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?


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"                  =>  "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 
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜