Restrict Explore UI to Editor and Admin roles

Access is restricted via not showing in the following places:

* hide from sidemenu
* hide from panel header menu
* disable keybinding `x`

Also adds a `roles` property to reactContainer routes that will be
checked if `roles` is set, and on failure redirects to `/`.
This commit is contained in:
David Kaltschmidt 2018-05-30 13:13:29 +02:00
parent 7a3c1e162c
commit f69654fcd5
5 changed files with 41 additions and 19 deletions

View File

@ -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",

View File

@ -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 {
} }
}); });
// 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 {
} }
} }
}); });
}
// delete panel // delete panel
this.bind('p r', () => { this.bind('p r', () => {

View File

@ -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 {
// 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 {
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();',

View File

@ -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) {
} }
/** @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,

View File

@ -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'),
}, },
}) })