Commit b5c53aae by David Committed by GitHub

Merge pull request #12108 from grafana/davkal/12001-explore-permissions

Restrict Explore UI to Editor and Admin roles
parents 574e92e1 0c45ee63
...@@ -77,6 +77,9 @@ func (hs *HTTPServer) registerRoutes() { ...@@ -77,6 +77,9 @@ func (hs *HTTPServer) registerRoutes() {
r.Get("/dashboards/", reqSignedIn, Index) r.Get("/dashboards/", reqSignedIn, Index)
r.Get("/dashboards/*", reqSignedIn, Index) r.Get("/dashboards/*", reqSignedIn, Index)
r.Get("/explore/", reqEditorRole, Index)
r.Get("/explore/*", reqEditorRole, Index)
r.Get("/playlists/", reqSignedIn, Index) r.Get("/playlists/", reqSignedIn, Index)
r.Get("/playlists/*", reqSignedIn, Index) r.Get("/playlists/*", reqSignedIn, Index)
r.Get("/alerting/", reqSignedIn, Index) r.Get("/alerting/", reqSignedIn, Index)
......
...@@ -128,7 +128,7 @@ func setIndexViewData(c *m.ReqContext) (*dtos.IndexViewData, error) { ...@@ -128,7 +128,7 @@ func setIndexViewData(c *m.ReqContext) (*dtos.IndexViewData, error) {
Children: dashboardChildNavs, Children: dashboardChildNavs,
}) })
if setting.ExploreEnabled { if setting.ExploreEnabled && (c.OrgRole == m.ROLE_ADMIN || c.OrgRole == m.ROLE_EDITOR) {
data.NavTree = append(data.NavTree, &dtos.NavLink{ data.NavTree = append(data.NavTree, &dtos.NavLink{
Text: "Explore", Text: "Explore",
Id: "explore", Id: "explore",
......
...@@ -14,7 +14,7 @@ export class KeybindingSrv { ...@@ -14,7 +14,7 @@ export class KeybindingSrv {
timepickerOpen = false; timepickerOpen = false;
/** @ngInject */ /** @ngInject */
constructor(private $rootScope, private $location, private datasourceSrv, private timeSrv) { constructor(private $rootScope, private $location, private datasourceSrv, private timeSrv, private contextSrv) {
// clear out all shortcuts on route change // clear out all shortcuts on route change
$rootScope.$on('$routeChangeSuccess', () => { $rootScope.$on('$routeChangeSuccess', () => {
Mousetrap.reset(); Mousetrap.reset();
...@@ -177,6 +177,8 @@ export class KeybindingSrv { ...@@ -177,6 +177,8 @@ export class KeybindingSrv {
} }
}); });
// jump to explore if permissions allow
if (this.contextSrv.isEditor) {
this.bind('x', async () => { this.bind('x', async () => {
if (dashboard.meta.focusPanelId) { if (dashboard.meta.focusPanelId) {
const panel = dashboard.getPanelById(dashboard.meta.focusPanelId); const panel = dashboard.getPanelById(dashboard.meta.focusPanelId);
...@@ -192,6 +194,7 @@ export class KeybindingSrv { ...@@ -192,6 +194,7 @@ export class KeybindingSrv {
} }
} }
}); });
}
// delete panel // delete panel
this.bind('p r', () => { this.bind('p r', () => {
......
import config from 'app/core/config';
import $ from 'jquery'; import $ from 'jquery';
import _ from 'lodash'; import _ from 'lodash';
import config from 'app/core/config';
import kbn from 'app/core/utils/kbn'; import kbn from 'app/core/utils/kbn';
import { PanelCtrl } from 'app/features/panel/panel_ctrl'; import { PanelCtrl } from 'app/features/panel/panel_ctrl';
import * as rangeUtil from 'app/core/utils/rangeutil'; import * as rangeUtil from 'app/core/utils/rangeutil';
import * as dateMath from 'app/core/utils/datemath'; import * as dateMath from 'app/core/utils/datemath';
import { encodePathComponent } from 'app/core/utils/location_util'; import { encodePathComponent } from 'app/core/utils/location_util';
...@@ -16,6 +16,7 @@ class MetricsPanelCtrl extends PanelCtrl { ...@@ -16,6 +16,7 @@ class MetricsPanelCtrl extends PanelCtrl {
datasourceName: any; datasourceName: any;
$q: any; $q: any;
$timeout: any; $timeout: any;
contextSrv: any;
datasourceSrv: any; datasourceSrv: any;
timeSrv: any; timeSrv: any;
templateSrv: any; templateSrv: any;
...@@ -37,6 +38,7 @@ class MetricsPanelCtrl extends PanelCtrl { ...@@ -37,6 +38,7 @@ class MetricsPanelCtrl extends PanelCtrl {
// make metrics tab the default // make metrics tab the default
this.editorTabIndex = 1; this.editorTabIndex = 1;
this.$q = $injector.get('$q'); this.$q = $injector.get('$q');
this.contextSrv = $injector.get('contextSrv');
this.datasourceSrv = $injector.get('datasourceSrv'); this.datasourceSrv = $injector.get('datasourceSrv');
this.timeSrv = $injector.get('timeSrv'); this.timeSrv = $injector.get('timeSrv');
this.templateSrv = $injector.get('templateSrv'); this.templateSrv = $injector.get('templateSrv');
...@@ -312,7 +314,7 @@ class MetricsPanelCtrl extends PanelCtrl { ...@@ -312,7 +314,7 @@ class MetricsPanelCtrl extends PanelCtrl {
getAdditionalMenuItems() { getAdditionalMenuItems() {
const items = []; const items = [];
if (this.datasource && this.datasource.supportsExplore) { if (this.contextSrv.isEditor && this.datasource && this.datasource.supportsExplore) {
items.push({ items.push({
text: 'Explore', text: 'Explore',
click: 'ctrl.explore();', click: 'ctrl.explore();',
......
...@@ -24,8 +24,9 @@ describe('MetricsPanelCtrl', () => { ...@@ -24,8 +24,9 @@ describe('MetricsPanelCtrl', () => {
}); });
}); });
describe('and has datasource set that supports explore', () => { describe('and has datasource set that supports explore and user has powers', () => {
beforeEach(() => { beforeEach(() => {
ctrl.contextSrv = { isEditor: true };
ctrl.datasource = { supportsExplore: true }; ctrl.datasource = { supportsExplore: true };
additionalItems = ctrl.getAdditionalMenuItems(); additionalItems = ctrl.getAdditionalMenuItems();
}); });
......
...@@ -6,6 +6,7 @@ import coreModule from 'app/core/core_module'; ...@@ -6,6 +6,7 @@ import coreModule from 'app/core/core_module';
import { store } from 'app/stores/store'; import { store } from 'app/stores/store';
import { BackendSrv } from 'app/core/services/backend_srv'; import { BackendSrv } from 'app/core/services/backend_srv';
import { DatasourceSrv } from 'app/features/plugins/datasource_srv'; import { DatasourceSrv } from 'app/features/plugins/datasource_srv';
import { ContextSrv } from 'app/core/services/context_srv';
function WrapInProvider(store, Component, props) { function WrapInProvider(store, Component, props) {
return ( return (
...@@ -16,16 +17,31 @@ function WrapInProvider(store, Component, props) { ...@@ -16,16 +17,31 @@ function WrapInProvider(store, Component, props) {
} }
/** @ngInject */ /** @ngInject */
export function reactContainer($route, $location, backendSrv: BackendSrv, datasourceSrv: DatasourceSrv) { export function reactContainer(
$route,
$location,
backendSrv: BackendSrv,
datasourceSrv: DatasourceSrv,
contextSrv: ContextSrv
) {
return { return {
restrict: 'E', restrict: 'E',
template: '', template: '',
link(scope, elem) { link(scope, elem) {
let component = $route.current.locals.component; // Check permissions for this component
const { roles } = $route.current.locals;
if (roles && roles.length) {
if (!roles.some(r => contextSrv.hasRole(r))) {
$location.url('/');
}
}
let { component } = $route.current.locals;
// Dynamic imports return whole module, need to extract default export // Dynamic imports return whole module, need to extract default export
if (component.default) { if (component.default) {
component = component.default; component = component.default;
} }
const props = { const props = {
backendSrv: backendSrv, backendSrv: backendSrv,
datasourceSrv: datasourceSrv, datasourceSrv: datasourceSrv,
......
...@@ -113,6 +113,7 @@ export function setupAngularRoutes($routeProvider, $locationProvider) { ...@@ -113,6 +113,7 @@ export function setupAngularRoutes($routeProvider, $locationProvider) {
.when('/explore/:initial?', { .when('/explore/:initial?', {
template: '<react-container />', template: '<react-container />',
resolve: { resolve: {
roles: () => ['Editor', 'Admin'],
component: () => import(/* webpackChunkName: "explore" */ 'app/containers/Explore/Wrapper'), component: () => import(/* webpackChunkName: "explore" */ 'app/containers/Explore/Wrapper'),
}, },
}) })
......
...@@ -11,6 +11,7 @@ export function ControllerTestContext() { ...@@ -11,6 +11,7 @@ export function ControllerTestContext() {
this.$element = {}; this.$element = {};
this.$sanitize = {}; this.$sanitize = {};
this.annotationsSrv = {}; this.annotationsSrv = {};
this.contextSrv = {};
this.timeSrv = new TimeSrvStub(); this.timeSrv = new TimeSrvStub();
this.templateSrv = new TemplateSrvStub(); this.templateSrv = new TemplateSrvStub();
this.datasourceSrv = { this.datasourceSrv = {
...@@ -27,6 +28,7 @@ export function ControllerTestContext() { ...@@ -27,6 +28,7 @@ export function ControllerTestContext() {
this.providePhase = function(mocks) { this.providePhase = function(mocks) {
return angularMocks.module(function($provide) { return angularMocks.module(function($provide) {
$provide.value('contextSrv', self.contextSrv);
$provide.value('datasourceSrv', self.datasourceSrv); $provide.value('datasourceSrv', self.datasourceSrv);
$provide.value('annotationsSrv', self.annotationsSrv); $provide.value('annotationsSrv', self.annotationsSrv);
$provide.value('timeSrv', self.timeSrv); $provide.value('timeSrv', self.timeSrv);
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment