summaryrefslogtreecommitdiff
path: root/app/controllers/application_controller.rb
diff options
context:
space:
mode:
authorStan Hu <stanhu@gmail.com>2017-10-02 23:28:22 -0700
committerStan Hu <stanhu@gmail.com>2017-10-04 11:47:36 -0700
commit5d52bb59dc632601f4028b430deae7348c5e279b (patch)
treec183ffd1755c4c9e4b784d07e4437664cf4b8129 /app/controllers/application_controller.rb
parentaaa57c9d35b1eb744fca970263220850db06639f (diff)
downloadgitlab-ce-5d52bb59dc632601f4028b430deae7348c5e279b.tar.gz
Fix username and ID not logging in production_json.log for Git activitysh-fix-username-logging
Devise sets `current_user`, but not all controllers authenticate users by session tokens. Try to use the controller-defined `authenticated_user` if `current_user` is not available. Closes gitlab-org/gitlab-ee#3611
Diffstat (limited to 'app/controllers/application_controller.rb')
-rw-r--r--app/controllers/application_controller.rb15
1 files changed, 12 insertions, 3 deletions
diff --git a/app/controllers/application_controller.rb b/app/controllers/application_controller.rb
index 771c6f3034a..967fe39256a 100644
--- a/app/controllers/application_controller.rb
+++ b/app/controllers/application_controller.rb
@@ -85,12 +85,21 @@ class ApplicationController < ActionController::Base
super
payload[:remote_ip] = request.remote_ip
- if current_user.present?
- payload[:user_id] = current_user.id
- payload[:username] = current_user.username
+ logged_user = auth_user
+
+ if logged_user.present?
+ payload[:user_id] = logged_user.try(:id)
+ payload[:username] = logged_user.try(:username)
end
end
+ # Controllers such as GitHttpController may use alternative methods
+ # (e.g. tokens) to authenticate the user, whereas Devise sets current_user
+ def auth_user
+ return current_user if current_user.present?
+ return try(:authenticated_user)
+ end
+
# This filter handles both private tokens and personal access tokens
def authenticate_user_from_private_token!
token = params[:private_token].presence || request.headers['PRIVATE-TOKEN'].presence