Refactor session_current_user_id in a shared concern (#4936)
This commit is contained in:
parent
c08e5f0a25
commit
0ffa127481
3 changed files with 13 additions and 12 deletions
|
|
@ -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
|
||||
|
||||
|
|
|
|||
|
|
@ -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
|
||||
|
|
|
|||
11
app/controllers/concerns/session_current_user.rb
Normal file
11
app/controllers/concerns/session_current_user.rb
Normal 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
|
||||
Loading…
Add table
Reference in a new issue