context_srv.ts 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. ///<reference path="../../headers/common.d.ts" />
  2. import config from 'app/core/config';
  3. import _ from 'lodash';
  4. import coreModule from 'app/core/core_module';
  5. import store from 'app/core/store';
  6. export class User {
  7. isGrafanaAdmin: any;
  8. isSignedIn: any;
  9. orgRole: any;
  10. timezone: string;
  11. helpFlags1: number;
  12. constructor() {
  13. if (config.bootData.user) {
  14. _.extend(this, config.bootData.user);
  15. }
  16. }
  17. }
  18. export class ContextSrv {
  19. pinned: any;
  20. version: any;
  21. user: User;
  22. isSignedIn: any;
  23. isGrafanaAdmin: any;
  24. isEditor: any;
  25. sidemenu: any;
  26. constructor() {
  27. this.pinned = store.getBool('grafana.sidemenu.pinned', false);
  28. if (this.pinned) {
  29. this.sidemenu = true;
  30. }
  31. if (!config.buildInfo) {
  32. config.buildInfo = {};
  33. }
  34. if (!config.bootData) {
  35. config.bootData = {user: {}, settings: {}};
  36. }
  37. this.version = config.buildInfo.version;
  38. this.user = new User();
  39. this.isSignedIn = this.user.isSignedIn;
  40. this.isGrafanaAdmin = this.user.isGrafanaAdmin;
  41. this.isEditor = this.hasRole('Editor') || this.hasRole('Admin');
  42. }
  43. hasRole(role) {
  44. return this.user.orgRole === role;
  45. }
  46. setPinnedState(val) {
  47. this.pinned = val;
  48. store.set('grafana.sidemenu.pinned', val);
  49. }
  50. toggleSideMenu() {
  51. this.sidemenu = !this.sidemenu;
  52. if (!this.sidemenu) {
  53. this.setPinnedState(false);
  54. }
  55. }
  56. }
  57. var contextSrv = new ContextSrv();
  58. export {contextSrv};
  59. coreModule.factory('contextSrv', function() {
  60. return contextSrv;
  61. });