I'm migrating an existing app to Rails 5 and using Devise for authentication. Database is Oracle12c.
I'm trying to figure out how to execute the following sql on the rails connection whenever a user would query the database. This context is required for the schema triggers to function correctly and this context should reflect the username of the user that is executing the query.
begin
main.ENV_VAR.prc_set_context_value('UserContext', '#{current_user}');
end;
Is there some sort of ActiveRecord hook to use to whenever a user is querying? Is there a Devise hook to use? Do I override ActiveRecord::Base?
Thanks for any assistance.
I don't know anything about Oracle, so adjust the execute command itself based on what makes sense. This should be in your ApplicationController
or whatever base controller you do your devise authenticate_user!
callback in, and make sure this comes after that callback:
before_action :authenticate_user!
before_action :set_user_context
protected
def set_user_context
if current_user
ApplicationRecord.connection.execute "main.ENV_VAR.prc_set_context_value('UserContext', '#{current_user.id}')"
end
end
Note that I also switched your current_user
to current_user.id
.