Merge branch 'enhance_raven'

This commit is contained in:
Sebastian Serth
2020-01-29 13:12:46 +01:00
3 changed files with 38 additions and 10 deletions

View File

@ -1,16 +1,18 @@
# frozen_string_literal: true
class ApplicationController < ActionController::Base
include ApplicationHelper
include Pundit
MEMBER_ACTIONS = [:destroy, :edit, :show, :update]
MEMBER_ACTIONS = %i[destroy edit show update].freeze
after_action :verify_authorized, except: [:help, :welcome]
before_action :set_locale, :allow_iframe_requests, :load_embed_options
after_action :verify_authorized, except: %i[help welcome]
before_action :set_raven_context, :set_locale, :allow_iframe_requests, :load_embed_options
protect_from_forgery(with: :exception, prepend: true)
rescue_from Pundit::NotAuthorizedError, with: :render_not_authorized
def current_user
::NewRelic::Agent.add_custom_attributes({ external_user_id: session[:external_user_id], session_user_id: session[:user_id] })
::NewRelic::Agent.add_custom_attributes(external_user_id: session[:external_user_id], session_user_id: session[:user_id])
@current_user ||= ExternalUser.find_by(id: session[:external_user_id]) || login_from_session || login_from_other_sources
end
@ -18,6 +20,19 @@ class ApplicationController < ActionController::Base
raise Pundit::NotAuthorizedError unless current_user
end
def set_raven_context
return if current_user.blank?
Raven.user_context(
id: current_user.id,
type: current_user.class.name,
email: current_user.email,
username: current_user.displayname,
consumer: current_user.consumer.name
)
end
private :set_raven_context
def render_not_authorized
respond_to do |format|
format.html do
@ -36,6 +51,7 @@ class ApplicationController < ActionController::Base
def set_locale
session[:locale] = params[:custom_locale] || params[:locale] || session[:locale]
I18n.locale = session[:locale] || I18n.default_locale
Raven.extra_context(locale: I18n.locale)
end
private :set_locale
@ -48,11 +64,12 @@ class ApplicationController < ActionController::Base
end
def load_embed_options
if session[:embed_options].present? && session[:embed_options].is_a?(Hash)
@embed_options = session[:embed_options].symbolize_keys
else
@embed_options = {}
end
@embed_options = if session[:embed_options].present? && session[:embed_options].is_a?(Hash)
session[:embed_options].symbolize_keys
else
{}
end
Raven.extra_context(@embed_options)
@embed_options
end
private :load_embed_options

View File

@ -166,6 +166,8 @@ describe InternalUsersController do
describe 'GET #forgot_password' do
context 'when no user is logged in' do
before(:each) do
allow(controller).to receive(:set_raven_context).and_return(nil)
expect(controller).to receive(:current_user).and_return(nil)
get :forgot_password
end
@ -176,6 +178,8 @@ describe InternalUsersController do
context 'when a user is already logged in' do
before(:each) do
allow(controller).to receive(:set_raven_context).and_return(nil)
expect(controller).to receive(:current_user).and_return(user)
get :forgot_password
end

View File

@ -154,7 +154,10 @@ describe SessionsController do
describe 'DELETE #destroy' do
let(:user) { double }
before(:each) { expect(controller).to receive(:current_user).at_least(:once).and_return(user) }
before(:each) {
allow(controller).to receive(:set_raven_context).and_return(nil)
expect(controller).to receive(:current_user).at_least(:once).and_return(user)
}
context 'with an internal user' do
before(:each) do
@ -217,6 +220,8 @@ describe SessionsController do
describe 'GET #new' do
context 'when no user is logged in' do
before(:each) do
allow(controller).to receive(:set_raven_context).and_return(nil)
expect(controller).to receive(:current_user).and_return(nil)
get :new
end
@ -227,6 +232,8 @@ describe SessionsController do
context 'when a user is already logged in' do
before(:each) do
allow(controller).to receive(:set_raven_context).and_return(nil)
expect(controller).to receive(:current_user).and_return(FactoryBot.build(:teacher))
get :new
end