From 2a25dc7ab873597de0505c35d8e50538805b9d8e Mon Sep 17 00:00:00 2001 From: "Grot (@grafanabot)" <43478413+grafanabot@users.noreply.github.com> Date: Thu, 7 Jul 2022 18:02:09 +0100 Subject: [PATCH] Rendering: Fix user information when using render key (#50879) (#51920) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * Rendering: Fix user information when using render key * fix when render user ID is 0 * update fix * improve fix * add comment (cherry picked from commit ca80865bf19ab7794c4f82cc085170eec9a7ecf9) Co-authored-by: Agnès Toulet <35176601+AgnesToulet@users.noreply.github.com> --- pkg/services/contexthandler/contexthandler.go | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) diff --git a/pkg/services/contexthandler/contexthandler.go b/pkg/services/contexthandler/contexthandler.go index 4e09458bd5b..83070bacefc 100644 --- a/pkg/services/contexthandler/contexthandler.go +++ b/pkg/services/contexthandler/contexthandler.go @@ -445,7 +445,7 @@ func (h *ContextHandler) initContextWithRenderAuth(reqContext *models.ReqContext return false } - _, span := h.tracer.Start(reqContext.Req.Context(), "initContextWithRenderAuth") + ctx, span := h.tracer.Start(reqContext.Req.Context(), "initContextWithRenderAuth") defer span.End() renderUser, exists := h.RenderService.GetRenderUser(reqContext.Req.Context(), key) @@ -454,12 +454,21 @@ func (h *ContextHandler) initContextWithRenderAuth(reqContext *models.ReqContext return true } - reqContext.IsSignedIn = true reqContext.SignedInUser = &models.SignedInUser{ OrgId: renderUser.OrgID, UserId: renderUser.UserID, OrgRole: models.RoleType(renderUser.OrgRole), } + + // UserID can be 0 for background tasks and, in this case, there is no user info to retrieve + if renderUser.UserID != 0 { + query := models.GetSignedInUserQuery{UserId: renderUser.UserID, OrgId: renderUser.OrgID} + if err := h.SQLStore.GetSignedInUserWithCacheCtx(ctx, &query); err == nil { + reqContext.SignedInUser = query.Result + } + } + + reqContext.IsSignedIn = true reqContext.IsRenderCall = true reqContext.LastSeenAt = time.Now() return true