From f98e176d7be9eab14b20529767465eed1534a8f4 Mon Sep 17 00:00:00 2001 From: aidanmountford Date: Tue, 2 Jun 2020 16:47:54 +1000 Subject: [PATCH] Templating: Add bult in __user {name, id, login, email} variable to templating system (#23378) * Updated templating code to support the $__user variable to expose the current userid, username, email and login. Fixed the $__org variable as it was returning the user id instead of the ordId. Updated the documentation to match * Updated solution to retrieving $__user variables to pull directly from user record (thereby allowing future access to properties that might not exist in the contextSvr). Replicated this initialisation in the variables feature Corrected typo's in documentation. * Repaired typecheck issues. * Updated patch to pull entirely from contextSrv without API calls. * ... And removed the redundant comments. * Updated documentation. --- public/app/core/services/context_srv.ts | 2 ++ public/app/features/templating/variable_srv.ts | 12 ++++++++++++ public/app/features/variables/state/actions.ts | 11 +++++++++++ 3 files changed, 25 insertions(+) diff --git a/public/app/core/services/context_srv.ts b/public/app/core/services/context_srv.ts index b6a3c8f08cc..e3edac66205 100644 --- a/public/app/core/services/context_srv.ts +++ b/public/app/core/services/context_srv.ts @@ -15,6 +15,8 @@ export class User { timezone: string; helpFlags1: number; lightTheme: boolean; + name?: string; + email?: string; hasEditPermissionInFolders: boolean; constructor() { diff --git a/public/app/features/templating/variable_srv.ts b/public/app/features/templating/variable_srv.ts index 33b399e634c..10cbd2dcb17 100644 --- a/public/app/features/templating/variable_srv.ts +++ b/public/app/features/templating/variable_srv.ts @@ -8,6 +8,7 @@ import { Graph } from 'app/core/utils/dag'; import { TemplateSrv } from 'app/features/templating/template_srv'; import { TimeSrv } from 'app/features/dashboard/services/TimeSrv'; import { DashboardModel } from 'app/features/dashboard/state/DashboardModel'; + // Types import { AppEvents, TimeRange, UrlQueryMap } from '@grafana/data'; import { CoreEvents } from 'app/types'; @@ -64,6 +65,17 @@ export class VariableSrv { this.templateSrv.setGlobalVariable('__org', { value: { name: contextSrv.user.orgName, + id: contextSrv.user.orgId, + toString: function() { + return this.id; + }, + }, + }); + this.templateSrv.setGlobalVariable('__user', { + value: { + name: contextSrv.user.name, + login: contextSrv.user.login, + email: contextSrv.user.email, id: contextSrv.user.id, toString: function() { return this.id; diff --git a/public/app/features/variables/state/actions.ts b/public/app/features/variables/state/actions.ts index 89abbf91a01..f00fc37efbb 100644 --- a/public/app/features/variables/state/actions.ts +++ b/public/app/features/variables/state/actions.ts @@ -102,6 +102,17 @@ export const completeDashboardTemplating = (dashboard: DashboardModel): ThunkRes }, }, }); + templateSrv.setGlobalVariable('__user', { + value: { + name: contextSrv.user.name, + login: contextSrv.user.login, + email: contextSrv.user.email, + id: contextSrv.user.id, + toString: function() { + return this.id; + }, + }, + }); }; };