【问题标题】:Authlogic accessing user id in session objectAuthlogic 访问会话对象中的用户 ID
【发布时间】:2009-08-22 12:06:00
【问题描述】:

我想知道是否有人知道如何使用 Authlogic gem 从 Rails 中的会话对象访问用户 ID?

我有一个清扫器,它运行以使特定于用户的缓存片段过期:

 def expire_cache_for(record)
    ActiveRecord::Base.logger.debug "team: #{record.team_id}"
    ActiveRecord::Base.logger.debug("User: #{session}")

    # Expire a fragment
    expire_fragment(:controller => 'groups',  :action => "team_#{record.team_id}_user_#{current_user.id}")
  end

如何使用 Authlogic 从我的清扫器访问用户 ID 以使片段过期?

【问题讨论】:

    标签: ruby-on-rails authlogic


    【解决方案1】:

    您应该能够通过使用您的 UserSession 模型(或您添加的任何从 Authlogic::Session::Base 派生的模型)来做到这一点:

    current_user = UserSession.find
    id = current_user && current_user.record.id
    

    【讨论】:

    • UserSession.find 不返回 UserSession 对象而不是 User 对象吗?当前用户是 UserSession.find.user
    • 约翰·皮格纳塔是正确的。会话将acts_as_authentic 模型存储在变量记录中,无论acts_as_authentic 模型的名称如何,该变量记录都是相同的。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-09-03
    • 2011-03-27
    • 1970-01-01
    • 1970-01-01
    • 2015-06-21
    • 1970-01-01
    • 2011-01-27
    相关资源
    最近更新 更多