context_srv.ts 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. import config from 'app/core/config';
  2. import _ from 'lodash';
  3. import coreModule from 'app/core/core_module';
  4. export class User {
  5. id: number;
  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. sidemenuSmallBreakpoint = false;
  30. hasEditPermissionInFolders: boolean;
  31. constructor() {
  32. if (!config.bootData) {
  33. config.bootData = { user: {}, settings: {} };
  34. }
  35. this.user = new User();
  36. this.isSignedIn = this.user.isSignedIn;
  37. this.isGrafanaAdmin = this.user.isGrafanaAdmin;
  38. this.isEditor = this.hasRole('Editor') || this.hasRole('Admin');
  39. this.hasEditPermissionInFolders = this.user.hasEditPermissionInFolders;
  40. }
  41. hasRole(role) {
  42. return this.user.orgRole === role;
  43. }
  44. isGrafanaVisible() {
  45. return !!(document.visibilityState === undefined || document.visibilityState === 'visible');
  46. }
  47. hasAccessToExplore() {
  48. return (this.isEditor || config.viewersCanEdit) && config.exploreEnabled;
  49. }
  50. }
  51. const contextSrv = new ContextSrv();
  52. export { contextSrv };
  53. coreModule.factory('contextSrv', () => {
  54. return contextSrv;
  55. });