context_srv.js 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. define([
  2. 'angular',
  3. 'lodash',
  4. '../core_module',
  5. 'app/core/store',
  6. 'app/core/config',
  7. ],
  8. function (angular, _, coreModule, store, config) {
  9. 'use strict';
  10. coreModule.service('contextSrv', function($rootScope, $timeout) {
  11. var self = this;
  12. function User() {
  13. if (window.grafanaBootData.user) {
  14. _.extend(this, window.grafanaBootData.user);
  15. }
  16. }
  17. // events
  18. $rootScope.$on('toggle-sidemenu', function() {
  19. self.toggleSideMenu();
  20. });
  21. this.hasRole = function(role) {
  22. return this.user.orgRole === role;
  23. };
  24. this.setSideMenuState = function(state) {
  25. this.sidemenu = state;
  26. store.set('grafana.sidemenu', state);
  27. };
  28. this.toggleSideMenu = function() {
  29. this.setSideMenuState(!this.sidemenu);
  30. $timeout(function() {
  31. $rootScope.$broadcast("render");
  32. }, 50);
  33. };
  34. this.getSidemenuDefault = function() {
  35. return this.hasRole('Admin');
  36. };
  37. this.version = config.buildInfo.version;
  38. this.lightTheme = false;
  39. this.user = new User();
  40. this.isSignedIn = this.user.isSignedIn;
  41. this.isGrafanaAdmin = this.user.isGrafanaAdmin;
  42. this.sidemenu = store.getBool('grafana.sidemenu', this.getSidemenuDefault());
  43. if (this.isSignedIn && !store.exists('grafana.sidemenu')) {
  44. // If the sidemenu has never been set before, set it to false.
  45. // This will result in this.sidemenu and the localStorage grafana.sidemenu
  46. // to be out of sync if the user has an admin role. But this is
  47. // intentional and results in the user seeing the sidemenu only on
  48. // their first login.
  49. store.set('grafana.sidemenu', false);
  50. }
  51. this.isEditor = this.hasRole('Editor') || this.hasRole('Admin');
  52. });
  53. });