context_srv.ts 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. import config from 'app/core/config';
  2. import _ from 'lodash';
  3. import coreModule from 'app/core/core_module';
  4. import store from 'app/core/store';
  5. export class User {
  6. isGrafanaAdmin: any;
  7. isSignedIn: any;
  8. orgRole: any;
  9. orgId: number;
  10. orgName: string;
  11. orgCount: number;
  12. timezone: string;
  13. helpFlags1: number;
  14. lightTheme: boolean;
  15. hasEditPermissionInFolders: boolean;
  16. constructor() {
  17. if (config.bootData.user) {
  18. _.extend(this, config.bootData.user);
  19. }
  20. }
  21. }
  22. export class ContextSrv {
  23. pinned: any;
  24. version: any;
  25. user: User;
  26. isSignedIn: any;
  27. isGrafanaAdmin: any;
  28. isEditor: any;
  29. sidemenu: any;
  30. sidemenuSmallBreakpoint = false;
  31. hasEditPermissionInFolders: boolean;
  32. constructor() {
  33. this.sidemenu = store.getBool('grafana.sidemenu', true);
  34. if (!config.bootData) {
  35. config.bootData = { user: {}, settings: {} };
  36. }
  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. this.hasEditPermissionInFolders = this.user.hasEditPermissionInFolders;
  42. }
  43. hasRole(role) {
  44. return this.user.orgRole === role;
  45. }
  46. isGrafanaVisible() {
  47. return !!(document.visibilityState === undefined || document.visibilityState === 'visible');
  48. }
  49. toggleSideMenu() {
  50. this.sidemenu = !this.sidemenu;
  51. store.set('grafana.sidemenu', this.sidemenu);
  52. }
  53. hasAccessToExplore() {
  54. return (this.isEditor || config.viewersCanEdit) && config.exploreEnabled;
  55. }
  56. }
  57. const contextSrv = new ContextSrv();
  58. export { contextSrv };
  59. coreModule.factory('contextSrv', () => {
  60. return contextSrv;
  61. });