context_srv.ts 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  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. constructor() {
  12. if (config.bootData.user) {
  13. _.extend(this, config.bootData.user);
  14. }
  15. }
  16. }
  17. export class ContextSrv {
  18. pinned: any;
  19. version: any;
  20. user: User;
  21. isSignedIn: any;
  22. isGrafanaAdmin: any;
  23. isEditor: any;
  24. sidemenu: any;
  25. constructor() {
  26. this.pinned = store.getBool('grafana.sidemenu.pinned', false);
  27. if (this.pinned) {
  28. this.sidemenu = true;
  29. }
  30. if (!config.buildInfo) {
  31. config.buildInfo = {};
  32. }
  33. if (!config.bootData) {
  34. config.bootData = {user: {}, settings: {}};
  35. }
  36. this.version = config.buildInfo.version;
  37. this.user = new User();
  38. this.isSignedIn = this.user.isSignedIn;
  39. this.isGrafanaAdmin = this.user.isGrafanaAdmin;
  40. this.isEditor = this.hasRole('Editor') || this.hasRole('Admin');
  41. }
  42. hasRole(role) {
  43. return this.user.orgRole === role;
  44. }
  45. setPinnedState(val) {
  46. this.pinned = val;
  47. store.set('grafana.sidemenu.pinned', val);
  48. }
  49. toggleSideMenu() {
  50. this.sidemenu = !this.sidemenu;
  51. if (!this.sidemenu) {
  52. this.setPinnedState(false);
  53. }
  54. }
  55. }
  56. var contextSrv = new ContextSrv();
  57. export {contextSrv};
  58. coreModule.factory('contextSrv', function() {
  59. return contextSrv;
  60. });