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.
This commit is contained in:
aidanmountford 2020-06-02 16:47:54 +10:00 committed by GitHub
parent 10158c90e3
commit f98e176d7b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 25 additions and 0 deletions

View File

@ -15,6 +15,8 @@ export class User {
timezone: string;
helpFlags1: number;
lightTheme: boolean;
name?: string;
email?: string;
hasEditPermissionInFolders: boolean;
constructor() {

View File

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

View File

@ -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;
},
},
});
};
};