加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

ruby-on-rails – 如何在ConnectionAdapter回调中使用Devise方法

发布时间:2020-12-17 02:10:28 所属栏目:百科 来源:网络整理
导读:我有一个rails 5.1应用程序,它使用Devise来处理我的用户模型的身份验证.这个应用程序有一个Oracle数据库后端,需要在执行任何查询之前使用登录用户设置系统上下文变量,所以我希望在ConnectionAdapter的:checkout回调中执行此操作. class ApplicationControll
我有一个rails 5.1应用程序,它使用Devise来处理我的用户模型的身份验证.这个应用程序有一个Oracle数据库后端,需要在执行任何查询之前使用登录用户设置系统上下文变量,所以我希望在ConnectionAdapter的:checkout回调中执行此操作.

class ApplicationController < ActionController::Base
  before_action :log_user

  ActiveRecord::ConnectionAdapters::OracleEnhancedAdapter.set_callback :checkout,:after do

    # Would like to get the logged-in user's username here so I can apply 
    # it to the oracle sys_context.

    # The below throws "undefined method 'user_signed_in?'"
    username = current_user.username if user_signed_in?

  end

  def log_user
    # When in this method,user_signed_in? and current_user work fine.
    puts "User is #{current_user.username}" if user_signed_in?
  end
end

user_signed_in?在:checkout回调块中运行时找不到方法,尽管它通常在控制器中可用.为什么?

此外,块中的current_user似乎评估为ConnectionAdapter中定义的current_user方法,而不是Devise定义的方法.如何访问Devise的current_user?

如何在此回调中使用这些Devise提供的方法?

解决方法

您不能使用checkout回调,在执行它时,它没有与控制器上下文的连接.您在ApplicationController中定义它的事实与它实际执行的上下文无关.

您需要在before_action中设置连接选项,以便在控制器上下文中运行.就像是:

before_action :set_user_context

def set_user_context
  if current_user
    ApplicationRecord.connection.execute "DBMS_SESSION.SET_CONTEXT('whatever','goes','here','#{current_user.username}')"
  end
end

…或类似的东西.请注意,您可能需要添加checkin回调以在连接完成时清除该值.

顺便说一下,我几天前回答了一个几乎完全相同的问题:https://stackoverflow.com/a/54837596/152786不同的命令,但可能有所帮助.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读