Rails - Authlogic 和 Facebook 应用程序 - Coherent Current_User

发布于 2024-08-23 20:23:53 字数 2454 浏览 9 评论 0原文

我目前正在 Rails 中构建一个 Web 应用程序。我正在使用 Authlogic 来处理正常的身份验证。我正在使用 facebooker 来处理外部站点上的 facebook 连接,并在 facebook 画布应用程序中对用户进行身份验证。

我在构建简单、连贯的 current_user 功能时遇到困难。目前,我有一堆不同的方法来获取/设置当前用户,我想尽可能地简化它。

我正在为我的会话使用 active_record_store (在environment.rb中定义) 这是我的应用程序控制器:

class ApplicationController < ActionController::Base

  helper :all # include all helpers, all the time
  protect_from_forgery # See ActionController::RequestForgeryProtection for details
  filter_parameter_logging :fb_sig_friends, :password, :password_confirmation
  helper_method :facebook_session, :current_user_session, :current_user

  before_filter :fb_setup 

  attr_accessor :current_user 
  helper_attr :current_user


  #Before filter to decide if we should use facebook sessions to set current user              

  def fb_setup 
   if request_comes_from_facebook? 
      ensure_authenticated_to_facebook 
      set_current_fb_user
   end 
  end 

  ############
  # AuthLogic
  ############
 def current_user_session
      return @current_user_session if defined?(@current_user_session)
      @current_user_session = UserSession.find
 end

def current_user
      return @current_user if defined?(@current_user)
      @current_user = current_user_session && current_user_session.user
end

##########
# Facebook
##########
def create_user 
 self.current_user = User.for(facebook_session.user.to_i, facebook_session)
end

def set_current_fb_user 
 self.current_user = User.for(facebook_session.user.to_i, facebook_session)
end

end

在我的用户模型中,我有这些方法来帮助从 facebook id 查找/创建用户: def self.for(facebook_id,facebook_session=nil) 返回 find_or_create_by_facebook_id(facebook_id) 做 |user| 除非 facebook_session.nil? user.store_session(facebook_session.session_key) 结尾 结尾 令人

def store_session(session_key) 
  if self.session_key != session_key
    update_attribute(:session_key,session_key)
  end
end

def facebook_session
  @facebook_session ||=  
   returning Facebooker::Session.create do |session| 
    session.secure_with!(session_key,facebook_id,1.day.from_now) 
  end
 end

困惑的是,authlogic 的方法仅在需要 current_user 时才会被调用,但 facebook 的 set_current_fb_user 在每个请求之前都会被调用。

我需要以一致的方式在 application_controller 中设置 self.current_user 或 @current_user,无论哪个会话负责设置 current_user。我在执行此操作时遇到了困难,并且我的代码库的其余部分也因此受到影响。如果有人有任何建议,我们将不胜感激。

如果您需要任何其他信息,我将很乐意提供。

谢谢

I'm currently building a web application in Rails. I'm using Authlogic to handle normal authentication. I'm using facebooker to handle facebook connect on the external site and to authenticate users within the facebook canvas application.

I'm having trouble building simple, coherent current_user functionality. Currently I have a whole mess of different methods getting/setting the current user, and I'd like to simplify this as much as possible.

I'm using active_record_store for my sessions (defined in environment.rb)
Here is my application Controller:

class ApplicationController < ActionController::Base

  helper :all # include all helpers, all the time
  protect_from_forgery # See ActionController::RequestForgeryProtection for details
  filter_parameter_logging :fb_sig_friends, :password, :password_confirmation
  helper_method :facebook_session, :current_user_session, :current_user

  before_filter :fb_setup 

  attr_accessor :current_user 
  helper_attr :current_user


  #Before filter to decide if we should use facebook sessions to set current user              

  def fb_setup 
   if request_comes_from_facebook? 
      ensure_authenticated_to_facebook 
      set_current_fb_user
   end 
  end 

  ############
  # AuthLogic
  ############
 def current_user_session
      return @current_user_session if defined?(@current_user_session)
      @current_user_session = UserSession.find
 end

def current_user
      return @current_user if defined?(@current_user)
      @current_user = current_user_session && current_user_session.user
end

##########
# Facebook
##########
def create_user 
 self.current_user = User.for(facebook_session.user.to_i, facebook_session)
end

def set_current_fb_user 
 self.current_user = User.for(facebook_session.user.to_i, facebook_session)
end

end

And in my User model, I have these methods to help finding/creating a user from a facebook id:
def self.for(facebook_id,facebook_session=nil)
returning find_or_create_by_facebook_id(facebook_id) do |user|
unless facebook_session.nil?
user.store_session(facebook_session.session_key)
end
end
end

def store_session(session_key) 
  if self.session_key != session_key
    update_attribute(:session_key,session_key)
  end
end

def facebook_session
  @facebook_session ||=  
   returning Facebooker::Session.create do |session| 
    session.secure_with!(session_key,facebook_id,1.day.from_now) 
  end
 end

What confuses this, is that authlogic's methods only get called when the current_user is required, but facebook's set_current_fb_user is called before every request.

I need either self.current_user or @current_user set in application_controller in a coherent way regardless of which session is responsible for setting the current_user. I'm having trouble doing this and the rest of my code base is suffering because of this. If anyone has any advice, it would be much appreciated.

If you need any additional info, I'll be happy to provide it.

Thanks

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

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

发布评论

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

评论(1

路还长,别太狂 2024-08-30 20:23:53

set_current_fb_user 方法在每次请求时都会被调用,因为它是从 fb_setup 调用的,而 fb_setupApplicationControllerbefore_filter (并且所有控制器都继承自 ApplicationController)。但是,您没有任何始终调用 current_userbefore_filter 方法。因此,尝试将 before_filter 行更改为:

before_filter [:set_current_fb_user, :current_user]

请注意,如果 set_current_fb_user 返回 false 值,则不会调用 current_user

有关详细信息,请参阅 Rails 操作控制器指南

The set_current_fb_user method is called upon every request because it is called from fb_setup, which is a before_filter of ApplicationController (and all controllers inherit from ApplicationController). However, you don't have any before_filter method that always calls current_user. So, try changing the before_filter line to:

before_filter [:set_current_fb_user, :current_user]

Note that if set_current_fb_user returns a false value, then current_user won't be called.

See the Rails Action Controller Guide for more information.

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