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.
pull/25277/head
aidanmountford 6 years ago committed by GitHub
parent 10158c90e3
commit f98e176d7b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 2
      public/app/core/services/context_srv.ts
  2. 12
      public/app/features/templating/variable_srv.ts
  3. 11
      public/app/features/variables/state/actions.ts

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

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

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

Loading…
Cancel
Save