diff --git a/pkg/api/api.go b/pkg/api/api.go index 7d2d679362e..6bc4e2f93a5 100644 --- a/pkg/api/api.go +++ b/pkg/api/api.go @@ -117,6 +117,7 @@ func Register(r *macaron.Macaron) { r.Get("/:id", wrap(GetUserById)) r.Get("/:id/orgs", wrap(GetUserOrgList)) r.Put("/:id", bind(m.UpdateUserCommand{}), wrap(UpdateUser)) + r.Post("/:id/using/:orgId", wrap(UpdateUserActiveOrg)) }, reqGrafanaAdmin) // org information available to all users. diff --git a/pkg/api/dataproxy.go b/pkg/api/dataproxy.go index b00ef595161..9a062ddfd2d 100644 --- a/pkg/api/dataproxy.go +++ b/pkg/api/dataproxy.go @@ -55,6 +55,13 @@ func NewReverseProxy(ds *m.DataSource, proxyPath string, targetUrl *url.URL) *ht req.Header.Add("Authorization", util.GetBasicAuthHeader(ds.BasicAuthUser, ds.BasicAuthPassword)) } + dsAuth := req.Header.Get("X-DS-Authorization") + if len(dsAuth) > 0 { + req.Header.Del("X-DS-Authorization") + req.Header.Del("Authorization") + req.Header.Add("Authorization", dsAuth) + } + // clear cookie headers req.Header.Del("Cookie") req.Header.Del("Set-Cookie") diff --git a/pkg/api/user.go b/pkg/api/user.go index 8f54feaf6a0..f98eec02c40 100644 --- a/pkg/api/user.go +++ b/pkg/api/user.go @@ -40,6 +40,24 @@ func UpdateUser(c *middleware.Context, cmd m.UpdateUserCommand) Response { return handleUpdateUser(cmd) } +//POST /api/users/:id/using/:orgId +func UpdateUserActiveOrg(c *middleware.Context) Response { + userId := c.ParamsInt64(":id") + orgId := c.ParamsInt64(":orgId") + + if !validateUsingOrg(userId, orgId) { + return ApiError(401, "Not a valid organization", nil) + } + + cmd := m.SetUsingOrgCommand{UserId: userId, OrgId: orgId} + + if err := bus.Dispatch(&cmd); err != nil { + return ApiError(500, "Failed change active organization", err) + } + + return ApiSuccess("Active organization changed") +} + func handleUpdateUser(cmd m.UpdateUserCommand) Response { if len(cmd.Login) == 0 { cmd.Login = cmd.Email diff --git a/public/app/core/services/backend_srv.js b/public/app/core/services/backend_srv.js index ff3784ab45e..f27e427c70b 100644 --- a/public/app/core/services/backend_srv.js +++ b/public/app/core/services/backend_srv.js @@ -96,6 +96,11 @@ function (angular, _, coreModule, config) { var requestIsLocal = options.url.indexOf('/') === 0; var firstAttempt = options.retry === 0; + if (requestIsLocal && options.headers && options.headers.Authorization) { + options.headers['X-DS-Authorization'] = options.headers.Authorization; + delete options.headers.Authorization; + } + return $http(options).then(null, function(err) { // handle unauthorized for backend requests if (requestIsLocal && firstAttempt && err.status === 401) { diff --git a/public/app/features/dashboard/submenu/submenu.ts b/public/app/features/dashboard/submenu/submenu.ts index 9852dda4fd0..3b9b0b6b0d8 100644 --- a/public/app/features/dashboard/submenu/submenu.ts +++ b/public/app/features/dashboard/submenu/submenu.ts @@ -26,25 +26,8 @@ export class SubmenuCtrl { return this.templateValuesSrv.getValuesForTag(variable, tagKey); } - updateUrlParamsWithCurrentVariables() { - // update url - var params = this.$location.search(); - // remove variable params - _.each(params, function(value, key) { - if (key.indexOf('var-') === 0) { - delete params[key]; - } - }); - - // add new values - this.templateSrv.fillVariableValuesForUrl(params); - // update url - this.$location.search(params); - } - variableUpdated(variable) { this.templateValuesSrv.variableUpdated(variable).then(() => { - this.updateUrlParamsWithCurrentVariables(); this.$rootScope.$emit('template-variable-value-updated'); this.$rootScope.$broadcast('refresh'); }); diff --git a/public/app/features/dashboard/timeSrv.js b/public/app/features/dashboard/timeSrv.js index 96ba832f8b0..cd52155ff5f 100644 --- a/public/app/features/dashboard/timeSrv.js +++ b/public/app/features/dashboard/timeSrv.js @@ -10,7 +10,7 @@ define([ var module = angular.module('grafana.services'); - module.service('timeSrv', function($rootScope, $timeout, $routeParams, timer, $location) { + module.service('timeSrv', function($rootScope, $timeout, $routeParams, timer) { var self = this; this.init = function(dashboard) { @@ -108,13 +108,6 @@ define([ this.old_refresh = null; } - // update url params - var urlParams = $location.search(); - var urlRange = this.timeRangeForUrl(); - urlParams.from = urlRange.from; - urlParams.to = urlRange.to; - $location.search(urlParams); - $rootScope.appEvent('time-range-changed', this.time); $timeout(this.refreshDashboard, 0); }; diff --git a/public/app/features/dashboard/viewStateSrv.js b/public/app/features/dashboard/viewStateSrv.js index cda5e69f006..2138dd37438 100644 --- a/public/app/features/dashboard/viewStateSrv.js +++ b/public/app/features/dashboard/viewStateSrv.js @@ -8,7 +8,7 @@ function (angular, _, $) { var module = angular.module('grafana.services'); - module.factory('dashboardViewStateSrv', function($location, $timeout) { + module.factory('dashboardViewStateSrv', function($location, $timeout, templateSrv, contextSrv, timeSrv) { // represents the transient view state // like fullscreen panel & edit @@ -25,6 +25,19 @@ function (angular, _, $) { } }; + // update url on time range change + $scope.onAppEvent('time-range-changed', function() { + var urlParams = $location.search(); + var urlRange = timeSrv.timeRangeForUrl(); + urlParams.from = urlRange.from; + urlParams.to = urlRange.to; + $location.search(urlParams); + }); + + $scope.onAppEvent('template-variable-value-updated', function() { + self.updateUrlParamsWithCurrentVariables(); + }); + $scope.onAppEvent('$routeUpdate', function() { var urlState = self.getQueryStringState(); if (self.needsSync(urlState)) { @@ -40,10 +53,26 @@ function (angular, _, $) { self.registerPanel(payload.scope); }); - this.update(this.getQueryStringState(), true); + this.update(this.getQueryStringState()); this.expandRowForPanel(); } + DashboardViewState.prototype.updateUrlParamsWithCurrentVariables = function() { + // update url + var params = $location.search(); + // remove variable params + _.each(params, function(value, key) { + if (key.indexOf('var-') === 0) { + delete params[key]; + } + }); + + // add new values + templateSrv.fillVariableValuesForUrl(params); + // update url + $location.search(params); + }; + DashboardViewState.prototype.expandRowForPanel = function() { if (!this.state.panelId) { return; } @@ -63,6 +92,7 @@ function (angular, _, $) { state.fullscreen = state.fullscreen ? true : null; state.edit = (state.edit === "true" || state.edit === true) || null; state.editview = state.editview || null; + state.org = contextSrv.user.orgId; return state; }; @@ -70,10 +100,11 @@ function (angular, _, $) { var urlState = _.clone(this.state); urlState.fullscreen = this.state.fullscreen ? true : null; urlState.edit = this.state.edit ? true : null; + urlState.org = contextSrv.user.orgId; return urlState; }; - DashboardViewState.prototype.update = function(state, skipUrlSync) { + DashboardViewState.prototype.update = function(state) { _.extend(this.state, state); this.dashboard.meta.fullscreen = this.state.fullscreen; @@ -83,10 +114,7 @@ function (angular, _, $) { this.state.edit = null; } - if (!skipUrlSync) { - $location.search(this.serializeToUrl()); - } - + $location.search(this.serializeToUrl()); this.syncState(); }; diff --git a/public/app/features/panel/partials/soloPanel.html b/public/app/features/panel/partials/soloPanel.html index e76ad724343..131d99d4389 100644 --- a/public/app/features/panel/partials/soloPanel.html +++ b/public/app/features/panel/partials/soloPanel.html @@ -1,9 +1,5 @@ -