context_srv.ts 1.5 KB

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