diff --git a/app/controllers/application_controller.rb b/app/controllers/application_controller.rb index bfbdb8fdb..5087677c3 100644 --- a/app/controllers/application_controller.rb +++ b/app/controllers/application_controller.rb @@ -1,6 +1,7 @@ class ApplicationController < ActionController::Base protect_from_forgery with: :exception, prepend: true + include SessionCurrentUser include Pundit def require_http_auth @@ -18,11 +19,6 @@ class ApplicationController < ActionController::Base raise NotAuthorizedError, "Unauthorized" end - def session_current_user_id - session["warden.user.user.key"].flatten[0] if session["warden.user.user.key"].present? - end - helper_method :session_current_user_id - def authenticate_user! return if current_user diff --git a/app/controllers/application_metal_controller.rb b/app/controllers/application_metal_controller.rb index 70b3afc5a..1a7f1f18d 100644 --- a/app/controllers/application_metal_controller.rb +++ b/app/controllers/application_metal_controller.rb @@ -1,10 +1,4 @@ class ApplicationMetalController < ActionController::Metal # Any shared behavior across metal-oriented controllers can go here. - - def session_current_user_id - # This method should stay in sync with the ApplicationController equivalent - # Could/should be extracted to the appropriate place for ideal code sharing and efficiency - - session["warden.user.user.key"].flatten[0] if session["warden.user.user.key"].present? - end + include SessionCurrentUser end diff --git a/app/controllers/concerns/session_current_user.rb b/app/controllers/concerns/session_current_user.rb new file mode 100644 index 000000000..02e2b5a70 --- /dev/null +++ b/app/controllers/concerns/session_current_user.rb @@ -0,0 +1,11 @@ +module SessionCurrentUser + extend ActiveSupport::Concern + + # Extracts the current user ID from the session + def session_current_user_id + return unless session["warden.user.user.key"] + + # the value is in the format [[1], "..."] where 1 is the ID + session["warden.user.user.key"].first.first + end +end