context_srv.ts 1.4 KB

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