Refactor session_current_user_id in a shared concern (#4936)

This commit is contained in:
rhymes 2019-11-26 19:21:56 +01:00 committed by Ben Halpern
parent c08e5f0a25
commit 0ffa127481
3 changed files with 13 additions and 12 deletions

View file

@ -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

View file

@ -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

View file

@ -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